如何从触摸事件之前启动的计时器获取值?
我想制作一个倒计时器,当在 android 中执行触摸时返回值(剩余秒数)。问题是我使用 andengine ,因此触摸事件不能在方法内部使用(如果我错了,请纠正我)。
scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
public boolean onSceneTouchEvent(Scene scene, TouchEvent arg1) {
int counterInt = 60;
Timer timer = new Timer();
counterText.setText(""+counterInt);
timer.wait(1000);
counterText.setText(""+(counterInt-1));
timer.wait(1000);
counterText.setText(""+(counterInt-1));
timer.wait(1000);
counterText.setText(""+(counterInt-1));
}
}
假设 counterInt 的默认数量是 60,并且它以秒为单位减少。当它显示 50 个用户触摸屏幕时,这意味着激活 touchevent arg1,我如何获取具有“50”的 counterInt 值?谢谢...
I'm want to make a countdown timer that return value(seconds left) when a touch performed in android. Problem is I use andengine so that the touch event cannot be used inside the method(correct me if I'm wrong).
scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
public boolean onSceneTouchEvent(Scene scene, TouchEvent arg1) {
int counterInt = 60;
Timer timer = new Timer();
counterText.setText(""+counterInt);
timer.wait(1000);
counterText.setText(""+(counterInt-1));
timer.wait(1000);
counterText.setText(""+(counterInt-1));
timer.wait(1000);
counterText.setText(""+(counterInt-1));
}
}
let say default number of counterInt is 60 and its decrease by seconds. When it shows 50 user touch the screen which means activate the touchevent arg1, how I can get the value of counterInt that has '50'? Thank you...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在寻找类似的东西,您想要剩余的秒数,那么最好使用 <代码>倒计时器。它的 onTick() 为您提供计时器启动时剩余的确切时间,并且当倒计时结束时您可以在 onFinish() 内执行任何操作。
If you are looking for something like that you want the remaining seconds than its better to use
CountDownTimer
. ItsonTick()
gives you the exact time left in the timer started and also you can do any stuff when the countdown finishes inside theonFinish()
.使用 AndEngine 的框架有一种更简单的解决方案。我为您创建了这个简单的类:此处。创建它的一个实例,并将其注册到您的场景中:
它将从那一刻开始计数。然后,您可以自由调用 tc.getSeconds() 来找出过去了多少秒,并从任何常量值中减去它们以模拟倒计时。
您还可以调用
reset
来重置秒数。Theres a way easier solution using AndEngine's framework. I've created this simple class for you: here. Create an instance of it, and register it in your scene:
It will start counting from that moment. Then, you can freely call
tc.getSeconds()
to find out how many seconds passed, and subtract them from any constant value to simulate a countdown.You can also call
reset
to reset the seconds.