0

I'm new to AndEngine and this really first attempt at using a touch event with game physics. I created this block of code following the mybringback AndEngine tutorial series. I created a sprite that simply bounces up and down on the ground. What I want to do is integrate a touch event so that the user may pick up the sprite (place it anywhere) then have it fall to the ground.

I have a sprite that bounces up and down, so that's good but my on touch event doesn't work. I did a little research for onAreaTouched but I think I'm still not understanding some of the concepts. If someone could tell me what I'm doing wrong that would be much appreciated.

Here is my 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();

}
4

1 回答 1

2

乍一看,您的触摸事件看起来是正确的——对于没有物理扩展的游戏。

在物理学中,您不想触摸/移动精灵本身,而是想要移动底层(不可见)物理体 - 由物理引擎处理的物理体,它对碰撞做出反应负责移动精灵取决于在物理上。

所以,你正试图通过移动你的精灵而不是你的身体来改变它。移动你的身体看看鼠标关节:

于 2013-07-25T22:28:20.890 回答