OnTouch在Player类中不起作用

时间:2018-01-23 14:52:17

标签: java android view ontouch

我正在创建一个游戏,并且想要读取角色类中角色的玩家输入。我不知道这是否是游戏开发的好习惯,但这似乎是合乎逻辑的事情。

我的Player类中包含以下代码(包含相关代码)但是我怀疑它不起作用,因为它不是主视图类或其他东西?所有帮助表示赞赏。 的播放器

public class Player implements View.OnTouchListener{

    private static Player instance = null;

    int x, y;
    boolean moveRight = false;
    Bitmap sprite;

    public Player (Context context){

        sprite = BitmapFactory.decodeResource(context.getResources(), R.drawable.test_sprite);
        x = 300;
        y = 0;
    }

    public void Update(){
        if (moveRight){
            x += 3;
        } else {
            x -= 3;
        }
    }

    public static Player getInstance(Context context) {
        if (instance == null){
            instance = new Player(context);
        }
        return instance;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        float x = motionEvent.getX(), y = motionEvent.getY();
        switch (motionEvent.getAction() & motionEvent.getActionMasked()){
            case MotionEvent.ACTION_DOWN:
                moveRight = true;
                break;
            case MotionEvent.ACTION_UP:
                moveRight = false;
                break;
        }
        return true;
    }
}

GameView

public class GameView extends SurfaceView implements Runnable {

    private boolean running = true;

    SurfaceHolder surfaceHolder = getHolder();

    SimpleTurret simpleTurret;
    Player player;

    private Thread thread = new Thread(this);

    public GameView (Context context){
        super(context);

        simpleTurret = SimpleTurret.getInstance(context);
        player = Player.getInstance(context);

        thread.start();
    }

    @Override
    public void run() {
        while (running){
            DrawCanvas();
            Control();
        }
    }

    public void DrawCanvas(){
        Canvas canvas = surfaceHolder.lockCanvas();
        if (surfaceHolder.getSurface().isValid()){
            canvas.drawColor(Color.RED);
            canvas.drawBitmap(simpleTurret.getSprite(), simpleTurret.getX(), simpleTurret.getY(), null);
            canvas.drawBitmap(player.getSprite(), player.getX(), player.getY(), null);
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }

    public void Control(){
        try{
            thread.sleep(17);
        } catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

public class GameView extends SurfaceView implements Runnable, OnTouchListener {

  public GameView(Context context) {
    super(context);
    this.setOnTouchListener(this);
  }

  @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {
    return false;
  } ...

this.setOnTouchListener(this);行将OnTouchListener放在扩展GameView的{​​{1}}类上。因此,现在只要触摸SurfaceView SurfaceView方法,就会调用

答案 1 :(得分:0)

您可以使用ImageView代替Bitmap,并使用方法 setOnTouchListener 设置View.OnTouchListener的实现:

ImageView sprite;

public Player (Context context){
    sprite = new ImageView(context);
    sprite.setBackgroundResource(R.drawable.monkey);
    sprite.setOnTouchListener(this);
    x = 300;
    y = 0;
}