Matteo Cardellini
Matteo Cardellini

Reputation: 896

AndEngine gravity doesn't work

Hello everybody I'm trying to make a sprite fall in my game. I searched all over the web and I did this:

    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() {}
    });

But the when i launch the game the sprite doesn't fall !! Why ?? Please I'm desperate !!

Upvotes: 2

Views: 1031

Answers (1)

Aleksandrs
Aleksandrs

Reputation: 1509

Sprite doesn't use physics, but body does. You should use Physics connector (which is connecting your sprite with your body):

Set the body variable:

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

and then use this physics connector:

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

Sorry for my English.

Upvotes: 3

Related Questions