简体   繁体   English

记忆游戏-使2张卡在X时间后消失

[英]Memory game - Make the 2 cards disappear after X time

I wanted to make a "memories" or "find pairs" game for Java, just to start with a basic game before starting something more difficult. 我想为Java制作一个“记忆”或“寻找对”游戏,只是从开始一个基本的游戏开始,然后再开始一些困难的事情。 My game works very well, but I just have 1 big problem. 我的游戏效果很好,但是我只有一个大问题。 I don't really know how to hide the 2 images x seconds after the user clicks on the second one. 我真的不知道如何在用户单击第二张图像后x秒内隐藏2张图像。

This is what is done: 这是完成的操作:

  1. The game is created with 4x4 buttons in a array 用数组中的4x4按钮创建游戏
  2. The values of the cards are distributed (name, position, icon) 卡的值已分发(名称,位置,图标)
  3. The user then clicks on the first card, which shows up immediately 然后,用户单击第一张卡,该卡立即显示
  4. The user clicks on the second one, if the first is equal to the second one then disable the 2, if not then re-hide the 2. 用户单击第二个,如果第一个等于第二个,则禁用第二个,如果没有,则重新隐藏第二个。

But I don't know how I can make the program show the second card and then after x seconds hide the 2... 但是我不知道如何使程序显示第二张卡,然后在x秒后隐藏2张卡...

How can I solve this? 我该如何解决?

Depending on what framework you are using, there should be a timer utility available to you. 根据您使用的框架,应该有一个可用的计时器实用程序。

For instance, if you are using Swing, then you should be able to use javax.swing.Timer as suggested in the above comments. 例如,如果您使用的是Swing,则应该能够使用上述注释中所建议的javax.swing.Timer Follow this link for a simple Swing Timer tutorial . 单击此链接以获取简单的Swing Timer教程 As they say on that page, swing timers can be used in one of two ways and one of those ways is: 正如他们在该页面上说的那样,可以使用以下两种方法之一来使用摆动计时器,其中一种是:

To perform a task once, after a delay 延迟一次执行一次任务

This sounds like exactly what you are trying to achieve. 这听起来完全像您要实现的目标。

You could also try a library like Joda Time which has lots of features and options. 您也可以尝试像Joda Time这样的库,它具有很多功能和选项。

您应该创建匹配函数和另一个不匹配函数,并且在不匹配函数内部,可以使用setTimeout并在选择它们时以及在一定时间后和它们不匹配时给两张卡片分类(翻转),请移除此翻转类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM