我如何使用NSTimer每秒进行一次动作?
对尼基塔说.
我正在制作一个非常简单的蛇游戏,我有一个名为Apple的对象,我想每隔X秒移动到一个随机位置.所以我的问题是,每X秒执行此代码的最简单方法是什么?
apple.x = rg.nextInt(470);
apple.y = rg.nextInt(470);
Run Code Online (Sandbox Code Playgroud)
谢谢.
编辑:
好吧有一个像这样的计时器:
Timer t = new Timer(10,this);
t.start();
Run Code Online (Sandbox Code Playgroud)
它的作用是在游戏启动时绘制我的图形元素,它运行以下代码:
@Override
public void actionPerformed(ActionEvent arg0) {
Graphics g = this.getGraphics();
Graphics e = this.getGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
e.fillRect(0, 0, this.getWidth(), this.getHeight());
ep.drawApple(e);
se.drawMe(g);
Run Code Online (Sandbox Code Playgroud) package xyz;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class XYZ {
public static void main(String[] args) throws InterruptedException {
class TimeClass implements ActionListener {
private int counter = 0;
@Override
public void actionPerformed(ActionEvent e) {
counter++;
System.out.println(counter);
}
}
Timer timer;
TimeClass tc = new TimeClass();
timer = new Timer (100, tc);
timer.start();
Thread.sleep(20000);
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中:
应该在main()函数内创建TimeClass.否则,它会显示错误"非静态变量,这不能从静态上下文中引用".为什么是这样?
当我使用TimeClass的访问说明符如public或private时,我得到了一个非法的表达式错误启动.为什么是这样?
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
PopIt("Exit Application", "Are you sure you want to exit?");
super.onBackPressed();
}
public void PopIt( String title, String message ){
new AlertDialog.Builder(this)
.setTitle( title )
.setMessage( message )
.setPositiveButton("YES", new OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1) {
//do stuff onclick of YES
finish();
}
}).setNegativeButton("NO", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//do stuff onclick of CANCEL
Toast.makeText(getBaseContext(), "You touched CANCEL", Toast.LENGTH_SHORT).show();
}
}).show();
}
Run Code Online (Sandbox Code Playgroud)
这个警报对话框速度太快,我无法阅读或点击它!这是为什么 …
我有一个JLabel.最初我已经为它设置了一些文字.
JLabel j = new JLabel();
// set width height and position
j.setText("Hello");
Run Code Online (Sandbox Code Playgroud)
我只希望文本Hello显示5秒钟.然后我想要显示文本再见.
我怎么能这样做
我的工作; 但我知道这是错的,因为它一次只执行1个if-else块.我想我需要一个计时器或一个计数器.让这个工作.救命 ?
long time = System.currentTimeMillis();
if ( time < (time+5000)) { // adding 5 sec to the time
j.setText("Hello");
} else {
j.setText("Bye");
}
Run Code Online (Sandbox Code Playgroud) 当我在JPanel之间切换时,我遇到了KeyListener没有响应(由于没有获得焦点)的问题.
我有谷歌这个,并知道要解决这个问题,我需要使用KeyBindings,但我不喜欢KeyBindings.所以我想知道,还有其他方法吗?
这是JPanel的初始化代码,它具有无响应的KeyListener:
public void init()
{
setFocusable(true);
requestFocusInWindow();
requestFocus();
addMouseListener(new MouseInput());
addMouseMotionListener(new MouseInput());
addMouseWheelListener(new MouseInput());
addKeyListener(new KeyInput(p));
t = new Timer(10, this);
t.start();
}
Run Code Online (Sandbox Code Playgroud)
如果需要,请随时索取更多代码示例!
可能重复:
如何将毫秒转换为"hh:mm:ss"格式?
我打赌很多人需要一个计时器,包括:minutes : seconds : hundreds of seconds.显然,你开始:
` public TimeGame(){
timer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
counter++;
}
});
timer.start();
}`
Run Code Online (Sandbox Code Playgroud)
然后你需要调用一个改变这个计数器的方法:minutes : seconds : hundreds of seconds.
`private String timeTransfer(){
minutes = counter/6000;
counter = counter - (minutes*6000);
seconds = counter/100 ;
counter = counter - (seconds*100);
milliseconds = counter;
return minutes + " : " + seconds + " : " + miliseconds;
}`
Run Code Online (Sandbox Code Playgroud)
但我的方法有一个错误.一旦它达到100毫秒,它就会跳回到0.通常它必须放 …
在我的Android应用程序中,我正在使用Timer schedule.but得到Timer-0致命异常如下所示.我可以删除它.我还提到下面的代码 -
01-28 13:44:41.142: E/AndroidRuntime(1307): FATAL EXCEPTION: Timer-0
01-28 13:44:41.142: E/AndroidRuntime(1307):android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.View.invalidate(View.java:5279)
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.View.setBackgroundDrawable(View.java:7626)
01-28 13:44:41.142: E/AndroidRuntime(1307): at android.view.View.setBackgroundResource(View.java:7535)
01-28 13:44:41.142: E/AndroidRuntime(1307): at com.example.iqtest.PlayGame$1.run(PlayGame.java:61)
01-28 13:44:41.142: E/AndroidRuntime(1307): at java.util.Timer$TimerImpl.run(Timer.java:284)
Run Code Online (Sandbox Code Playgroud)
和代码是 -
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated …Run Code Online (Sandbox Code Playgroud) 通过以下代码(timer2的间隔为1000)
private void timer1_Tick(object sender, EventArgs e) {
timer7.Enabled=false;
timer8.Enabled=false;
lblTimer_Value_InBuildings.Text="0";
}
private void timer2_Tick(object sender, EventArgs e) {
lblTimer_Value_InBuildings.Text=(int.Parse(lblTimer_Value_InBuildings.Text)+1).ToString();
}
Run Code Online (Sandbox Code Playgroud)
我们不能在for循环中创建延迟
for(int i=1; i<=Max_Step; i++) {
// my code...
// I want delay here:
timer1.Interval=60000;
timer1.Enabled=true;
timer2.Enabled=true;
// Thread.Sleep(60000); // makes no change even if uncommenting
}
Run Code Online (Sandbox Code Playgroud)
我是否取消对该行Thread.Sleep(60000);或不行,我们什么也看不到有改变lblTimer_Value_InBuildings在timer2_Tick.
你能给我一个解决方案(有或没有计时器)?
我不知道如何做到这一点,我已经尝试搞乱计时器但到目前为止无济于事.
那我该怎么办呢?
我有一个空白的标签.当某个事件被触发时,我希望标签在5秒钟内说"竞争成功设置",之后我希望它返回为空白.
当然可以这样做?? 它可以?我玩了一个计时器,但我似乎远远不够.
任何帮助都是最受欢迎的.我的微弱尝试在下面.
private void UpdateLabel(object sender, EventArgs e)
{
var timer = new Timer()
{
Interval = 5000,
};
timer.Tick += (s, evt) =>
lblCompetitionSetupSuccess.Text = "Competition successfully setup";
timer.Start();
lblCompetitionSetupSuccess.Text = string.Empty;
}
Run Code Online (Sandbox Code Playgroud)