Julia Posted November 21, 2014 Share Posted November 21, 2014 Hey, I've added a 'complete' state to my game.Here is a playable example: juliahofs.nl/platformer3/platformer_test.html You have to pick up all 10 coins to be able to see the problem. When you see the text, just click on the screen and continue.I've put this piece of code in it: Platformer.Complete = function (game) {};Platformer.Complete.prototype = {create: function () {var wonImage = game.add.sprite(game.world.centerX, 200, 'IWon');wonImage.anchor.setTo(0.5, 0.5);var restartText = game.add.text(game.world.centerX, 500, "START", {font: "35px Roboto", fill: "#000", align: "center"});restartText.anchor.setTo(0.5, 0.5);restartText.inputEnabled = true;restartText.events.onInputDown.add(function() {game.state.start('Play');}, this);}};When I click the start text, the game only flickers. It doesn't restart the game. I'm guessing it has something to do with the onInputDown event? Or should I destroy the image and the text of the complete state (I thought this was done automatically when changing states).Any tips? Link to comment Share on other sites More sharing options...
Mariusz Posted November 21, 2014 Share Posted November 21, 2014 In your code, you declare your variable score implicitly, which makes it a global variable. Because you don't reset that variable back to zero in the create() function of the play state, it goes straight into the complete state. Link to comment Share on other sites More sharing options...
Julia Posted November 21, 2014 Author Share Posted November 21, 2014 Thanks for you answer, Mariusz. That seems to make sense. But how would I return the score back to zero? I tried this: create: function(){ score = 0; score = game.add.text(20 , 20, "0 / 10", fontStyle); score.fixedToCamera = true;},updateText: function() { score.setText(count + " / 10");},But still the same problem... Or did you mean the 'count' variable? Link to comment Share on other sites More sharing options...
Julia Posted November 21, 2014 Author Share Posted November 21, 2014 Ah yes, that was indeed the problem Now I have this: create: function () { count = 0; score = game.add.text(20 , 20, "0 / 10", fontStyle); score.fixedToCamera = true;},updateText: function() { score.setText(count + " / 10");},Thanks a lot!! Link to comment Share on other sites More sharing options...
Recommended Posts