Andengine中的连续触摸

Continuous Touch in Andengine

通过搜索,我找到了 onUpdate-onUpdateManager 之类的方法,但我一定是做错了,因为我找不到。我正在使用 .runOnUpdateThread(Runabale); 但它不起作用

如果有帮助,这是我的代码的一部分:

public class GameActivity extends SimpleBaseGameActivity {
private boolean action_up ,action_down;
Sprite eliSprite;
Runnable runabale=new Runnable() {
    @Override
    public void run() {
        Log.d("UPDATE","OnUpdate");
        if (action_down){
            eliSprite.setY(eliSprite.getY()-10);
        }
    }
};

受保护的场景 onCreateScene() { 场景 s = new Scene();

    PhysicsWorld mWorld =new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH),true);
    eliSprite = new Sprite(400, 240, james_walking[3], getVertexBufferObjectManager());
    final Sprite eliSprite2 = new  Sprite(400, 370, james_walking[0], getVertexBufferObjectManager());

    eliSprite.setScale(0.5f);
    eliSprite2.setScale(0.5f);

    FixtureDef characteristics= PhysicsFactory.createFixtureDef(1,0,.5f);
    final Body b= PhysicsFactory.createBoxBody(mWorld, eliSprite, BodyDef.BodyType.DynamicBody,characteristics);
    mWorld.registerPhysicsConnector(new PhysicsConnector(eliSprite,b));

    FixtureDef characteristics2= PhysicsFactory.createFixtureDef(1,.90f,1);
    final Body b2= PhysicsFactory.createCircleBody(mWorld,eliSprite2, BodyDef.BodyType.StaticBody,characteristics2);
    mWorld.registerPhysicsConnector(new PhysicsConnector(eliSprite2,b2));


    s.attachChild(eliSprite);
    s.attachChild(eliSprite2);

    s.registerUpdateHandler(mWorld);
    s.setTouchAreaBindingOnActionDownEnabled(true);

    s.setOnSceneTouchListener(new IOnSceneTouchListener() {
        @Override
        public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
            action_down = pSceneTouchEvent.isActionDown();
            action_up = pSceneTouchEvent.isActionUp();
            return true;
        }
    });
    this.getEngine().runOnUpdateThread(runabale);
    return s;
}

编辑

    @Override
public void runOnUpdateThread(Runnable pRunnable) {
    super.runOnUpdateThread(pRunnable);
    if(action_down) {
        Log.d("UPDATE","ActionDown pressed");
        b.setTransform(b.getPosition().x,b.getPosition().y-10,0);

    }

}

@Override
protected Scene onCreateScene() {
    Scene s = new Scene();

    PhysicsWorld mWorld =new PhysicsWorld(new Vector2(0, 0),true);
    eliSprite = new AnimatedSprite(400,240,james_walking,getVertexBufferObjectManager());

    eliSprite.setScale(0.5f);
    eliSprite.animate(200,true);

    FixtureDef characteristics= PhysicsFactory.createFixtureDef(1,0,.5f);
    b= PhysicsFactory.createBoxBody(mWorld, eliSprite, BodyDef.BodyType.DynamicBody,characteristics);

    mWorld.registerPhysicsConnector(new PhysicsConnector(eliSprite,b));

    s.attachChild(eliSprite);
    s.setOnSceneTouchListener(new IOnSceneTouchListener() {
        @Override
        public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
            action_down= pSceneTouchEvent.isActionDown();
            return true;
        }
    });

    s.registerUpdateHandler(mWorld);
    s.setTouchAreaBindingOnActionDownEnabled(true);

    return s;
}

您应该重写精灵对象的 onAreaTouched 方法,里面是放置代码的最佳位置。

eliSprite = new Sprite(400, 240, james_walking[3], getVertexBufferObjectManager()){
    public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
        if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
            setY(getY() - 10);
        }
    }
};

并且您应该将您的 sprite 注册为触摸区域侦听器。

s.registerTouchArea(eliSprite);

请注意,如果您希望在用户手指移动之前执行代码,则应改用 TouchEvent.ACTION_MOVE

我在游戏中使用了它。请根据您的代码进行调整。

在您的 onAreaTouchedonSceneTouchedEvent 场景中:

@Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY){
            if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
                leftPressed = true;
                //rightPressed = false;
            }   
            if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
                leftPressed = false;
            }
            return true;
        }

在精灵的 onManagedUpdate() 方法中输入:

@Override
        protected void onManagedUpdate(float pSecondsElapsed){
            super.onManagedUpdate(pSecondsElapsed);
            if(leftPressed == true){
                playerCar.setPosition(playerCar.getX() - 4f, playerCar.getY());
            }
            if(rightPressed == true){
                playerCar.setPosition(playerCar.getX() + 4f, playerCar.getY());
            }

        }

当我按下按钮时,这会将我的精灵像原木一样向左移动。所以通常你将布尔值在 TouchEvent == Action_Down 上更改为 true,在 Action_UP 上更改为 false。 onManagedUpdate 当此布尔值为真时移动您的精灵。