猿问

在特定点显示对象但仅持续几秒钟

我正在制作一款 Android 游戏(只是为了学习一些东西)。您收集对象并为此获得积分。例如,当玩家达到 200 分时,物体开始移动得更快,背景发生变化等......就像是下一个级别。但是当你达到那 200 分时,我想在屏幕上显示图像或文本(类似于“2 级”)大约 2 秒,但我不知道如何显示。


我尝试使用计时器,但失败了。


我有一个“如果”的陈述


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

}


慕沐林林
浏览 71回答 1
1回答

慕婉清6462132

尝试这个: 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它应该工作。当然,你可以随心所欲地实现它,因为这不是有史以来最干净的代码。或者,如果您想要更高效的处理程序等,请参阅:https://stackoverflow.com/questions/23838071/how-to-hide-a-imageview-and-show-it-after-5-second
随时随地看视频慕课网APP

相关分类

Java
我要回答