xonecas.io
211
Ze

zen / commit 920d914

fixed invaders movement

xonecas2011-02-07 920d91433794fd471ce8d482d28f705e1d463013 parent d416682

1 file changed

js/script.js+42−21
@@ -165,7 +165,6 @@
165165 });
166166
167167 var Invader = Image.extend({
168- isDrawn: false,
169168 face: '1',
170169 direction: 1,
171170
@@ -203,22 +202,14 @@
203202
204203 },
205204
206- move: function (width, others) {
207- var that = this;
208-
209- (function trust () {
210- that.moving = true;
211- that.clear();
212- that.x += IMGSIZE * that.direction;
213- if (!that.isDead) {
214- that.draw(that.x, that.y);
215- setTimeout(trust, 1000);
216- }
217- that.moving = false;
205+ move: function (direction) {
206+ this.moving = true;
207+ this.clear();
208+ this.x += IMGSIZE * direction;
209+ if (!this.isDead)
210+ this.draw(this.x, this.y);
218211
219- if ((others[others.length -1].x + IMGSIZE) >= width || others[0].x <= (PADSIZE + IMGSIZE))
220- that.direction = (that.direction === 1)? -1: 1;
221- }) ();
212+ this.moving = false;
222213 }
223214 });
224215
@@ -241,13 +232,23 @@
241232 this.started = true;
242233 this.cannon = new Cannon(IMGSIZE, this.ctx, get('cannon'));
243234 this.cannon.draw(this.width /2, this.height - (IMGSIZE + PADSIZE));
244-
235+
245236 var rows = 2, cols = 11, index = 0, center = this.width/2,
246237 x = Math.floor(center - (((IMGSIZE + PADSIZE) * 11) - 20) /2), y = PADSIZE;
238+
239+ this.formation = {
240+ width: (IMGSIZE+PADSIZE)*11-20,
241+ height: (IMGSIZE+PADSIZE)*2-20,
242+ 'x': x, 'y': y,
243+ direction: 1
244+ };
245+
246+
247247 while (rows--) {
248248 while (cols--) {
249249 index = this.invaders.push(new Invader(IMGSIZE, this.ctx, 'skully')) -1;
250- this.invaders[index].live(x, y);
250+ var invader = this.invaders[index];
251+ invader.live(x, y);
251252 x += IMGSIZE + PADSIZE;
252253 }
253254 y += IMGSIZE + PADSIZE;
@@ -255,7 +256,6 @@
255256 cols = 11;
256257 }
257258
258-
259259 },
260260
261261 // Death to HUMANS!
@@ -273,9 +273,29 @@
273273 setTimeout(attack, 3000);
274274 }
275275 }) ();
276+ },
277+
278+ moveFormation: function () {
279+ var invaders = this.invaders,
280+ formation = this.formation,
281+ block = PADSIZE+IMGSIZE,
282+ that = this;
276283
277- for (var ship = 0; ship < this.invaders.length; ship++)
278- this.invaders[ship].move(this.width, this.invaders);
284+ (function attackangle () {
285+ var x_max = formation.x + formation.width;
286+ if (x_max >= that.width - block || formation.x <= PADSIZE )
287+ formation.direction = (formation.direction === -1)? 1: -1;
288+
289+ for (var z=0; z < invaders.length; z++) {
290+
291+ var invader = invaders[z];
292+ invader.move(formation.direction);
293+ }
294+ formation.x += (block*formation.direction);
295+
296+
297+ setTimeout(attackangle, 1000);
298+ }) ();
279299 },
280300
281301 keydown: function (ev) {
@@ -321,6 +341,7 @@
321341 game.keyup.call(game, ev);
322342 }
323343
344+ game.moveFormation();
324345 game.invade();
325346 }