使用pshysics box 2d在AndEngine中跳转精灵

时间:2014-02-14 08:51:36

标签: android andengine

我有一个精灵,如何使用和引擎扩展PhsycicsBox2D进行跳转?

目前,我已经做了所有事情,但它没有跳过。

OnTouch 让它跳转

        @Override
        public boolean onSceneTouchEvent(Scene pScene,
                TouchEvent pSceneTouchEvent) {
            // TODO Auto-generated method stub
            if (pSceneTouchEvent.isActionDown())
            {

                b.setLinearVelocity(new Vector2(b.getLinearVelocity().x, -13.5f)); 


            }
            return false;
        }

玩家,物理世界,身体和连接器。

        player = new AnimatedSprite(playerX, playerY, this.mPlayerTextureRegion, getVertexBufferObjectManager());

            physicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
            mScene.registerUpdateHandler(physicsWorld);

            final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
            b = PhysicsFactory.createBoxBody(physicsWorld, player, BodyType.StaticBody, objectFixtureDef);

            physicsWorld.registerPhysicsConnector(new PhysicsConnector(player, b, true, true));

            mScene.attachChild(player);

2 个答案:

答案 0 :(得分:1)

每次在更新处理程序中都必须更新精灵位置,如下所示:获取世界中的所有实体现在获取正文的位置然后更新精灵位置

mScene.registerUpdateHandler(new Timer(1f, new ITimerCallback() {



                        @Override
                        public void onTick() {

                                Iterator<Body> it = bxWorld.getBodies();`
            while(it.hasNext()) {
                            Body b = it.next();
                            Object userData = b.getUserData();

                            if (userData != null && userData instanceof Sprite) {
                                //Synchronize the Sprites position and rotation with the corresponding body
                                final Sprite sprite = (Sprite)userData;
                                final Vector2 pos = b.getPosition();
                                sprite.setPosition(pos.x * PTM_RATIO, pos.y * PTM_RATIO);
                                sprite.setRotation(-1.0f * ccMacros.CC_RADIANS_TO_DEGREES(b.getAngle()));

                            }   
                        }
                        }

                }));

如果要检查实体可见性,请使用调试绘制类。

答案 1 :(得分:1)

尝试使用Pathmodifire

http://code.google.com/p/andengineexamples/source/browse/src/org/anddev/andengine/examples/PathModifierExample.java?r=4f9e33fa42af65ad61057787d03f2c08fc551b62

Pathmodifire会做的伎俩

将当前位置的坐标传递到下一个位置(跳转位置)