[英]OnTouch not working in Player class
I am creating a game and would like to read the player input for the character in the characters class. 我正在创建一个游戏,并且想读取character类中角色的玩家输入。 I don't know if this is good practice for game development however it seems like the logical thing to do.
我不知道这是否是游戏开发的好习惯,但是这样做似乎是合乎逻辑的。
I have the following code within my Player class (relevant code included) however my suspicion is that it isn't working as it isn't the primary view class or something? 我的Player类中包含以下代码(包括相关代码),但是我怀疑它不起作用,因为它不是主视图类或其他东西? All help is appreciated.
感谢所有帮助。 Player
播放器
public class Player implements View.OnTouchListener{
private static Player instance = null;
int x, y;
boolean moveRight = false;
Bitmap sprite;
public Player (Context context){
sprite = BitmapFactory.decodeResource(context.getResources(), R.drawable.test_sprite);
x = 300;
y = 0;
}
public void Update(){
if (moveRight){
x += 3;
} else {
x -= 3;
}
}
public static Player getInstance(Context context) {
if (instance == null){
instance = new Player(context);
}
return instance;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
float x = motionEvent.getX(), y = motionEvent.getY();
switch (motionEvent.getAction() & motionEvent.getActionMasked()){
case MotionEvent.ACTION_DOWN:
moveRight = true;
break;
case MotionEvent.ACTION_UP:
moveRight = false;
break;
}
return true;
}
}
GameView GameView
public class GameView extends SurfaceView implements Runnable {
private boolean running = true;
SurfaceHolder surfaceHolder = getHolder();
SimpleTurret simpleTurret;
Player player;
private Thread thread = new Thread(this);
public GameView (Context context){
super(context);
simpleTurret = SimpleTurret.getInstance(context);
player = Player.getInstance(context);
thread.start();
}
@Override
public void run() {
while (running){
DrawCanvas();
Control();
}
}
public void DrawCanvas(){
Canvas canvas = surfaceHolder.lockCanvas();
if (surfaceHolder.getSurface().isValid()){
canvas.drawColor(Color.RED);
canvas.drawBitmap(simpleTurret.getSprite(), simpleTurret.getX(), simpleTurret.getY(), null);
canvas.drawBitmap(player.getSprite(), player.getX(), player.getY(), null);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
public void Control(){
try{
thread.sleep(17);
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
public class GameView extends SurfaceView implements Runnable, OnTouchListener {
public GameView(Context context) {
super(context);
this.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
} ...
this.setOnTouchListener(this);
line puts the OnTouchListener
on the GameView
class which extends the SurfaceView
. 线放
OnTouchListener
上GameView
它扩展了类SurfaceView
。 So now anytime there is a touch on your SurfaceView
the onTouch
method would be called 所以现在只要您的
SurfaceView
上有触摸, onTouch
方法就会被调用
You can use ImageView instead a Bitmap and set your implementation of View.OnTouchListener with the method setOnTouchListener : 您可以使用ImageView代替Bitmap并使用setOnTouchListener方法设置View.OnTouchListener的实现 :
ImageView sprite;
public Player (Context context){
sprite = new ImageView(context);
sprite.setBackgroundResource(R.drawable.monkey);
sprite.setOnTouchListener(this);
x = 300;
y = 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.