而发动机的重力不起作用

时间:2012-11-05 00:32:25

标签: android andengine gravity

大家好,我正在努力让精灵陷入游戏中。我搜索了整个网络,我做了这个:

    scene = new Scene();

    main = new Sprite(sX, sY, mainTextureRegion);
    main.setScale(1);
    main.setFlippedHorizontal(true);
    scene.attachChild(main);
    mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
    final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
    final Body body = PhysicsFactory.createBoxBody(mPhysicsWorld, main, BodyType.DynamicBody, objectFixtureDef);
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(main, body, true, true));
    final Vector2 gravity = new Vector2(0, 5f);
    mPhysicsWorld.setGravity(gravity);


    scene.registerUpdateHandler(new IUpdateHandler() {
        @Override
        public void onUpdate(float pSecondsElapsed) {
            mPhysicsWorld.onUpdate(pSecondsElapsed);
        }

        @Override
        public void reset() {}
    });

但是当我启动游戏时,精灵不会掉下来!为什么?请我绝望!!

1 个答案:

答案 0 :(得分:3)

Sprite不使用物理,但身体确实如此。您应该使用物理连接器(将您的精灵与身体连接):

设置正文变量:

Body yourBody = PhysicsFactory.createBoxBody(mPhysicsWorld, main, BodyType.DynamicBody, objectFixtureDef);

然后使用此物理连接器:

mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(main, yourBody, true, true));

抱歉我的英文。