【发布时间】:2013-07-25 21:57:30
【问题描述】:
我是 AndEngine 的新手,这是第一次尝试将触摸事件与游戏物理结合使用。我按照 mybringback AndEngine 教程系列创建了这个代码块。我创建了一个简单地在地面上上下弹跳的精灵。我想要做的是集成一个触摸事件,以便用户可以拿起精灵(将它放在任何地方)然后让它掉到地上。
我有一个上下弹跳的精灵,这很好,但我的触摸事件不起作用。我对 onAreaTouched 做了一些研究,但我认为我仍然不理解一些概念。如果有人能告诉我我做错了什么,那将不胜感激。
这是我的 onPopulateScene:
@Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
final Sprite sPlayer = new Sprite(CAMERA_WIDTH / 2,CAMERA_HEIGHT / 2,
playerTexureRegion, this.mEngine.getVertexBufferObjectManager()){
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y)
{
//Not sure if I'm doing this right
if (pSceneTouchEvent.isActionUp())
{
this.setPosition(X, Y);
}else if(pSceneTouchEvent.isActionDown())
{
this.setPosition(X, Y);
}else if(pSceneTouchEvent.isActionMove())
{
this.setPosition(X, Y);
}
return true;
};
};
sPlayer.setRotation(45.0f);
final FixtureDef PLAYER_FIX = PhysicsFactory.createFixtureDef(10.0f,
1.0f, 0.0f);
Body body = PhysicsFactory.createCircleBody(physicsWorld, sPlayer,
BodyType.DynamicBody, PLAYER_FIX);
//Set touch Area here
this.scene.registerTouchArea(sPlayer);
this.scene.attachChild(sPlayer);
physicsWorld.registerPhysicsConnector(new PhysicsConnector(sPlayer,
body, true, false));
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
【问题讨论】:
标签: android andengine game-physics touch-event