我有服装View,基本上,在不同的线程中运行我的游戏(在另一个类中实现),但是虽然有一个被调用的onTouchEvent(),我无法调用onFling方法(测试槽)断点)。
Costum View的课程:
public class RenderGameActivity extends SurfaceView implements
SurfaceHolder.Callback, GestureDetector.OnGestureListener {
private GameThread thread;
private Context context;
private static boolean showPauseMenu;
private GestureDetector gestureScanner;
public RenderGameActivity(Context context) {
super(context);
this.context = context;
// adding the callback (this) to the surface holder to intercept events
getHolder().addCallback(this);
// create the game loop thread
thread = new GameThread(getHolder(), this);
// make the GamePanel focusable so it can handle events
setFocusable(true);
gestureScanner = new GestureDetector(context, this);
}
@Override
public void surfaceChanged(...) {}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setName("TacticalDefence.game");
// at this point the surface is created and
// we can safely start the game loop
thread.setRunning(true);
thread.start();
}
public void surfacePaused() {
synchronized(thread) {
thread.setRunning(false);
try {
thread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
thread.setRunning(false);
// tell the thread to shut down and wait for it to finish
// this is a clean shutdown
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// try again shutting down the thread
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Game.onTouchEvent(event);
return gestureScanner.onTouchEvent(event);
}
public void render(Canvas canvas) {
Game.onDraw(context, canvas);
// display fps
if(IsInDebug.isInDebugMode(context)) displayFps(canvas, avgFps);
}
/**
* This is the game update method. It iterates through all the objects
* and calls their update method if they have one or calls specific
* engine's update method.
*/
public void update() { Game.onUpdate();}
/*...*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return Game.onFling(e1, e2, velocityX, velocityY);
}
}
很抱歉,如果代码太多,但我觉得我必须发布它,因为我没有哪个部分会有用...
答案 0 :(得分:0)
这可能不是解决问题的最佳方法,但我找不到其他方法...... 我用这段代码“重制”了onFling()方法:
private static float [] e1;
private static float start;
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean d = true;
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
MotionEvent e2 = event;
float velocityX = Math.abs((e1 [0] - e2.getX()) / start);
float velocityY = Math.abs((e1 [0] - e2.getY()) / start);
onFling(e1, event, velocityX, velocityY);
} else if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
e1 = new float[] {event.getX(), event.getY()};
start = System.currentTimeMillis() / 1000;
}
Game.onTouchEvent(event);
return true;
}
public static boolean onFling(float[] e1, MotionEvent e2, float velocityX, float velocityY) {
return Game.onFling(e1, e2, velocityX, velocityY);
}