[SOLVED] How to create obstacle in game like TempleRun?

Hi guys, I have another problem now.:disappointed_relieved:

I want to create a game like Temple Run and I also read the source code of CubeField.java. But I can not create obstacle just appear at the road (only three way player can run, and I want the obstacle just appear at the way that player will run and not too dense).

Screenshot:


Suppose the blue cube is obstacle and jaime is the player.

I tried to use Timer to delay the obstacle appear, so it will not be too dense but it did not work.

Does anyone can help me ? I will very thankful.

We would need to see more of your code to know where the problem is exactly.

However, If I had to take a guess based on the console output in your screenshot, it probably has to do with the code you’re using for your Timer, seeing as the output value being printed out for your timer is not a float.

The correct way to make a timer with JME is to accumulate the tpf float value in the update loop every frame. Then that value can be referenced to find out how much time has passed since you began accumulating the value.

private int barrierTimer = 0;

public void upate(float tpf){
     barrierTimer += tpf;

     if(barrierTimer > 0.5f){ 
        //code to place your barriers here every 0.5 seconds

       //reset timer
       barrierTimer = 0;
    } 
}

like post above, use TPF float value where 1 = 1 second. 0.5f = half of second. etc…

I belive in your code you have microseconds, not seconds anyway. for things like this you use tpf(time per frame).

I like to subtract the period length itself from the accumulated time, rather than resetting to zero. I guess it feels better to start the next timer loop iteration with that little portion of last frame’s tpf that was over the period length, instead of discarding it.

that way, if barrierTimer is equal to 0.55f this frame, you begin next frame with 0.05f in the accumulated value (on the way back up to 0.5f).

if(barrierTimer > 0.5f){ 
        //code to place your barriers here every 0.5 seconds

       //rollover timer
       barrierTimer -= 0.5f;
    } 

It’s likely only a small difference in individual periodic timer loops, but in the back of my head I still think of the discarded leftover time as ominously accumulating somewhere.

If you keep track of the distance traveled, that could also be accumulated in a similar manner as time.

For very large tpfs (like what happens when you come back from a pause from lost focus), this code will run the barrierTimer block every frame until it catches up.

…which may or may not be desirable, but something to consider.

Personally, I completely avoid JME’s tpf and keep my own (in double)… but the concept is sound either way.

2 Likes

Thanks you for everyone who is reply me. Have a nice Christmas ! :smiley:

1 Like