1

I´m making an Android game (just to learn some stuff). You collect objects and you get points for that. When player reaches for example 200 points, the objects start coming faster, the background changes etc...It is like a next level. But when you reach those 200 points, I would like to show an image or text on the screen (something like "2 level") for like 2 seconds but I don´t know how.

I tried to work with the timer but I failed.

I´ve got an "if" statement

if (score >= 200) {
    frameLayout.setBackgroundResource(R.drawable.lvl2);  // background change

    // Make objects go faster
    collect_obj1 = Math.round(screenWidth / 57F); 
    collect_obj2 = Math.round(screenWidth / 33F);  
    critical_obj = Math.round(screenWidth / 42F); // If you hit this one = Game Over

    characterLvl1.setImageResource(R.drawable.characterLvl2);  // character change
}
Paula
  • 37
  • 5
  • Take a look at this https://stackoverflow.com/q/23838071/3852459 – vcmkrtchyan Jul 06 '19 at 15:34
  • Possible duplicate of [How to hide a ImageView and show it after 5 second](https://stackoverflow.com/questions/23838071/how-to-hide-a-imageview-and-show-it-after-5-second) – Gaurav Mall Jul 06 '19 at 15:40

1 Answers1

-1

Try this:

 long start_time = System.currentTimeMillis();
 long current_time = System.currentTimeMillis();
 long time_limit = 2000; // In milliseconds: this is the time you want to show the 
                         // object(2 sec)
 while(current_time - start_time) != 2000) {
    ShowObject(); //Here is where you show the object.
    current_time = System.currentTimeMillis();
 }

 HideObject(); //Hide the object after the while loop

It should work. And of course implement it however you want, because this is not the cleanest code ever. Or if you want something more efficient with handlers, etc. see this:

Link to another similar question on StackOverflow

Edit: Made it a bit better.

Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33
  • You need to edit your code just a tiny bit more. Currently, your code will only check if the two seconds has passed once. What you need to do is to add your code in a loop which will basically check whether or not the two seconds has passed. Again, a bad approach and what you said about using Handlers is the way to go here. – Bilal Naeem Jul 06 '19 at 16:44
  • Yeah, forgot about that. @BilalNaeem Thanks, will fix it! – Gaurav Mall Jul 06 '19 at 16:45
  • Yeah, handlers are greater, but I just thought about doing something interesting other than the already established and existing solution. It is, after all, a duplicate. – Gaurav Mall Jul 06 '19 at 16:46