安卓游戏控制

时间:2012-07-15 15:45:07

标签: android

帮助我控制我的adnroid游戏)

我有2个按钮: 向左移动并向右移动如果我们按左侧精灵向左移动,如果我们按右精灵移动。

这是代码:

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN ||
                event.getAction() == MotionEvent.ACTION_MOVE){ 
        touchX = event.getX();
    }
    if(touchX == arrow_leftX){
        gameLoopThread.setRunning(false);
    }
    else if(touchX == arrow_leftX){
        playerX = playerX - xSpeed;
    }
    else if(touchX == arrow_rightX){
        playerX = playerX - xSpeed;
    }
    else if(touchX == arrow_rightX){
        gameLoopThread.setRunning(false);
    }
    return super.onTouchEvent(event);
} 

所以问题是:

  

它只向左移动,我不知道如何向右移动?

1 个答案:

答案 0 :(得分:1)

The Problem is here i think

else if(touchX == arrow_rightX){
        playerX = playerX - xSpeed;
    }


else if(touchX == arrow_rightX){

// Here You can see that you are again subtracting the x axis from
// playerX 
// it is clear that you are moving your image with this 
// but if you decrease value from x it shall move to lext
// to  move it to  right you need to increase its value 

        playerX = playerX + xSpeed;
    }