Fix: group -> array, tint grey while disbled
This commit is contained in:
@@ -1,15 +1,16 @@
|
|||||||
class Alien extends Phaser.Sprite {
|
class Alien extends Phaser.Sprite {
|
||||||
|
|
||||||
constructor(initialX, initialY, onFiredFunction, onEscapedFunction) {
|
constructor(initialX, initialY, onGoOnstageFunction, onFiredFunction, onEscapedFunction) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.isActivated = false;
|
this.isActivated = false;
|
||||||
this.isOnstage = false;
|
this.isOnWaitingRoom = true;
|
||||||
this.speedGrowing = Alien.DEFAULT_SPEED_SEC;
|
this.speedGrowing = Alien.DEFAULT_SPEED_SEC;
|
||||||
|
|
||||||
this.initialX = initialX;
|
this.initialX = initialX;
|
||||||
this.initialY = initialY;
|
this.initialY = initialY;
|
||||||
|
|
||||||
|
this.onGoOnstageFunction = onGoOnstageFunction;
|
||||||
this.onFiredFunction = onFiredFunction;
|
this.onFiredFunction = onFiredFunction;
|
||||||
this.onEscapedFunction = onEscapedFunction;
|
this.onEscapedFunction = onEscapedFunction;
|
||||||
|
|
||||||
@@ -29,38 +30,42 @@ class Alien extends Phaser.Sprite {
|
|||||||
onOut() {
|
onOut() {
|
||||||
if(!this.isActivated)
|
if(!this.isActivated)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
this.tint = 0xffffff;
|
||||||
}
|
}
|
||||||
|
|
||||||
onOver() {
|
onOver() {
|
||||||
if(!this.isActivated)
|
if(!this.isActivated)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
this.tint = 0xffaaaa;
|
||||||
}
|
}
|
||||||
|
|
||||||
onDown() {
|
onDown() {
|
||||||
if(!this.isActivated)
|
if(!this.isActivated)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if(!this.isOnstage) {
|
if(this.isOnWaitingRoom) {
|
||||||
this.isOnstage = true;
|
this.isOnWaitingRoom = false;
|
||||||
this.goOnstage();
|
this.goOnstage();
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
this.onFired();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(typeof this.onFiredFunction !== "undefined")
|
|
||||||
this.onFiredFunction(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
setActive(isActivated) {
|
setActive(isActivated) {
|
||||||
let beforeState = this.isActivated;
|
|
||||||
this.isActivated = isActivated;
|
this.isActivated = isActivated;
|
||||||
|
|
||||||
if(beforeState && !this.isActivated) {
|
if(this.isActivated) {
|
||||||
this.shutdown();
|
this.tint = 0xffffff;
|
||||||
|
} else {
|
||||||
|
this.tint = 0x555555;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
shutdown() {
|
removeTween() {
|
||||||
if(typeof this.tweenScaleUp !== "undefined") {
|
if(typeof this.tweenScaleUp !== "undefined") {
|
||||||
this.tweenScaleUp.stop();
|
this.tweenScaleUp.stop();
|
||||||
this.tweenScaleUp = null;
|
this.tweenScaleUp = null;
|
||||||
@@ -70,11 +75,11 @@ class Alien extends Phaser.Sprite {
|
|||||||
this.tweenScaleDown = null;
|
this.tweenScaleDown = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.scale.set(0);
|
this.scale.set(Alien.ONSTAGE_MIN_SCALE);
|
||||||
}
|
}
|
||||||
|
|
||||||
goWaitingRoom() {
|
goWaitingRoom() {
|
||||||
this.isOnstage = false;
|
this.isOnWaitingRoom = true;
|
||||||
|
|
||||||
this.x = this.initialX;
|
this.x = this.initialX;
|
||||||
this.y = this.initialY;
|
this.y = this.initialY;
|
||||||
@@ -82,6 +87,20 @@ class Alien extends Phaser.Sprite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
goOnstage() {
|
goOnstage() {
|
||||||
|
this.goRandomPosition();
|
||||||
|
|
||||||
|
if(typeof this.onGoOnstageFunction !== "undefined")
|
||||||
|
this.onGoOnstageFunction();
|
||||||
|
}
|
||||||
|
|
||||||
|
onFired() {
|
||||||
|
this.goRandomPosition();
|
||||||
|
|
||||||
|
if(typeof this.onFiredFunction !== "undefined")
|
||||||
|
this.onFiredFunction();
|
||||||
|
}
|
||||||
|
|
||||||
|
goRandomPosition() {
|
||||||
// set random position
|
// set random position
|
||||||
const SPAWN_BOX_LEFT = 80;
|
const SPAWN_BOX_LEFT = 80;
|
||||||
const SPAWN_BOX_WIDTH = game.world.width - 160;
|
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);
|
// .drawRect(SPAWN_BOX_LEFT, SPAWN_BOX_TOP, SPAWN_BOX_WIDTH, SPAWN_BOX_HEIGHT);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
this.tint = 0xffffff;
|
||||||
|
|
||||||
// start scale tween animation
|
// start scale tween animation
|
||||||
this.shutdown();
|
this.removeTween();
|
||||||
|
|
||||||
this.tweenScaleUp = game.add.tween(this.scale)
|
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);
|
.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;
|
return;
|
||||||
|
|
||||||
if(typeof this.onEscapedFunction !== "undefined")
|
if(typeof this.onEscapedFunction !== "undefined")
|
||||||
this.onEscapedFunction(this);
|
this.onEscapedFunction();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Alien.DEFAULT_SPEED_SEC = 3000;
|
Alien.DEFAULT_SPEED_SEC = 3000;
|
||||||
Alien.WAITING_SCALE = 2;
|
Alien.WAITING_SCALE = 2;
|
||||||
|
Alien.ONSTAGE_MIN_SCALE = 0.1;
|
||||||
Alien.ONSTAGE_MAX_SCALE = 3;
|
Alien.ONSTAGE_MAX_SCALE = 3;
|
||||||
|
|||||||
@@ -21,18 +21,17 @@ class Game {
|
|||||||
|
|
||||||
|
|
||||||
// contents
|
// contents
|
||||||
this.alienGroup = game.add.group();
|
this.activatedAlienIndex = 0;
|
||||||
this.alienGroup.inputEnableChildren = true;
|
this.aliens = [];
|
||||||
for(let i = 0; i < 10; i++) {
|
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.goWaitingRoom();
|
||||||
alien.setActive(false);
|
alien.setActive(false);
|
||||||
this.alienGroup.add(alien);
|
this.aliens.push(alien);
|
||||||
}
|
}
|
||||||
|
|
||||||
// activate first alien
|
|
||||||
// this.alienGroup[0].setActive(true);
|
|
||||||
|
|
||||||
|
|
||||||
// bottom
|
// bottom
|
||||||
let screenBottom = new ScreenBottom(game);
|
let screenBottom = new ScreenBottom(game);
|
||||||
@@ -78,15 +77,23 @@ class Game {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
startGame() {
|
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");
|
console.log("onAlienFired");
|
||||||
sprite.goOnstage();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onAlienEscaped(sprite) {
|
onAlienEscaped() {
|
||||||
console.log("onAlienEscaped");
|
console.log("onAlienEscaped");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user