0

I have this class which extends SurfaceView. The code that I have so far makes the player follow my finger, but it also allows the player to "teleport" to wherever the finger goes and I do not want that.

@Override
public boolean onTouchEvent(MotionEvent event) {

    pointerX = (int)event.getX();
    pointerY = (int)event.getY();
    switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:

                if (!player.getPlaying()) {
                    player.setPlaying(true);

                }
                if (!player.playerAlive) {
                    if ((pointerX >= rescaleX(600) && pointerX <= rescaleX(842)) && (pointerY >= rescaleY(900) && pointerY <= rescaleY(1142))) {
                        //newGame();
                        player.setX(600);
                        player.setY(900);
                    }
                }
                if (player.playerAlive) {
                   // player.move(true);
                }

            break;

        case MotionEvent.ACTION_MOVE:

                if (!player.getPlaying()){
                    player.setPlaying(true);
                    player.move(true);
                } else {
                    player.move(true);
                }

            break;

        case MotionEvent.ACTION_UP:
            //player.move(true);
            if (!player.playerAlive) {
                if ((pointerX >= rescaleX(600) && pointerX <= rescaleX(842)) && (pointerY >= rescaleY(900) && pointerY <= rescaleY(1142))) {
                    newGame();
                    player.setX(600);
                    player.setY(900);
                }
            }
            break;
    }
    return true;
    //return super.onTouchEvent(event);
    }

Here is the move() method in the player class:

public void move(boolean b) {
    if(b){
        if((GamePanel.pointerX * GamePanel.WIDTH / MainActivity.dispX) - 141 >= 1380 - getWidth()){setX(1380-getWidth());}
        if((GamePanel.pointerY * GamePanel.HEIGHT / MainActivity.dispY) - 141 >= 1880 - getHeight()){setY(1880 - getHeight());}
            setX((int) ((GamePanel.pointerX * GamePanel.WIDTH / MainActivity.dispX) - 141));
            setY((int) ((GamePanel.pointerY * GamePanel.HEIGHT / MainActivity.dispY) - 141));

    }

1 Answers1

0

Don't "directly" translate finger inputs into 'logic' for moving things.

You should have your onTouchEvent method reads the X and Y values of where a finger press is, and then passes that to some logic. That logic should then decide if that info should be passed to a new method moveCharacterTo(x,y)

the 'logic' I mention above could be several things, You could use the strategy pattern to decide what 'strategy/policy(algorithm)' should be used to process the screen. Or you could just have a single class with a method to compare touch events and see if they are 'close' to the character or not, and then react accordingly.

mawalker
  • 2,072
  • 2
  • 22
  • 34
  • Isn't that what I am already doing? I'm using the move method in player to move. But it seems like what you mention is for organization sake, I can't seem to figure out why the player keeps "teleporting" when a finger taps away from the player when I only call the move method when the MOVE MotionEvent should be triggered. – Tirth Allspark Rami Dec 29 '15 at 12:11