如何对用户多次点击做出反应

发布于 2024-12-26 18:46:53 字数 1207 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2025-01-02 18:46:53

手动解决方案是设置一个延迟(记录点击的时间戳),并忽略某个时间增量内的点击。我建议使用高分辨率 java.lang.System.nanoTime()

特别是电容式触摸屏很容易产生多次点击,甚至是无意的。它在 Android 中没有得到处理,并且已被证明对我们的应用程序来说是一个严重的问题...

更新:伪代码示例

private long lastTap=0;
onTap() {
  long now = System.nanoTime();
  if (now-lastTap < threshold) return;
  else lastTap = now;
  ...
}

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

private long lastTap=0;
onTap() {
  long now = System.nanoTime();
  if (now-lastTap < threshold) return;
  else lastTap = now;
  ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文