Java如何将onTouchEvent的运动仅设置为某些对象

时间:2015-05-27 16:52:42

标签: java android ontouchevent

最近,我从一些教程中学到了如何使屏幕的任何部分接收触摸输入,但我想知道如何仅将onTouchEvent赋予某个按下的对象。我还研究过,如果我想这样做,我需要让对象的类扩展View(尽管我还不知道该怎么做)。不幸的是,我的班级扩展了另一堂课。 那么如何将onTouchEvent仅设置为播放器对象? 附:目前,我的代码会从触摸屏幕的任何部分接收触摸事件。

主要代码:

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{
public GamePanel(Context context){
    super(context);

    //Add callback to the surfaceview to intercept events
    getHolder().addCallback(this);

    //Make GamePanel focusable so it can handle events
    setFocusable(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder){
    bg = new Background(BitmapFactory.decodeResource(getResources(),R.drawable.background));
    player = new Player(BitmapFactory.decodeResource(getResources(),R.drawable.character),WIDTH/2,HEIGHT/2+40,80,75,14);
    bouncer = new ArrayList<Bouncer>();

    thread = new MainThread(getHolder(), this);

    //Start the game loop
    thread.setRunning(true);
    thread.start();

}
@Override
public boolean onTouchEvent(MotionEvent event){
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        //if not jumping
        if(!player.getIsJumping() && !player.getFall()) {
            //set temp to store distance that should be travelled
            double temp = 1.06 * (bouncer.get(bouncerIndex).getY()-bouncer.get(bouncerIndex+1).getY());
            //setDy based on s = v0*t + 1/2*a*t^2
            player.setDy(((int)Math.ceil((temp+450)/30))*-1);
            player.setIsJumping(true);
            jumpStartTime = System.nanoTime();
        }
        return true;
    }
    if(event.getAction() == MotionEvent.ACTION_UP){
        return true;
    }
    return super.onTouchEvent(event);
}
}

玩家代码:

 public class Player extends GameObject{
 public Player(Bitmap res,int x, int y,int w,int h, int numFrames){
        this.x = x;
        this.y = y;
        height = h;
        width = w;

        Bitmap[] image = new Bitmap[numFrames];
        spritesheet = res;

        for(int i=0;i<image.length;i++){
            if(i%7==0 && i>0)row++;
            image[i] = Bitmap.createBitmap(spritesheet,(i-7*row)*width,row*height,width,height);
        }
        animation.setFrames(image);
        animation.setDelay(200);
    }
}

游戏对象代码:

public abstract class GameObject {
protected int x;
protected int y;
protected int width;
protected int height;
protected int dx;
protected int dy;

public void setX(int x){
    this.x = x;
}

public void setY(int y){
    this.y = y;
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}
}

对不整齐的代码抱歉。谢谢!

1 个答案:

答案 0 :(得分:0)

就在这里if(event.getAction() == MotionEvent.ACTION_DOWN)。这可能就是为什么

  

目前,我的代码会从触摸屏幕的任何部分接收触摸事件。

如何只为播放器设置ontouchlistener而不是实现它?

等等;

player.setOnTouchListener(new View.OnTouchListener() {               
        public boolean onTouch(View v, MotionEvent event) {             

            int action = event.getAction();
            switch (action) {

            case MotionEvent.ACTION_DOWN:
                Log.i("PLAYER", "image has been touched"); 
             //if not jumping
               if(!player.getIsJumping() && !player.getFall()) {
            //set temp to store distance that should be travelled
              double temp = 1.06 * (bouncer.get(bouncerIndex).getY()-      bouncer.get(bouncerIndex+1).getY());
            //setDy based on s = v0*t + 1/2*a*t^2
              player.setDy(((int)Math.ceil((temp+450)/30))*-1);
              player.setIsJumping(true);
              jumpStartTime = System.nanoTime();
                }  


                break;


            }               
            return false;
        }
    });

希望这有帮助。