0

ok to i created an ontouchlistener class:

package drop.out.Game;

import android.view.MotionEvent;

import android.view.View;

import drop.out.Game.Model.Player;

public final class TrackingTouchListener 

 implements View.OnTouchListener{

//player class
private final Player mplayer;
Player plyr;

//constructor bringing in the player
TrackingTouchListener(Player plyr){mplayer = plyr;}

    //on touch stuff
     public boolean onTouch(View v, MotionEvent evt) {

         /**@if touching/moving on the left side of screen */
         if(MotionEvent.ACTION_DOWN== evt.getAction() && evt.getX() < (v.getWidth()/2) || MotionEvent.ACTION_MOVE== evt.getAction() && evt.getX() <(v.getWidth()/2)){
             moveL(mplayer);
         }
         /**@if touching/moving on the right side of the screen */
         if(MotionEvent.ACTION_DOWN== evt.getAction() && evt.getX() > (v.getWidth()/2) || MotionEvent.ACTION_MOVE== evt.getAction() && evt.getX() >(v.getWidth()/2)){
             moveR(mplayer);
         } 

         return true; 
     }

     //call functions in the player class that moves the player x by -1
     private void moveL(Player player){
         player.moveL();
         //e.g. player_x -=10;
     }

     private void moveR(Player player){
         player.moveR();  
         //e.g.player_x +=10;
     }

}

Unfortunately the player_x is only updated when the screen is pressed or dragged across, I was hoping to have it move when the finger is on ether side of the screen. Any ideas?

Russell Cargill
  • 270
  • 1
  • 6
  • 19

2 Answers2

0

If you're not moving your finger, you'll stop getting updates.

Instead of calling move from the TouchListener, you should set a boolean flag like shouldMove, and then at a fixed interval issue a move command if shouldMove is true.

Then your character will continue to move even when you stop receiving touch events. Just clear this boolean when you lift your finger, or move out of a certain area.

Tim
  • 35,413
  • 11
  • 95
  • 121
0

You could also just set an x value on the touchlistener then check its location to determine left or right in the main loop (like you are within the touch listener) and just set it to 0 when the finger is lifted up

edit: so

if(MotionEvent.ACTION_DOWN== evt.getAction()  || MotionEvent.ACTION_MOVE== evt.getAction()) 
{
    moveX = evt.getX();
}

then check the moveX later and set the moveX to 0; on MotionEvent.ACTION_UP

RustyH
  • 473
  • 7
  • 22