简体   繁体   English

如何通过单击方向按钮移动onDraw形状

[英]How to move a onDraw shape by click direction button

I'm doing a simulator on Android to simulate the move of our robot(represented by a circle in this case). 我正在Android上做一个模拟器来模拟机器人的运动(在这种情况下以圆圈表示)。 The circle should move forward, turn right or turn left accordingly when the forward/ right/ left button is clicked. 单击前进/右/左按钮时,圆圈应向前移动,向右转或向左转。 But when I run this program and click the buttons, the circle didn't move... 但是当我运行该程序并单击按钮时,圆圈没有移动。

In the main activity I have the onClickListener like this: 在主要活动中,我具有如下所示的onClickListener:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    findViewById(R.id.forward_button).setOnClickListener(mGlobal_OnClickListener);
    findViewById(R.id.right_button).setOnClickListener(mGlobal_OnClickListener);
    findViewById(R.id.left_button).setOnClickListener(mGlobal_OnClickListener);


}


 View.OnClickListener mGlobal_OnClickListener = new View.OnClickListener() {
    public void onClick(View v) {
        GameView gameView = new GameView(Menu.this);
        switch(v.getId()) {
            case R.id.forward_button:
                gameView.controlRobot(GameView.FORWARD);
                break;
            case R.id.right_button:
                gameView.controlRobot(GameView.RIGHT);
                break;
            case R.id.left_button:
                gameView.controlRobot(GameView.LEFT);


        }
    }
};

In the GamaView Class which extends View class, I draw a circle like this: 在扩展View类的GamaView类中,我画了一个圆形:

canvas.drawCircle((currentX * cellHeight)+(cellHeight/2),   //x of center
            (currentY * cellHeight)+(cellHeight/2),  //y of center
            (cellHeight*3*0.45f),                           //radius
            robot);

Also In the GameView Class I have this controlRobot method to move this circle: (The move/ rotate methods are correct. I have tested them) 同样在GameView类中,我有这个controlRobot方法来移动这个圆圈:( move / rotate方法是正确的。我已经测试了它们)

public boolean controlRobot(int keyCode) {
    boolean moved = false;
    boolean rotated = false;

    //move(0):move up on the map
    //move(3):move right on the map
    //move(6):move down on the map
    //move(9):move left on the map

        //forward
    switch (keyCode) {
        case FORWARD:
            if(rotate_count%12==0)
                moved = maze.move(0);

            if(rotate_count%12==3)
                moved = maze.move(3);
            if(rotate_count%12==6)
                moved = maze.move(6);
            if(rotate_count%12==9)
                moved = maze.move(9);
            break;
        case RIGHT:
            rotated = maze.rotate(Maze.RIGHT,rotate_count);
            if(rotated)
            rotate_count+=3;
            break;
        case LEFT:
            rotated = maze.rotate(Maze.LEFT,rotate_count);
            if(rotated)
            rotate_count-=3;
            break;
    }


    if(moved||rotated) {
        //the ball was moved so we'll redraw the view
        invalidate();
    }
    return true;
}

when your reference the GameView in your onclick like this 当您像这样在onclick中引用GameView时

GameView gameView = new GameView(Menu.this);

that is not the GameView in your layout file 那不是您的布局文件中的GameView

R.layout.main

try referencing the GameView in your onClick like you did your buttons 尝试像在按钮上一样引用onClick中的GameView

findViewById(R.id.forward_button)

except with the R.id for the GameView in your layout file 布局文件中GameView的R.id除外

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM