Improve Paperoids example.

This commit is contained in:
Jonathan Puckey 2013-03-10 15:58:27 +01:00
parent fd3fc7f2d7
commit f6cf20fa8c

View file

@ -245,31 +245,8 @@
var Bullets = new function() {
var group = new Group();
var children = group.children;
return {
fire: function(position, angle) {
if (children.length > 4) return;
var bullet = new Path.Circle(position, 0.5);
bullet.fillColor = 'white';
bullet.strokeWidth = 0;
bullet.data = {
vector: new Point({
angle: angle,
length: 10
}),
timeToDie: 58
};
bullet.position += bullet.data.vector;
group.addChild(bullet);
},
move: function() {
// check for bullet hit
for (var i = 0; i < children.length; i++) {
var bullet = children[i];
bullet.data.timeToDie--;
if (bullet.data.timeToDie < 1) {
bullet.remove();
} else {
bullet.position += bullet.data.vector;
function checkHits(bullet) {
for (var r = 0; r < Rocks.children.length; r++) {
var rock = Rocks.children[r];
if (rock.bounds.contains(bullet.position) ) {
@ -284,6 +261,39 @@
bullet.remove();
}
}
}
return {
fire: function(position, angle) {
// We can only fire 5 bullets at a time:
if (children.length == 5)
return;
var vector = new Point({
angle: angle,
length: 10
});
var bullet = new Path.Circle({
center: position + vector,
radius: 0.5,
parent: group,
fillColor: 'white',
strokeWidth: 'white',
strokeWidth: 0,
data: {
vector: vector,
timeToDie: 58
}
});
},
move: function() {
for (var i = 0; i < children.length; i++) {
var bullet = children[i];
bullet.data.timeToDie--;
if (bullet.data.timeToDie < 1) {
bullet.remove();
} else {
bullet.position += bullet.data.vector;
checkHits(bullet);
keepInView(bullet);
}
}