Hi I’m writing my first phaser game at the moment. It’s a kind of retro racer game. Anyway I have run in to a strange problem. I am trying to set the position of my car to become increasingly unstable. i am incrementing a var on each game tick and am trying to use this increasing value to set a random position for the car.
However my function only sets the vars position the first time it is run. every subsequent time I can see that the function is called and that the incremebted value is being applied to the cars position, however, the value seems to be being ignored or reset.
I cannot see any place where the position of the car might be being reset in the rest of my code. here are the relavant functions within my update function. Is there some reason this snippet would only be applied once?
mainTick: function(){
// This functionhouses my timer event. it is called once on game start and then calls itself
game.time.events.repeat(Phaser.Timer.SECOND * 1, 1, this.mainTick, this);
this.gameTick();
},
gameTick: function() {
clockText.setText(currentTemp);
currentTemp += 10;
gameTime += 1;
console.log("before heat added car.body.x= "+car.body.x);// whatever gets added by the heat function, this var reverts to the same value. only the first change is accepted.
this.heat(currentTemp);
console.log("after heat added car.body.x= "+car.body.x);
},
heat: function(t){
var entropy = t * Math.random().toFixed();
car.body.x += t;
entropy = t * Math.random().toFixed();
car.body.y += entropy;
},
@donnyBronson I believe you should have more information about update method of Phaser.
update method getting called every frame, which means (lets say 60fps), it’s called 60 times in a second.
So normally we don’t create variables in update instead we use create method.
I think you should probably start a video tutorial (a basic one), understand the logic and then start making ur game.
I don’t think you want to call time.events.repeat() recursively, because each call will create a new repeating event. You would either call time.events.add() recursively or call time.events.repeat() only once, at the start.
var entropy = t * Math.random().toFixed();
I think the problem is this line, which assigns either “0” or “1”.
Hi Samme
thanks for the tip on th difference between …repeat() and … add() I’ll definatly incorporate that change.
unfortunatly that entropy var is not the problem. I can get rid of the randomisation but and set that var with any integer and i still the same problem. this first itteration is set but then somehow the further changes never get made. the car.body.x is not updated again.
Thanks for the input here.
The moral support really helps