Wade Clear Interval not working
prajwal.be.21

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...

All 3 Comments
krumza

hi, yea there is some bug i think

try:

1 variant:

use pure setInterval

App = function()
{

	this.init = function()
	{

		wade.loadScene('scene1.wsc', true, function()
        {
            //somewhere here button with onClick wade.app.startGame();
            wade.app.gameFlag = false;
            wade.app.direction = +10;
            wade.app.myGame;
            
            wade.app.startGame = function() {
              if(wade.app.gameFlag === false) {
                wade.app.gameFlag = true;
                wade.getSceneObject('button').getSpriteByName('start').setVisible(false);
                wade.getSceneObject('button').getSpriteByName('stop').setVisible(true);
                wade.app.myGame = setInterval(function(){
                  wade.getSceneObject('nut').setPosition(wade.getSceneObject('nut').getPosition().x, 
                  wade.getSceneObject('nut').getPosition().y+(1*wade.app.direction));
                  if(wade.getSceneObject('nut').getPosition().y > 150){
                    wade.app.direction = -10;
                  }
                  if(wade.getSceneObject('nut').getPosition().y < 40){
                    wade.app.direction = +10;
                  }
                }, 50);
              }
              else if(wade.app.gameFlag === true) {
                wade.app.gameFlag = false;
                clearInterval(wade.app.myGame); 
                wade.getSceneObject('button').getSpriteByName('start').setVisible(true);
                wade.getSceneObject('button').getSpriteByName('stop').setVisible(false);
              }
            };
            
        });
	};
};

2 variant:

use    

wade.clearAllTimeoutsAndIntervals() 

 instead 

wade.clearInterval(myGame);

 3 variant

in button onClick use

//button onClick

if(wade.app.gameFlag === false) {
    wade.app.gameFlag = true;
    wade.resumeSimulation();
    wade.getSceneObject('button').getSpriteByName('start').setVisible(false);
    wade.getSceneObject('button').getSpriteByName('stop').setVisible(true);
  }
  else if(wade.app.gameFlag === true) {
    wade.app.gameFlag = false;
    wade.pauseSimulation();
    wade.getSceneObject('button').getSpriteByName('start').setVisible(true);
    wade.getSceneObject('button').getSpriteByName('stop').setVisible(false);
  }

 

all the options work

Gio

You are right, there is a bug there. I'm fixing it for version 3.7 of WADE (to be released a few weeks from now).

In the meantime, you can include this code in one of your scripts, to override the current definition of wade.clearInterval with something that works:

wade.clearInterval = function(intervalUid)
{
    for (var i=0; i<timeouts[i]; i++)
    {
        if (timeouts[i].uid == intervalUid && timeouts[i].repeat)
        {
            wade.removeObjectFromArrayByIndex(i, timeouts);
            return true;
        }
    }
    return false;
};

Sorry for the inconvenience

prajwal.be.21

Hey krumza and Gio... Thanks for the help... I used krumza's first solution and it's working a-ok...Really appreciate the help guys :) 

Post a reply
Add Attachment
Submit Reply
Login to Reply