onGesturePerformedListener 与 SurfaceView 和动画线程
我正在开发一款使用手势和 2d 动画的 Android 儿童游戏。我在游戏中有三个类:主活动、SurfaceView(用于动画)和控制动画的线程。非常简单的东西。我的手势按照我想要的方式工作。我让场景按照我想要的方式工作。问题是,我希望场景在用户执行手势时做出反应。所以,现场有一个明星。我希望孩子追踪星星(作为手势预加载),但当他追踪它时,我想为星星设置动画。我已经放置了许多日志来查看线程是否正在触发,但它没有接收消息。所以,基本上,我希望两者协同工作 - 我希望 onGesturePerformedListener 捕获触摸事件,并且我希望 SurfaceView 捕获相同的触摸事件。双方都需要对此做出反应。就像我说的,我让他们出色地独立工作,但我需要他们同时工作。我缺少什么?
Main Activity:
mGameView = (GameView)findViewById(R.id.gamearea);
mGameView.setStatusView((TextView)findViewById(R.id.text));
mGameView.setScoreView((TextView)findViewById(R.id.score));
mGestureView = (GestureOverlayView)findViewById(R.id.gestures);
mGestureView.addOnGesturePerformedListener(this);
该类还包含 onGesturePerformed 方法。
SurfaceView:(这是设置线程的方法。)
public void setThread(GameThread newThread) {
thread = newThread;
setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(thread!=null) {
Log.d(TAG, "Touched the screen");
return thread.onTouch(event);
}
else return false;
}
});
setClickable(true);
setFocusable(true);
}
Thread:
//Finger touches the screen
public boolean onTouch(MotionEvent e) {
if(e.getAction() != MotionEvent.ACTION_DOWN){
Log.d(TAG, "onTouch Fired");
return false;
}
if(e.getAction() == MotionEvent.ACTION_DOWN)
{
dotX = e.getX();
dotY = e.getY();
Log.d(TAG, "Updating Dot by touch.");
return true;
}
if(mMode == STATE_READY || mMode == STATE_LOSE || mMode == STATE_WIN) {
doStart();
Log.d(TAG, "doStart Loaded");
return true;
}
if(mMode == STATE_PAUSE) {
Log.d(TAG, "mode is paused");
unpause();
return true;
}
synchronized (mSurfaceHolder) {
Log.d(TAG, "synchronized");
this.onTouch(e);
}
return false;
}
我的工作理论是,一切都取决于overlayview的放置 - 这是我理解上的失败之处。 main.xml 是主活动的设置(如上所示),如下所示:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"/>
<com.trial.game.test.GameView
android:id="@+id/gamearea"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<RelativeLayout
android:id="@+id/gameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/score"
android:text="@string/score_text"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top"
android:textColor="#ff0000ff"
android:textSize="24sp"
android:layout_marginTop="3px"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/text"
android:text="@string/mode_ready"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#ff0000ff"
android:textSize="20sp"/>
</RelativeLayout>
根据手势叠加或游戏视图的位置,其中之一将触发,但不会同时触发。我在这里忽略或误解了什么。有人吗?
I am working on a children's game for Android that uses gestures and 2d animation. I have three classes in the game: the main activity, the SurfaceView (for the animation), and a thread to control the animation. Pretty simplistic stuff. I have gestures working the way I want. And I have the scene working the way I want. The problem is, I would like the scene to react as the user performs the gesture. So, there is a star on the scene. I would like the child to trace the star (preloaded as a gesture) but as he traces it, I would like to animate the star. I have put a number of logs to see if the thread is firing, but it is not receiving messages. So, basically, I want the the two to work in tandem - I want to the onGesturePerformedListener to grab the touch event, and I want the surfaceView to grab the same touch event. Both need to react to it. Like I said, I have them working independently brilliantly, but I need them to work at the same time. What am I missing?
Main Activity:
mGameView = (GameView)findViewById(R.id.gamearea);
mGameView.setStatusView((TextView)findViewById(R.id.text));
mGameView.setScoreView((TextView)findViewById(R.id.score));
mGestureView = (GestureOverlayView)findViewById(R.id.gestures);
mGestureView.addOnGesturePerformedListener(this);
This class also holds the onGesturePerformed method.
SurfaceView: (This is the method that sets the thread.)
public void setThread(GameThread newThread) {
thread = newThread;
setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(thread!=null) {
Log.d(TAG, "Touched the screen");
return thread.onTouch(event);
}
else return false;
}
});
setClickable(true);
setFocusable(true);
}
Thread:
//Finger touches the screen
public boolean onTouch(MotionEvent e) {
if(e.getAction() != MotionEvent.ACTION_DOWN){
Log.d(TAG, "onTouch Fired");
return false;
}
if(e.getAction() == MotionEvent.ACTION_DOWN)
{
dotX = e.getX();
dotY = e.getY();
Log.d(TAG, "Updating Dot by touch.");
return true;
}
if(mMode == STATE_READY || mMode == STATE_LOSE || mMode == STATE_WIN) {
doStart();
Log.d(TAG, "doStart Loaded");
return true;
}
if(mMode == STATE_PAUSE) {
Log.d(TAG, "mode is paused");
unpause();
return true;
}
synchronized (mSurfaceHolder) {
Log.d(TAG, "synchronized");
this.onTouch(e);
}
return false;
}
My working theory is that everything hinges on the placement of the overlayview - this is where I have a breakdown in understanding. The main.xml, which is what the Main activity is set to (seen above) looks like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"/>
<com.trial.game.test.GameView
android:id="@+id/gamearea"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<RelativeLayout
android:id="@+id/gameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/score"
android:text="@string/score_text"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top"
android:textColor="#ff0000ff"
android:textSize="24sp"
android:layout_marginTop="3px"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/text"
android:text="@string/mode_ready"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#ff0000ff"
android:textSize="20sp"/>
</RelativeLayout>
Based on the placement of the gestureoverlay or the gameview one or the other will fire, but not both at the same time. What I am overlooking or misunderstanding here. Anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论