Skip to content

Instantly share code, notes, and snippets.

@pctroll
Created August 27, 2017 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pctroll/b0a67b77abd033bcef1b1c64344e9d24 to your computer and use it in GitHub Desktop.
Save pctroll/b0a67b77abd033bcef1b1c64344e9d24 to your computer and use it in GitHub Desktop.
TimerHandler = function (timerHandlerData, context, timerSound) {
this.timerSound = timerSound;
this.data = timerHandlerData;
//Reference to the guide sprite on the scene
if (timerHandlerData.timerAtlas == "")
this.timerHolderSprite = context.add.sprite(timerHandlerData.timerX,timerHandlerData.timerY,timerHandlerData.timerHolderSpriteName);
else
this.timerHolderSprite = context.add.sprite(timerHandlerData.timerX, timerHandlerData.timerY, timerHandlerData.timerAtlas, timerHandlerData.timerHolderSpriteName);
//Reference to the guide sprite on the scene
if (timerHandlerData.timerAtlas == "")
this.timerSprite = context.add.sprite(0,0,timerHandlerData.timerSpriteName);
else
this.timerSprite = context.add.sprite(0,0, timerHandlerData.timerAtlas, timerHandlerData.timerSpriteName);
//Animations
var frames = Phaser.Animation.generateFrameNames('buildTheMachine_timer_', 1, 20, '.png', 4);
this.counter = this.timerSprite.animations.add('countDown', frames, 1, false, false);
var style = { font: "36px Arial", fill: "#ffffff", align: "center", fontWeight : "bold"};
this.timerText = context.add.text(185, 80, "", style);
this.timerHolderSprite.addChild(this.timerSprite);
this.timerHolderSprite.addChild(this.timerText);
this.loop = null;
};
TimerHandler.prototype= {
start : function (initialTime, context) {
this.counter.delay = initialTime / 20 * 1000;
if(this.loop != null)
context.time.events.remove(this.loop);
this.timerText.setText(initialTime);
if(this.counter.isPlaying){
this.counter.restart();
}else{
this.counter.play();
}
this.loop = context.time.events.loop(1000, function(){
if(this.timerSound != null)
this.timerSound.play();
initialTime--;
if(initialTime < 0) initialTime = 0;
this.timerText.setText(initialTime);
}, this);
},
stop: function(context) {
if(this.loop != null)
context.time.events.remove(this.loop);
this.counter.restart();
this.counter.stop();
this.timerText.setText("");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment