class Alien extends Phaser.Sprite { constructor(initialX, initialY, onFiredFunction, onEscapedFunction) { super(); this.isActivated = false; this.isOnstage = false; this.speedGrowing = Alien.DEFAULT_SPEED_SEC; this.initialX = initialX; this.initialY = initialY; this.onFiredFunction = onFiredFunction; this.onEscapedFunction = onEscapedFunction; Phaser.Sprite.call(this, game, this.initialX, this.initialY, 'alien_normal'); this.anchor.set(0.5); // this.scale.set(Alien.MAX_SCALE); this.setOnInitialPosition(); this.inputEnabled = true; this.events.onInputOut.add(this.onOut, this); this.events.onInputOver.add(this.onOver, this); this.events.onInputDown.add(this.onDown, this); game.add.existing(this); } onOut() { if(!this.isActivated) return; } onOver() { if(!this.isActivated) return; } onDown() { if(!this.isActivated) return; if(typeof this.onFiredFunction !== "undefined") this.onFiredFunction(this); } setActive(isActivated) { let beforeState = this.isActivated; this.isActivated = isActivated; if(beforeState && !this.isActivated) { this.shutdown(); } } shutdown() { if(typeof this.tweenScaleUp !== "undefined") { this.tweenScaleUp.stop(); this.tweenScaleUp = null; } if(typeof this.tweenScaleDown !== "undefined") { this.tweenScaleDown.stop(); this.tweenScaleDown = null; } this.scale.set(0); } setOnInitialPosition() { this.x = this.initialX; this.y = this.initialY; this.scale.set(Alien.MAX_SCALE); } goOnstage() { // set random position let SPAWN_BOX_LEFT = 100; let SPAWN_BOX_WIDTH = game.world.width - 200; let SPAWN_BOX_TOP = 300; let SPAWN_BOX_HEIGHT = game.world.height - 400; this.x = SPAWN_BOX_LEFT + Math.random() * SPAWN_BOX_WIDTH; this.y = SPAWN_BOX_TOP + Math.random() * SPAWN_BOX_HEIGHT; // start scale tween animation this.shutdown(); this.tweenScaleUp = game.add.tween(this.scale) .to( { x: Alien.MAX_SCALE, y: Alien.MAX_SCALE }, Alien.DEFAULT_SPEED_SEC, Phaser.Easing.Linear.None, false); this.tweenScaleDown = game.add.tween(this.scale) .to( { x: 0, y: 0 }, Alien.DEFAULT_SPEED_SEC, Phaser.Easing.Linear.None, false); this.tweenScaleUp.chain(this.tweenScaleDown); this.tweenScaleDown.onComplete.addOnce(this.onEscaped, this); this.tweenScaleUp.start(); } onEscaped() { if(!this.isActivated) return; if(typeof this.onEscapedFunction !== "undefined") this.onEscapedFunction(this); } } Alien.DEFAULT_SPEED_SEC = 3000; Alien.MAX_SCALE = 3;