Jump to content

Howto run more than one timer at the same time


csimpson.web
 Share

Recommended Posts

Hi guys im building a whack the rabbit style game , where i have an overall game timer , and then i need to run individual timers on each hole (10 in total) .. 

 

Hit a problem trying to run them all from game.time.create, obviously its overiding the other timers. 

 

My question is , how do i run a master game timer , and individual game timers alongside .. 

 

 

Link to comment
Share on other sites

thats what i thought , and i am using that example .. what am i missing here ??

var playState = {    create: function() {        //Load the background image        this.background = game.add.sprite(0, 0, 'background');        this.background.anchor.x = 0;        this.background.anchor.y = 0;        this.doorsClosed = game.add.sprite(110, 120, 'doorsClosed');        this.doorsClosed.inputEnabled = true;        this.doorsClosed.events.onInputDown.add(this.doorsClosedListener, this);        this.doorsClosed.visible = false;        this.doorsHalfOpen = game.add.sprite(110, 120, 'doorsHalfOpen');        this.doorsHalfOpen.inputEnabled = true;        this.doorsHalfOpen.events.onInputDown.add(this.doorsHalfOpenListener, this);        this.fireStateOne = game.add.sprite(90, 120, 'fireStateOne');        this.doorsHalfOpen.visible = false;        this.fireStateOne.visible = false;        this.doorsOpen = game.add.sprite(35, 120, 'doorsOpen');        this.doorsOpen.inputEnabled = true;        this.doorsOpen.events.onInputDown.add(this.doorsOpenListener, this);        this.fireStateTwo = game.add.sprite(90, 120, 'fireStateTwo');        this.doorsOpen.visible = false;        this.fireStateTwo.visible = false;        this.ovenTimers();        this.masterTimer();            },    update: function() {        //this.holeTimer();    },    // -================- create: function() -================-    masterTimer: function() {		//Create the overall game timer    	this.masterTimer = game.time.create(false);    	//  Set a TimerEvent to occur after 2 seconds    	this.masterTimer.loop(1000, this.updateTimer, this);    	//Set the label to show the time remaining    	this.timeLabel = game.add.text(game.width-240, 20, 'Time Left: '+game.global.masterTimer+'s', { font: '30px Arial', fill: '#ffffff' });		this.timeLabel.anchor.setTo(0, 0);		this.masterTimer.start();		//console.info(masterTimer);    },        ovenTimers: function() {        // Create the oven timer        this.oven1timer = game.time.create(false);        // Set a TimerEvent to occur after 2 seconds        this.oven1timer.loop(1000, this.updateOvenTimer, this);        // Set the label to show the time remaining        this.timeLabel = game.add.text(game.width - 1870, 60, 'Time Left: ' + game.global.oven1timer + 's', {            font: '26px Arial',            fill: '#ffffff'        });        this.timeLabel.anchor.setTo(0, 0);        this.oven1timer.start();    },	    /*    loadDoorState: function(doorState) {        switch (doorState) {            case 1:                this.doorsClosed.visible = true;                break;            case 2:                this.doorsClosed.visible = false;                this.doorsHalfOpen.visible = true;                this.fireStateOne.visible = true;                break;            case 3:                this.doorsHalfOpen.visible = false;                this.doorsOpen.visible = true;                this.fireStateOne.visible = false;                this.fireStateTwo.visible = true;                break;            default:                console.warn('no state defined');                break;        }    },    holeTimer: function() {        if (game.global.timer < 3) {            game.global.doorState = 3;            this.loadDoorState(game.global.doorState);            //console.log(game.global.doorState);        } else if (game.global.timer > 5) {            game.global.doorState = 1;            this.loadDoorState(game.global.doorState);            //console.log(game.global.doorState);        } else {            game.global.doorState = 2;            this.loadDoorState(game.global.doorState);            //console.log(game.global.doorState);        }    },    */    updateTimer: function() {        game.global.masterTimer = game.global.masterTimer - 1;        if (game.global.masterTimer < 0) {            //Flag game as complete            game.global.gameEnded = true;            this.masterTimer.stop();            // Name of the game            var nameLabel = game.add.text(game.world.centerX, 480, 'Oh No!', {                font: '60px Arial',                fill: '#ffffff'            });            nameLabel.anchor.setTo(0.5, 0.5);            this.playLabel = game.add.text(game.world.centerX, 680, 'The ovens have lost optimum temperature!', {                font: '30px Arial',                fill: '#ffffff'            });            this.playLabel.anchor.setTo(0.5, 0.5);            this.loadDoorState(game.global.doorState);            setTimeout(function() {                game.state.start('menu');            }, 3000);        } else {            this.timeLabel.setText('Time Left: ' + game.global.masterTimer + 's');        }    },    updateOvenTimer: function() {        game.global.oven1timer = game.global.oven1timer - 1;        if (game.global.oven1timer < 0) {            //Flag game as complete            game.global.gameEnded = true;            this.oven1timer.stop();            // Name of the game            var nameLabel = game.add.text(game.world.centerX, 480, 'Oh No!', {                font: '60px Arial',                fill: '#ffffff'            });            nameLabel.anchor.setTo(0.5, 0.5);            this.playLabel = game.add.text(game.world.centerX, 680, 'The ovens have lost optimum temperature!', {                font: '30px Arial',                fill: '#ffffff'            });            this.playLabel.anchor.setTo(0.5, 0.5);            this.loadDoorState(game.global.doorState);            setTimeout(function() {                game.state.start('menu');            }, 3000);        } else {            this.timeLabel.setText('Time Left: ' + game.global.oven1timer + 's');        }    },    doorsClosedListener: function() {        alert('closed clicked');    },    doorsHalfOpenListener: function() {        //alert('Half open clicked');        console.log('this should reset the timer');        game.global.oven1timer = 10;        game.global.doorState = 1;        this.loadDoorState(game.global.doorState);        this.doorsHalfOpen.visible = false;        this.fireStateOne.visible = false;    },    doorsOpenListener: function() {        //alert('Open clicked');        game.global.oven1timer = 4;        game.global.doorState = 2;        this.loadDoorState(game.global.doorState);        this.doorsHalfOpen.visible = true;        this.fireStateTwo.visible = false;        this.doorsOpen.visible = false;        this.fireStateOne.visible = true;    },};
Link to comment
Share on other sites

Hmm, I'm not sure. Is there even any reason to use separate timers for this? Can you not just use TimerEvents on a single timer or even just store game.time.now + the time until the next event and poll that? The game does have a master timer already instantiated by default referenced at game.time.events. Your method looks rather elaborate and I'm not entirely sure from your code what you're trying to do.

Link to comment
Share on other sites

Knowing me ive already overly complicated in my head. 

 

its basically 10 ovens , that need to blow open in 3 stages each stage has differnt bits in ... ive got it running so that the ovens do what they need to , i just need to run them from individual timers to operate each oven , the idea of the game is you get 2 minutes to keep the ovens temperature hot by prressing them

 

atm one timer is overwriting the other, i need 11 timers basically that i can run 10 for the ovens ... and 1 master overall game timer..

 

would it help if i put the game on github to give a context to see what im trying to do ?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...