【问题标题】:Using onAreaTouched with physics - AndEngine将 onAreaTouched 与物理结合使用 - AndEngine
【发布时间】: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


    【解决方案1】:

    乍一看,您的触摸事件看起来不错 - 对于没有物理扩展的游戏。

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

    所以,您正试图以错误的方式移动精灵而不是身体。移动你的身体看看鼠标关节:

    【讨论】:

    • 谢谢。这看起来很有趣。在 Android 中,我能够通过一个简单的触摸事件来移动图像,并且我认为在 AndEngine 中这将是相同的想法。我不知道这是一个完全不同的概念。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多