为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕沐林林 2023-03-09 15:38:06
我正在制作一款 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}
查看完整描述

1 回答

?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

尝试这个:


 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

查看完整回答
反对 回复 2023-03-09
  • 1 回答
  • 0 关注
  • 59 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信