From f2cce23d9b29a6703f95986d36955f3b27a5ab52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=A2=E1=84=91=E1=85=B3=E1=86=AF=20=E1=84=82?= =?UTF-8?q?=E1=85=A9=E1=84=90=E1=85=B3=E1=84=87=E1=85=AE=E1=86=A8?= Date: Sat, 12 May 2018 22:31:15 +0900 Subject: [PATCH] Fix: group -> array, tint grey while disbled --- src/game/mouse/space_invaders/alien.js | 52 ++++++++++++++++++-------- src/game/mouse/space_invaders/game.js | 29 ++++++++------ 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/src/game/mouse/space_invaders/alien.js b/src/game/mouse/space_invaders/alien.js index 2cd3b7d..d449030 100644 --- a/src/game/mouse/space_invaders/alien.js +++ b/src/game/mouse/space_invaders/alien.js @@ -1,15 +1,16 @@ class Alien extends Phaser.Sprite { - constructor(initialX, initialY, onFiredFunction, onEscapedFunction) { + constructor(initialX, initialY, onGoOnstageFunction, onFiredFunction, onEscapedFunction) { super(); this.isActivated = false; - this.isOnstage = false; + this.isOnWaitingRoom = true; this.speedGrowing = Alien.DEFAULT_SPEED_SEC; this.initialX = initialX; this.initialY = initialY; + this.onGoOnstageFunction = onGoOnstageFunction; this.onFiredFunction = onFiredFunction; this.onEscapedFunction = onEscapedFunction; @@ -29,38 +30,42 @@ class Alien extends Phaser.Sprite { onOut() { if(!this.isActivated) return; + + this.tint = 0xffffff; } onOver() { if(!this.isActivated) return; + + this.tint = 0xffaaaa; } onDown() { if(!this.isActivated) return; - if(!this.isOnstage) { - this.isOnstage = true; + if(this.isOnWaitingRoom) { + this.isOnWaitingRoom = false; this.goOnstage(); return; + } else { + this.onFired(); } - - if(typeof this.onFiredFunction !== "undefined") - this.onFiredFunction(this); } setActive(isActivated) { - let beforeState = this.isActivated; this.isActivated = isActivated; - if(beforeState && !this.isActivated) { - this.shutdown(); + if(this.isActivated) { + this.tint = 0xffffff; + } else { + this.tint = 0x555555; } } - shutdown() { + removeTween() { if(typeof this.tweenScaleUp !== "undefined") { this.tweenScaleUp.stop(); this.tweenScaleUp = null; @@ -70,11 +75,11 @@ class Alien extends Phaser.Sprite { this.tweenScaleDown = null; } - this.scale.set(0); + this.scale.set(Alien.ONSTAGE_MIN_SCALE); } goWaitingRoom() { - this.isOnstage = false; + this.isOnWaitingRoom = true; this.x = this.initialX; this.y = this.initialY; @@ -82,6 +87,20 @@ class Alien extends Phaser.Sprite { } goOnstage() { + this.goRandomPosition(); + + if(typeof this.onGoOnstageFunction !== "undefined") + this.onGoOnstageFunction(); + } + + onFired() { + this.goRandomPosition(); + + if(typeof this.onFiredFunction !== "undefined") + this.onFiredFunction(); + } + + goRandomPosition() { // set random position const SPAWN_BOX_LEFT = 80; const SPAWN_BOX_WIDTH = game.world.width - 160; @@ -97,8 +116,10 @@ class Alien extends Phaser.Sprite { // .drawRect(SPAWN_BOX_LEFT, SPAWN_BOX_TOP, SPAWN_BOX_WIDTH, SPAWN_BOX_HEIGHT); // } + this.tint = 0xffffff; + // start scale tween animation - this.shutdown(); + this.removeTween(); this.tweenScaleUp = game.add.tween(this.scale) .to( { x: Alien.ONSTAGE_MAX_SCALE, y: Alien.ONSTAGE_MAX_SCALE }, Alien.DEFAULT_SPEED_SEC, Phaser.Easing.Linear.None, false); @@ -114,11 +135,12 @@ class Alien extends Phaser.Sprite { return; if(typeof this.onEscapedFunction !== "undefined") - this.onEscapedFunction(this); + this.onEscapedFunction(); } } Alien.DEFAULT_SPEED_SEC = 3000; Alien.WAITING_SCALE = 2; +Alien.ONSTAGE_MIN_SCALE = 0.1; Alien.ONSTAGE_MAX_SCALE = 3; diff --git a/src/game/mouse/space_invaders/game.js b/src/game/mouse/space_invaders/game.js index 1133239..178d5d6 100644 --- a/src/game/mouse/space_invaders/game.js +++ b/src/game/mouse/space_invaders/game.js @@ -21,18 +21,17 @@ class Game { // contents - this.alienGroup = game.add.group(); - this.alienGroup.inputEnableChildren = true; + this.activatedAlienIndex = 0; + this.aliens = []; for(let i = 0; i < 10; i++) { - let alien = new Alien(150 + 80 * i, 150, this.onAlienFired, this.onAlienFired); + let alien = new Alien(150 + 80 * i, 150, + () => { this.activateNextAlien(); }, // onGoOnstage + this.onAlienFired, this.onAlienEscaped); alien.goWaitingRoom(); alien.setActive(false); - this.alienGroup.add(alien); + this.aliens.push(alien); } - // activate first alien - // this.alienGroup[0].setActive(true); - // bottom let screenBottom = new ScreenBottom(game); @@ -78,15 +77,23 @@ class Game { */ startGame() { - console.log("startGame"); + // activate first alien + this.activateNextAlien(); } - onAlienFired(sprite) { + activateNextAlien() { + if(this.activatedAlienIndex === this.aliens.length) + return; + + this.aliens[this.activatedAlienIndex].setActive(true); + this.activatedAlienIndex++; + } + + onAlienFired() { console.log("onAlienFired"); - sprite.goOnstage(); } - onAlienEscaped(sprite) { + onAlienEscaped() { console.log("onAlienEscaped"); }