Jump to content

Counter updating like crazy


BrunoHautenfaust
 Share

Recommended Posts

I saw a few examples and games with implemented simple score logic. Seemed easy enough. But why is my counter updating like crazy? By each passed obstacle it jumps to 20 or so. Is it because the game loop calls each function 60 times per second. If so, what should I do?

 

I want the counter to itterate by 1 each time an obstacle is passed(behind the player or out of bounds). The game is an endless runner type.

 

My code:



update{
obstacles.forEachAlive(this.passedObstacle, this);
},
passedObstacle: function(){
var obstacle = obstacles.getFirstAlive();
if (obstacle.x < player.x) {
obstacle.kill(); // with or without - same thing
pass += 1;
passText.text = 'pass: ' + pass;
}
},

// Tried with (obstacle.x < game.world.bounds.left)
// AND
// (obstacle.x + obstacle.width < game.world.bounds.left)


Link to comment
Share on other sites

in your code you call passedObstacle for each of your obstacles, but in the function you get only the first alive.

so if you have 20 obstacles, in passedObstacle you test 20 times the first alive.

 

you can test each obstacle by parameter in the callback function like this:

passedObstacle: function(obstacle){        /* not necessary        var obstacle = obstacles.getFirstAlive();        */        if (obstacle.x < player.x) {            obstacle.kill(); // with or without - same thing            pass += 1;            passText.text = 'pass: ' + pass;        }    },
Link to comment
Share on other sites

Yes, Skeptron, I was going to but today I saw a problem. The solution was not 100% what I thought I had in mind. So I tried to fix it before I write again. But I couldn't. 

passedObstacle: function(obstacle) {   // This kills the obstacle when it's behind the player        if (obstacle.x < player.x) {            pass += 1;            obstacle.kill();            passText.text = 'pass: ' + pass;        }},
passedObstacle: function(obstacle){    // Counter updates by too much.        if (obstacle.x < player.x) {            pass += 1;            if (obstacle.x < game.world.x - o.width) {            obstacle.kill();                }            passText.text = 'pass: ' + pass;        }},
 
 The problem with this is that it kills(removes from screen) the obstacle right after it gets passed the player. The player doesn't phase out of the screen gradually. The counter should itterate by 1 when the obstacle is behind the player AND kill the obstacle when it's out of bounds.
Link to comment
Share on other sites

Well, you "know" that but you still write obstacle.kill() in both your methods nonetheless.

 

I think your increment goes crazy because there are a lot of update() loops where the obstacle is behind your player (even though I can just assume because I don't know from your snippets when/how the passedObstacle function is called). So as long as the object exists (and maybe even if it's killed), the loop will successfully run and increment the score. (Destroy() might be better, think about it).

 

You need to keep a track of the obstacles to increment the score by one per obstacle : that is, have an array somewhere and put IDs on the obstacle objects and remember which ones have incremented the counter, so that they never do it again.

 

Or, simpler solution, put a new property on obstacle - like "grantsPoints", set to true -, and set it to false once it has granted a point to the player. Check that property before assigning any point, so that "dead" obstacles don't provide points to the player anymore.

Link to comment
Share on other sites

Yeah, I have kill() in both methods. I keep it in passedObstacle() just to get the point logic going. Here's my addObstacle(). It works just fine!

addObstacle: function() {        var obstacle = obstacles.getFirstDead();                if (obstacle) {            obstacle.reset(game.world.width, game.world.height-86);            obstacle.body.velocity.x = -200;            obstacle.body.immovable = true;        }         obstacle.checkWorldBounds = true;         obstacle.outOfBoundsKill = true;    },

And passedObstacle(), following your last solution idea, if I got it right:

  passedObstacle: function(obstacle) {        // obstacle.alive isn't needed. But just for the sake of clarity, I put it there.        if (obstacle.alive && obstacle.x < player.x) {              obstacle.grantPoint = true;            pass += 1;            passText.text = 'pass: ' + pass;        }        obstacle.grantPoint = false;    },

How it looks in update():

 update() {     mainState.addObstacle();     obstacles.forEachAlive(this.passedObstacle, this);}

However, the counter still adds (a lot) more than 1.

Link to comment
Share on other sites

It's working! Incredible! And comparing your version to mine I saw where my mistake was.
   (obstacle.alive && obstacle.x < player.x) {... obstacle.grantPoint = true; ...}

passes a lot of loop counts where obstacle.grantPoint = true;

And that's why the counter was inaccurate.

While having obstacle.grantPoint in the if statement provides a more accurate checking.

 

Thank you! :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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