BrunoHautenfaust Posted November 8, 2015 Share Posted November 8, 2015 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 More sharing options...
frenetikm Posted November 8, 2015 Share Posted November 8, 2015 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; } }, BrunoHautenfaust 1 Link to comment Share on other sites More sharing options...
BrunoHautenfaust Posted November 8, 2015 Author Share Posted November 8, 2015 It works! At first I ommited obstacle.kill(); because I was following my comment on the side. But then I included it.Thanks! Link to comment Share on other sites More sharing options...
Skeptron Posted November 9, 2015 Share Posted November 9, 2015 Mark the post as answered please if you're happy with the solution Link to comment Share on other sites More sharing options...
BrunoHautenfaust Posted November 9, 2015 Author Share Posted November 9, 2015 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 More sharing options...
Skeptron Posted November 9, 2015 Share Posted November 9, 2015 For killing a sprite out of world bound you don't even need to code anything, Phaser can handle it : http://phaser.io/docs/2.3.0/Phaser.Sprite.html#outOfBoundsKill Just set sprite.outOfBoundsKill = true; Link to comment Share on other sites More sharing options...
BrunoHautenfaust Posted November 9, 2015 Author Share Posted November 9, 2015 I know about that. I have it in addObstacle().But if I set the counter to ++ after 1 obstacle is behind the player, AND kill the obstacle when it's out of bounds, the counter does not equal 1 but 10 or 25, etc. Link to comment Share on other sites More sharing options...
Skeptron Posted November 9, 2015 Share Posted November 9, 2015 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 More sharing options...
BrunoHautenfaust Posted November 9, 2015 Author Share Posted November 9, 2015 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 More sharing options...
Skeptron Posted November 9, 2015 Share Posted November 9, 2015 No. passedObstacle: function(obstacle) { if (obstacle.grantPoint && obstacle.x < player.x) { obstacle.grantPoint = false; pass += 1; passText.text = 'pass: ' + pass; } }Don't forget to initialize obstacle.grantPoint to true. BrunoHautenfaust 1 Link to comment Share on other sites More sharing options...
BrunoHautenfaust Posted November 9, 2015 Author Share Posted November 9, 2015 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 More sharing options...
Recommended Posts