[英]Stopping a Swing timer until the user clicks
I'm writing a card game. 我在写纸牌游戏。 Right now I'm having problems with mouse handling.
现在,我在处理鼠标时遇到了问题。 Below is the timer that handles the game flow of drawing and discarding cards.
下面是处理抽奖和弃牌游戏流程的计时器。
final Timer timer = new Timer(1000, null);
timer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
b.players[p].drawCard();
if(p==0) // player zero is the human player
{
timer.stop();
// ...
b.players[p].discardCard(i);
timer.start();
}
else
b.players[p].discardCard(0);
p=(p+1)%4;
b.repaint();
}
});
The thing is that I want to stop the timer, wait until the user clicks the card he wants to discard, then start the timer again. 问题是我想停止计时器,等到用户单击要丢弃的卡后再重新启动计时器。
b
implements MouseListener
in a basic way: b
以基本方式实现MouseListener
:
public void mouseClicked(MouseEvent arg0)
{
clickX = arg0.getX();
clickY = arg0.getY();
}
There's also the xYtoCardIndex()
method somewhere out there. 那里也有
xYtoCardIndex()
方法。
What do I do here? 我在这里做什么? I assume I have to do nothing in a nonblocking way, right?
我想我必须以无阻碍的方式做任何事情,对不对?
In pseudo-code, in your MouseEventListener : 在伪代码中,在MouseEventListener中:
public void mouseClicked(MouseEvent arg0)
{
clickX = arg0.getX();
clickY = arg0.getY();
Card discarded = getCard(clickX,clickY);
b.players[p].discardCard(discarded);
// The card has been discarded, I can start my timer again.
timer.start();
}
In your drawCard function : 在您的drawCard函数中:
public void drawCard() {
// Stop the timer
timer.stop();
// Do the drawing.
}
This way, when the player draws a card, the timer stops until a card is discarded. 这样,当玩家抓牌时,计时器停止,直到弃牌为止。
First, your code is not compiled: 首先,您的代码未编译:
b.players[p].discardCard(int i);
contains a syntax error int i
. 包含语法错误
int i
。
Second, I do not really understand the problem. 第二,我不太了解这个问题。 Stop the timer when you want, implement your listener (ie mouse listener) that starts the timer.
在需要时停止计时器,实现启动计时器的侦听器(即鼠标侦听器)。
Or probably I did not understand your question? 也许我不明白您的问题?
BTW I have just checked Timer API. 顺便说一句,我刚刚检查了计时器API。 It does not have neither start nor stop methods.
它既没有启动方法也没有停止方法。 You have to deal with specific tasks to control execution.
您必须处理特定任务以控制执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.