好的,我创建了一个 ontouchlistener 类:
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;
}
}
不幸的是 player_x 仅在屏幕被按下或拖过时才会更新,我希望当手指在屏幕的以太一侧时它会移动。有任何想法吗?