frokenstein Posted November 16, 2014 Share Posted November 16, 2014 It seems my kill sprite callback function has no access to any scope. I am trying to update a var and I cannot do it from within the callback this.killAsteroid(). I start with this:asteroid.events.onOutOfBounds.add(this.killAsteroid, asteroid);My kill function is like so:killAsteroid: function(asteroid) {// Kill asteroid that is out of bounds or has collided with somethingasteroid.kill();console.log("asteroid killed");this.asteroidCount--;console.log("Asteroid count after kill: " + this.asteroidCount);}I can kill the asteroid, apparently. But this.asteroidCount is NaN or undefined. I can access this variable from every other function in the app except here, and I presume it's because of scoping of this callback. How can I update this.asteroidCount from within this function? Even if I use this.game.asteroidCount or Starpatrol.Game.asteroidCount it also returns NaN. Link to comment Share on other sites More sharing options...
bluedot Posted November 16, 2014 Share Posted November 16, 2014 I think you have to do this,asteroid.events.onOutOfBounds.add(this.killAsteroid, asteroid, this); Link to comment Share on other sites More sharing options...
frokenstein Posted November 16, 2014 Author Share Posted November 16, 2014 Thanks. But I've tried that and it doesn't work, either. Link to comment Share on other sites More sharing options...
Sir_Everard Posted November 16, 2014 Share Posted November 16, 2014 var that = this;asteroid.events.onOutOfBounds.add(function(asteroid){// Kill asteroid that is out of bounds or has collided with somethingasteroid.kill();console.log("asteroid killed");that.asteroidCount--;console.log("Asteroid count after kill: " + that.asteroidCount);}, asteroid);var that = this;asteroid.events.onOutOfBounds.add(function(asteroid){that.killAsteroid(asteroid, that);}, asteroid);Two different ways to do this for you. frokenstein 1 Link to comment Share on other sites More sharing options...
frokenstein Posted November 16, 2014 Author Share Posted November 16, 2014 Thank you. I think that would work. I actually just solved it using a different methodology by tracking the sprite count using countLiving(). Link to comment Share on other sites More sharing options...
Recommended Posts