Repeated function only takes effect once

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;
},

this is the part where ur car’s position change, which calling in every second.

But any case could you share also update call, andwhere are u calling mainTick at first time ?

Or a fiddle would be usefull in this case i believe.

Hey thanks for your time here.
Here is the whole update function

update: function() {
var hitPlatform = game.physics.arcade.collide(car, platforms);
platforms.alpha = 0;
hole.animations.play('fizz', true);
cursors.up.onDown.add(this.playFx);
cursors.down.onDown.add(this.playFx);
  if (!stuck) {// a bool to check if we have hit something
    if (gameLive == false) {// a bool to check if the game is running or not
      car.scale.setTo(2, 2);
      car.alpha = 1;
      game.add.tween(car.scale).to({
        x: 1,
        y: 1
      }, 2000, Phaser.Easing.Bounce.Out, true);
      gameLive = true;
      this.mainTick();

    }
//SIDEWAYS
    if (cursors.left.isDown) {
      console.log("left");
     if (!leftTog) {
         car.body.x -= drift;
        leftTog=true;
     }
    } else {
      leftTog=false;
    }
    if (cursors.right.isDown) {
     if (!rightTog) {
        car.body.x += drift;
        rightTog=true;
     }
    } else  {
      rightTog=false;
    }
    //UP DOWN

    if (cursors.up.isDown) {
      console.log("frame= "+car.frame);
     if (!upTog) {
        this.moveIt(1);
        if(frameNo<=carLoopLength-1){
          frameNo+=1;
          car.frame = frameNo;
        }else{
          frameNo=1;
          car.frame = frameNo;
        }
        upTog=true;
     }

  }else{
    upTog=false;
  }
    //
    if (cursors.down.isDown) {

     if (!downTog) {
        this.moveIt(-1);
        downTog=true;
        if(frameNo>=2){
          frameNo-=1;
          car.frame = frameNo;
        }else{
          frameNo=carLoopLength;
          car.frame = frameNo;
        }
     }
   }  else {
     downTog = false;
   }
  } // end stuck
if (hole.body.y >= game.world.height) {
  holeFull=false;
  hole.body.y = holeYstart;
  hole.body.x = holeRandomiser();
  currHole++;
  if (currHole == 5) {

    currHole = 0;
  }
  console.log("hole.body.x = " + hole.body.x);
}
//detect hole collision
game.physics.arcade.overlap(hole, car, crash, checkRespawnTime, this);

},

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

Thanks
I’ve actually already tried moving that var to the create function.
That doesnt seem to be my issue.
thanks anyway

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 :slight_smile:

OK, I think you can solve this by modifying the coordinates of the sprite instead of the body:

2 Likes

Ah ha!

Fantastic, yeah that seems to work now.
Thanks so much Samme