Fix: group -> array, tint grey while disbled
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user