如何对用户多次点击做出反应
我在 AndEngine 中使用此方法来检测用户何时点击屏幕,
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
if(pSceneTouchEvent.isActionDown()) {
if(pSceneTouchEvent.isActionDown()) { //Jump only if the user tapped, not moved his finger or something
taps++;
if(taps == 1){
if(isJumping == false){
final float jumpDuration = 2;
final float startX = player.getY();
final float jumpHeight = 100;
final MoveYModifier moveUpModifier = new MoveYModifier(.1f, startX, startX - jumpHeight);
final MoveYModifier moveDownModifier = new MoveYModifier(.1f, startX - jumpHeight, startX);
final SequenceEntityModifier modifier = new SequenceEntityModifier(moveUpModifier, moveDownModifier);
player.registerEntityModifier(modifier);
isJumping = true;
hipp_jump.play();
return true;
}
}
}
}
return false;
}
Sooo 我遇到的问题是,如果用户双击屏幕,则精灵会跳跃两次并移动他离开了他应该回到的位置。因为当它跳跃两次时 Y 会发生变化。
即使用户点击多次,如何允许精灵每次点击仅移动一次?
I am using this method in AndEngine
to detect when a user taps on the screen,
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
if(pSceneTouchEvent.isActionDown()) {
if(pSceneTouchEvent.isActionDown()) { //Jump only if the user tapped, not moved his finger or something
taps++;
if(taps == 1){
if(isJumping == false){
final float jumpDuration = 2;
final float startX = player.getY();
final float jumpHeight = 100;
final MoveYModifier moveUpModifier = new MoveYModifier(.1f, startX, startX - jumpHeight);
final MoveYModifier moveDownModifier = new MoveYModifier(.1f, startX - jumpHeight, startX);
final SequenceEntityModifier modifier = new SequenceEntityModifier(moveUpModifier, moveDownModifier);
player.registerEntityModifier(modifier);
isJumping = true;
hipp_jump.play();
return true;
}
}
}
}
return false;
}
Sooo The issue I am having with this is that if the user double-taps the screen, then the sprite jumps twice which moves him out of the position he should return to. Because when it jumps twice the Y changes.
How can I allow the sprite to move only ONCE to each tap, even if the user taps more than once?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
手动解决方案是设置一个延迟(记录点击的时间戳),并忽略某个时间增量内的点击。我建议使用高分辨率
java.lang.System.nanoTime()
特别是电容式触摸屏很容易产生多次点击,甚至是无意的。它在 Android 中没有得到处理,并且已被证明对我们的应用程序来说是一个严重的问题...
更新:伪代码示例
The manual solution is to set a delay (record a timestamp of tap), and ignore taps within some timedelta. I suggest using the high-resolution
java.lang.System.nanoTime()
Especially capacitive touchscreens are prone to generate multiple taps even unintentionally. It is not handled in Android, and has proven to be a serious problem for our app...
Update: pseudocode sample