如何用速度在And-engine中移动Object

时间:2015-04-08 09:51:18

标签: android box2d andengine

我正在开发一个游戏。我需要移动对象或精灵与静态身体附加到它。我可以移动它。因为我有四面墙,所以马对象不要移出容器。墙是我也是静态的身体。我想同时旋转和移动物体。到目前为止,我已经完成了旋转对象,但尚未完成移动。这就是我如何旋转它的代码`

@Override
    protected Scene onCreateScene() {
        scene = new Scene();
        this.mphysicworld = new PhysicsWorld(new Vector2(0,
                SensorManager.GRAVITY_EARTH), false);
        float pX = (CAMERA_WIDTH - Iregion_blackEgg.getWidth());
        sp_blackEgg = new Sprite(pX - 50, 50, this.Iregion_blackEgg,
                getVertexBufferObjectManager());

        Body eggBody =PhysicsFactory.createBoxBody(this.mphysicworld, sp_blackEgg,
                BodyType.DynamicBody, FIXTURE_DEF);
        this.mphysicworld.registerPhysicsConnector(new PhysicsConnector(
                sp_blackEgg, eggBody, true, true));

        eggBody.setLinearVelocity(pX, pY);

        RectFunction();

        final Vector2 gravity = new Vector2(0, 0f);
        mphysicworld.setGravity(gravity);

        scene.registerUpdateHandler(new IUpdateHandler() {

            @Override
            public void reset() {

            }

            @Override
            public void onUpdate(float pSecondsElapsed) {

                final float rotationOffset = pSecondsElapsed
                        *rotationIncrementalFactor;
                // if(i==100){

                RotateFunct(rotationOffset);
                // }else{
                // i=i+1;
                // }

                // this.onUpdate(pSecondsElapsed);
                  mphysicworld.onUpdate(pSecondsElapsed);
            }
        });

        scene.attachChild(sp_blackEgg);

        return scene;
    }

    public void RectFunction() {

        final Rectangle Right = new Rectangle(0, 0, CAMERA_WIDTH, 10f,
                this.getVertexBufferObjectManager());
        Right.setColor(org.andengine.util.color.Color.GREEN);
        Body myBodyRight = PhysicsFactory.createBoxBody(this.mphysicworld, Right,
                BodyType.StaticBody, FIXTURE_DEF);

        final Rectangle Top = new Rectangle(0, 0, 10f, CAMERA_HEIGHT,
                this.getVertexBufferObjectManager());
        Top.setColor(org.andengine.util.color.Color.GREEN);
        Body myBodyTop = PhysicsFactory.createBoxBody(this.mphysicworld,
                Top, BodyType.StaticBody, FIXTURE_DEF);

        final Rectangle Bottom = new Rectangle(CAMERA_WIDTH-10, 0, 10f, CAMERA_HEIGHT,
                this.getVertexBufferObjectManager());
        Bottom.setColor(org.andengine.util.color.Color.GREEN);
        Body myBodyBottom = PhysicsFactory.createBoxBody(this.mphysicworld,
                Bottom, BodyType.StaticBody, FIXTURE_DEF);


        final Rectangle Left = new Rectangle(0, CAMERA_HEIGHT-10, CAMERA_WIDTH, 10f,
                this.getVertexBufferObjectManager());
        Left.setColor(org.andengine.util.color.Color.GREEN);
        Body myBodyLeft = PhysicsFactory.createBoxBody(this.mphysicworld,
                Left, BodyType.StaticBody, FIXTURE_DEF);


        scene.attachChild(Right);
        scene.attachChild(Top);
        scene.attachChild(Bottom);
        scene.attachChild(Left);

    }

    public void RotateFunct(float rotationOffset) {

        sp_blackEgg.setRotation(sp_blackEgg.getRotation() + rotationOffset);
eggBody.applyForce(0f, 2000f,
            eggBody.getWorldCenter().x,
            eggBody.getWorldCenter().y);
    eggBody.applyLinearImpulse(0f, 200f,
            eggBody.getWorldCenter().x,
            eggBody.getWorldCenter().y);
    eggBody.setLinearVelocity(0f, 20f);
    eggBody.applyTorque(2000f);
    eggBody.applyAngularImpulse(20f);
    eggBody.setAngularVelocity(10f);

    }

`

0 个答案:

没有答案