Android 触摸不起作用。和引擎
Android Touch not working. Andengine
场景做的不错,就是触摸不行。我注册了所有精灵并制作了 onTouchAreaBindingOnActionDownEnabled(true)
。我曾尝试使用 MenuScene
中制造的 Andengine,但它也无法正常工作。这是我现在的代码。
我和这位先生有同样的问题here,因为我没有出路我把我的代码改成了:
import org.andengine.engine.camera.Camera;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import com.***.RM; //I changed the directory name to ***
import com.***.SceneManager; //I changed the directory name to ***
import com.***.SceneType; //I changed the directory name to ***
public class MainMenuScene extends BaseScene {
private final int MENU_PLAY = 0;
private final int MENU_OPTIONS = 1;
private final int MENU_CREDITS = 2;
private final int MENU_EXIT = 3;
final int w = 256, h = 64;
public class MenuSprite extends Sprite {
private int ID;
public MenuSprite(int pID, int pX, int pY, ITextureRegion tex, VertexBufferObjectManager vbom) {
super(pX, pY, w, h, tex, vbom);
this.ID = pID;
}
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
System.out.println("text");
if (pSceneTouchEvent.isActionDown() || pSceneTouchEvent.isActionMove()) {
onclick(this.ID);
this.setScale(1.5f);
return true;
} else {
this.setScale(1f);
}
return true;
}
}
@Override
public void createScene() {
attachChild(new Sprite(RM.MX, RM.MY, rM.menu_back, vbom) {
@Override
protected void preDraw(GLState pGLState, Camera pCamera) {
super.preDraw(pGLState, pCamera);
pGLState.enableDither();
}
});
final MenuSprite playMenuItem = new MenuSprite(MENU_PLAY, RM.MX, RM.CH * 4 / 6, rM.menu_play, vbom);
final MenuSprite optionsMenuItem = new MenuSprite(MENU_OPTIONS, RM.MX, RM.CH * 3 / 6, rM.menu_options, vbom);
final MenuSprite creditsMenuItem = new MenuSprite(MENU_CREDITS, RM.MX, RM.CH * 2 / 6, rM.menu_credit, vbom);
final MenuSprite exitMenuItem = new MenuSprite(MENU_EXIT, RM.MX, RM.CH * 1 / 6, rM.menu_exit, vbom);
attachChild(playMenuItem);
attachChild(optionsMenuItem);
attachChild(creditsMenuItem);
attachChild(exitMenuItem);
registerTouchArea(playMenuItem);
registerTouchArea(optionsMenuItem);
registerTouchArea(creditsMenuItem);
registerTouchArea(exitMenuItem);
setTouchAreaBindingOnActionDownEnabled(true);
}
@Override
public void disposeScene() {
}
@Override
public SceneType getSceneType() {
return SceneType.SCENE_MENU;
}
@Override
public void onBackKeyPressed() {
System.exit(0);
}
public void onclick(int ID) {
switch (ID) {
case MENU_PLAY:
SceneManager.getInstance().createLevelSelectionScene();
break;
case MENU_OPTIONS:
break;
case MENU_EXIT:
onBackKeyPressed();
break;
default:
break;
}
}
}
截图如下(我去掉了标题):
在 Logcat 中,我在触摸时得到以下信息:
04-02 22:29:31.909: I/InputReader(159): dispatchTouch::touch event's action is 0
04-02 22:29:31.909: I/InputDispatcher(159): Delivering touch to current input target
但我的应用程序过滤消息没有触摸日志,甚至没有我添加的 system.out
。
pOnCreateSceneCallback.onCreateSceneFinished(menuScene);
不存在。
这不会给出任何警告或错误,但会产生问题。
场景做的不错,就是触摸不行。我注册了所有精灵并制作了 onTouchAreaBindingOnActionDownEnabled(true)
。我曾尝试使用 MenuScene
中制造的 Andengine,但它也无法正常工作。这是我现在的代码。
我和这位先生有同样的问题here,因为我没有出路我把我的代码改成了:
import org.andengine.engine.camera.Camera;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import com.***.RM; //I changed the directory name to ***
import com.***.SceneManager; //I changed the directory name to ***
import com.***.SceneType; //I changed the directory name to ***
public class MainMenuScene extends BaseScene {
private final int MENU_PLAY = 0;
private final int MENU_OPTIONS = 1;
private final int MENU_CREDITS = 2;
private final int MENU_EXIT = 3;
final int w = 256, h = 64;
public class MenuSprite extends Sprite {
private int ID;
public MenuSprite(int pID, int pX, int pY, ITextureRegion tex, VertexBufferObjectManager vbom) {
super(pX, pY, w, h, tex, vbom);
this.ID = pID;
}
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
System.out.println("text");
if (pSceneTouchEvent.isActionDown() || pSceneTouchEvent.isActionMove()) {
onclick(this.ID);
this.setScale(1.5f);
return true;
} else {
this.setScale(1f);
}
return true;
}
}
@Override
public void createScene() {
attachChild(new Sprite(RM.MX, RM.MY, rM.menu_back, vbom) {
@Override
protected void preDraw(GLState pGLState, Camera pCamera) {
super.preDraw(pGLState, pCamera);
pGLState.enableDither();
}
});
final MenuSprite playMenuItem = new MenuSprite(MENU_PLAY, RM.MX, RM.CH * 4 / 6, rM.menu_play, vbom);
final MenuSprite optionsMenuItem = new MenuSprite(MENU_OPTIONS, RM.MX, RM.CH * 3 / 6, rM.menu_options, vbom);
final MenuSprite creditsMenuItem = new MenuSprite(MENU_CREDITS, RM.MX, RM.CH * 2 / 6, rM.menu_credit, vbom);
final MenuSprite exitMenuItem = new MenuSprite(MENU_EXIT, RM.MX, RM.CH * 1 / 6, rM.menu_exit, vbom);
attachChild(playMenuItem);
attachChild(optionsMenuItem);
attachChild(creditsMenuItem);
attachChild(exitMenuItem);
registerTouchArea(playMenuItem);
registerTouchArea(optionsMenuItem);
registerTouchArea(creditsMenuItem);
registerTouchArea(exitMenuItem);
setTouchAreaBindingOnActionDownEnabled(true);
}
@Override
public void disposeScene() {
}
@Override
public SceneType getSceneType() {
return SceneType.SCENE_MENU;
}
@Override
public void onBackKeyPressed() {
System.exit(0);
}
public void onclick(int ID) {
switch (ID) {
case MENU_PLAY:
SceneManager.getInstance().createLevelSelectionScene();
break;
case MENU_OPTIONS:
break;
case MENU_EXIT:
onBackKeyPressed();
break;
default:
break;
}
}
}
截图如下(我去掉了标题):
在 Logcat 中,我在触摸时得到以下信息:
04-02 22:29:31.909: I/InputReader(159): dispatchTouch::touch event's action is 0
04-02 22:29:31.909: I/InputDispatcher(159): Delivering touch to current input target
但我的应用程序过滤消息没有触摸日志,甚至没有我添加的 system.out
。
pOnCreateSceneCallback.onCreateSceneFinished(menuScene);
不存在。
这不会给出任何警告或错误,但会产生问题。