Jump to content

Destroy Timer Loop?


RyanTheBoy
 Share

Recommended Posts

EDIT: Also, if anyone knows of a way to preserve indentation when copy-pasting that would be great. 

 

EDIT2: Perhaps the best question to ask is what are the methods associated with the TimeManager at this.game.time? 

 

Howdy folks! Have another issue and wondered if anyone knew what I was doing wrong.

 

I've got my main.js file orchestrating all of my prototyped classes which is working without issue except for the timer. When I play through the game(an infinite-runner in the style of Canabalt or BIT.TRIP RUNNER) everything goes well up to and including my eventual death. 

 

The problem occurs when I hit the restart button on the 'Game Over' screen. The game starts fine enough but the timer moves twice as quickly. If I try to replay a third time it goes three times as fast, you get the idea.

 

I assume that first timer is staying initiated from the initial playthrough and subsequent playthroughs just add additional timer loops to the game. When I attempt to call TIMER.destroy() in my endGame() function, I get this error...

 

Uncaught TypeError: Object [object Object] has no method 'destroy'

 

Here is the code from my main method. I can post the other classes if need be(and in fact I think it may behoove me to move the timer to my Level.js class for sanitiy's sake). 

/* MAIN.JS/**********//* Contains all state definitions,/* global variables, and the /* main Phaser.Game variable./**********/ // General Globalsvar game = new Phaser.Game(800, 600, Phaser.AUTO, 'gameScreen');var SPEED = 3.5;var SPEED_MAX = 10.5;var DIST = 0;var DIST_MAX = 600;var SCORE = 0;var TIME = 60;var TIMER = null;var CENTER = new Phaser.Point(0.5, 0.5); // Game state DefinitionsStates = {}; States.MainMenu = function() {} // MainMenu State Globalsvar mainMenu = new MainMenu(game); States.MainMenu.prototype = {preload: function() {mainMenu.preload();},create: function() {mainMenu.create();}}; States.GamePlay = function() {this.timer = null;} // GamePlay State Globalsvar level = new Level(game);var player = new Player(game);var hud = new HUD(game); States.GamePlay.prototype = {preload: function() {level.preload();player.preload();}, create: function() {level.create();player.create();hud.create();}, update: function() {level.update();player.update();hud.update();}}; States.GameOver = function() {} // MainMenu State Globalsvar gameOver = new GameOver(game); States.GameOver.prototype = {preload: function() {gameOver.preload();},create: function() {gameOver.create();}}; game.state.add('GamePlay', States.GamePlay);game.state.add('MainMenu', States.MainMenu);game.state.add('GameOver', States.GameOver);game.state.start('MainMenu'); // Timer function for timed events. function tickLoop() {TIME--;SCORE += Math.floor(SPEED);if (level.speedUp) level.speedUp--;} // Global Functionsfunction startGame() {// Reinitialize Global VariablesSCORE = 0;TIME = 60;SPEED = 3.5;DIST = 0;if (!TIMER) TIMER = game.time.events.loop(Phaser.Timer.SECOND, tickLoop, this); game.state.start('GamePlay');} function endGame() {TIMER.destroy();game.state.start('GameOver');} function moveIt(s) {var speed = SPEED; if (level.speedUp) {speed += 2;}  if (level.speedDown) {speed -= level.speedDown * 0.5;} s.x -= speed;} function rotateIt(s) {s.angle += 10;}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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