Hi, I'm creating this simple game, where on clicking a button, bunch of nuts start moving up and down.
To achieve this, in the wade editor, on clicking the button I'm calling a function startGame() which I have written in the App function as this.startGame().... and on click I'm calling it with wade.app.startGame();
Now here in this function, to achieve the timer effect I'm using wade.setInterval and assigning it's return id to a variable called myGame. So the movement starts as expected but on clicking stop, the wade.clearInterval(myGame) does not seem to work. When I check the return value of clearInterval() it always shows false... But the interval id values match when I console.log it...
To get a better understanding of what I'm talking about, please look at my code snippet for the startGame() function
var gameFlag = false;
var direction = +10;
var myGame;
this.startGame = function() {
if(gameFlag === false) {
gameFlag = true;
wade.getSceneObject('button').getSpriteByName('start').setVisible(false);
wade.getSceneObject('button').getSpriteByName('stop').setVisible(true);
myGame = wade.setInterval(function(){
wade.getSceneObject('nut').setPosition(wade.getSceneObject('nut').getPosition().x,
wade.getSceneObject('nut').getPosition().y+(1*direction));
if(wade.getSceneObject('nut').getPosition().y > 150){
direction = -10;
}
if(wade.getSceneObject('nut').getPosition().y < 40){
direction = +10;
}
}, 50);
console.log(myGame);
}
else if(gameFlag === true) {
gameFlag = false;
console.log(myGame);
wade.clearInterval(myGame); //This is not working and returns false, does not stop animation
wade.getSceneObject('button').getSpriteByName('start').setVisible(true);
wade.getSceneObject('button').getSpriteByName('stop').setVisible(false);
}
};
As you can see I'm decalring myGame, direction and GameFlag outside the scope of startGame()
I also would like to know if there is a better way to run this gameLoop... maybe something in the update method?
I would like to clear the interval on clicking stop...The other code in the if runs though... my sceneobject's sprite is changed...
Please help...