gpt4 book ai didi

java - 不会继续在 ACTION_DOWN 上执行语句

转载 作者:行者123 更新时间:2023-11-30 09:16:40 28 4
gpt4 key购买 nike

我正在为我的任务制作一个月球着陆器游戏

我有一个按钮并为其设置了一个 onTouchListener(不是 onClick)当按钮按下或 ACTION_DOWN 时,火箭飞起来并在 ACTION_UP 时停止加速。

目前它只在 ACTION_DOWN 上执行一次,所以它的行为就像一个 onClick 按钮。

任何帮助将不胜感激

  • gView 是所有魔法发生的游戏类实例
  • gView.mainThruster() 调用游戏类中使飞船向上飞行的方法,例如 Y -= 10;

    mThrust = (Button) findViewById(R.id.mainThrustBtn);    
    mThrust.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {

    if(event.getAction() == MotionEvent.ACTION_DOWN) {
    if(fuel > 0){

    gView.checkFuel(fuel);
    gView.mainThruster();
    mThrustSound.start();
    fuel -= 0.5;
    fuelGauge.setProgress((int)fuel);
    }
    }
    else if(event.getAction() == MotionEvent.ACTION_UP){

    mThrustSound.pause();
    //mThrustSound.release();
    }
    else{
    //do nothing
    }
    return true;
    }
    });

最佳答案

文档使这一点相当清楚:

On pointing devices with source class SOURCE_CLASS_POINTER such as touch screens, the pointer coordinates specify absolute positions such as view X/Y coordinates. Each complete gesture is represented by a sequence of motion events with actions that describe pointer state transitions and movements. A gesture starts with a motion event with ACTION_DOWN that provides the location of the first pointer down. As each additional pointer that goes down or up, the framework will generate a motion event with ACTION_POINTER_DOWN or ACTION_POINTER_UP accordingly. Pointer movements are described by motion events with ACTION_MOVE. Finally, a gesture end either when the final pointer goes up as represented by a motion event with ACTION_UP or when gesture is canceled with ACTION_CANCEL.

因此您应该期望获得单个ACTION_DOWN 事件,然后是多个ACTION_MOVE 事件。听起来基本上你还需要响应 ACTION_MOVE 如果你想处理 Action ,如果你想在触摸发生时连续做某事,你可能想要某种计时器事件(因为您不会生成新的运动事件 - 仅触摸和按住不涉及运动)。

关于java - 不会继续在 ACTION_DOWN 上执行语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19354346/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com