AndEngine运动不顺畅

时间:2013-06-14 22:20:49

标签: java android andengine

我正在测试AndEngine。我的代码动画两个正方形模拟重力和重力* 2加速度,一个手动,另一个用2DBox。 但是我看到了这个运动。这是正常的吗?我该如何解决?

public class MainActivity extends BaseGameActivity {

    public static int WIDTH = 480;
    public static int HEIGHT = 800;

    public Scene mScene;
    public PhysicsWorld mPhysicsWorld;
    public Body roofWallBody;

    private Body body;
    private final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(1f, 0f, 1f);

    @Override
    public Engine onCreateEngine(final EngineOptions pEngineOptions) {
        return new FixedStepEngine(pEngineOptions, 60);
    }

    @Override
    public EngineOptions onCreateEngineOptions() {
        EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED,
                new FillResolutionPolicy(), new Camera(0, 0, WIDTH, HEIGHT));
        engineOptions.getRenderOptions().setDithering(true);
        engineOptions.getRenderOptions().setMultiSampling(true);
        engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
        return engineOptions;
    }

    @Override
    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) {

        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) {
        mScene = new Scene();
        mScene.setBackground(new Background(0.9f, 0, 0.9f));
        pOnCreateSceneCallback.onCreateSceneFinished(mScene);
    }

    @Override
    public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) {

        mPhysicsWorld = new FixedStepPhysicsWorld(60, new Vector2(0f, SensorManager.GRAVITY_EARTH * 2), false, 3, 2);
        mScene.registerUpdateHandler(mPhysicsWorld);

        final FixtureDef WALL_FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 1f, 0.5f);
        final Rectangle floor = new Rectangle(0, HEIGHT - 3f, WIDTH, 3f, this.getVertexBufferObjectManager());
        roofWallBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, floor, BodyType.StaticBody, WALL_FIXTURE_DEF);
        this.mScene.attachChild(floor);

        IAreaShape box = new Rectangle(300f, 240f, 100, 100, mEngine.getVertexBufferObjectManager());
        body = PhysicsFactory.createBoxBody(mPhysicsWorld, box, BodyType.DynamicBody, boxFixtureDef);
        this.mScene.attachChild(box);
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(box, body));

        final Rectangle otherBox = new Rectangle(100, 100, 100, 100, getVertexBufferObjectManager()) {
            float velocity = 0;

            @Override
            protected void onManagedUpdate(float pSecondsElapsed) {
                super.onManagedUpdate(pSecondsElapsed);
                velocity += pSecondsElapsed * SensorManager.GRAVITY_EARTH;
                setPosition(getX(), getY() + velocity);
                if (getY() > HEIGHT) {
                    setPosition(getX(), 0);
                    velocity = 0;
                }
            }

        };
        otherBox.setColor(Color.YELLOW);
        this.mScene.attachChild(otherBox);

        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }

}

0 个答案:

没有答案