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?
If you’re not moving your finger, you’ll stop getting updates.
Instead of calling
movefrom the TouchListener, you should set a boolean flag likeshouldMove, and then at a fixed interval issue a move command ifshouldMoveis 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.