1

i want to make a flappy bird game, and i want to know how to make a timer that starts when the player presses the screen, the bird (or squid in my case) will start to go up and when the timer ends it will start to go down again.

here is the code

batch.begin();
        //ignore this part
        batch.draw(Assets.sprite_back,BackX ,0);
        batch.draw(Assets.sprite_back2,BackX2 ,0);
        //this is the squid
        batch.draw(Assets.sprite_squiddy,10 ,squiddyY);
batch.end();

}

public void generalUpdate(){
    //ignore
    BackX -= 1;
    BackX2 -= 1;
    //look at this
    squiddyY -=4;
    //continue ignoring
    if(BackX<=-480){
        BackX = 480;
    }
    if(BackX2<=-480){
        BackX2 = 480;
    }
    //this is the part we need
    if(Gdx.input.justTouched()){
        squiddyY += 100;

    }
}

so actually now if you press it goes automatically up and i want him to go there smoothly

i hope i explained myself good enough.

oh yea and if there is a better way to do it please tell me

Alon Kagan
  • 69
  • 9

3 Answers3

0

Here are several timer tutorials. You can use that idea and bind start stop on the events in your game.

http://examples.javacodegeeks.com/android/core/os/handler/android-timer-example/

http://www.theappguruz.com/blog/android-count-timer/

Android - Creating a Timer loop for a pong game

Community
  • 1
  • 1
0

This may not be the answer you want but it maybe what you need.

If you are making Flappy Bird, I would discourage the use of timer. It can be done in easier way by using Gravity, i.e by adding Velocity and Acceleration.

velocityY = 0;
accelerationY = -100;
public void generalUpdate(){
//ignore
BackX -= 1;
BackX2 -= 1;
//continue ignoring
if(BackX<=-480){
    BackX = 480;
}
if(BackX2<=-480){
    BackX2 = 480;
}
//this is the part we need

velocityY += accelerationY;
squiddyY  += velocityY;
if(Gdx.input.justTouched()){
    velocityY += 100;

}

This would give you the exact "falling down" and "jump" effect you need.

Zohaib Amir
  • 3,482
  • 2
  • 11
  • 29
0

here is a way to create a timer

private long time = 0;

In your update() sector

if(TimeUtils.nanoTime() - time >= TIME)
{
spawn();
time = TimeUtils.nanoTime();
}

In your spawn() create your spawning mechanism

Hope this helps and enjoy :)

Fish
  • 1,689
  • 1
  • 18
  • 28