Jump to content

[Phaser/Box2D] Setting Body to Sprite, the applying continuous movement


Recommended Posts

I have been working with Phaser for about 6 months now, and have recently purchased the Box2D plugin, and for the most part its great, although I am having a few basic problems with spawning the sprite with an appropriately sized body, then applying a continuous velocity.

Below is the offending function. First a sprite is spawned from a loaded atlas (the zombie), but unfortunately the sprite is way too big (or my game is too small, one of them) and needs to be scaled down to a 1/4 of its original size. I found a question elsewhere on here where someone (it may have even been Rich) gave an answer to define the body separately, then reattach it to the sprite and the sprite to it. This appears to work fine-ish, giving me a body that is registered as the sprite's and collides properly etc etc.

On spawn the zombie plays an anim of pulling itself out of the ground (the spawn anim), and then when that is completed it should play the walk animation, then actually move. The commented section within the OnComplete callback shows all of the different methods of getting something to move in Box2D the internet can give me, but none of them work.

function spawnZombie(x, y){
    var zombie = zombies.create(x, y, 'zombie');
    zombie.anchor.setTo(0.5, 0.5);
    zombie.scale.setTo(0.25, 0.25);

    var zomBody = new Phaser.Physics.Box2D.Body(this.game, null, x, y, 0.5);
    zomBody.setRectangle(30, 50, 0, 0, 0);
    zombie.animations.add('spawn', Phaser.Animation.generateFrameNames('appear/appear_', 1, 11, '', 1), 7, false);
    zombie.animations.add('walk', Phaser.Animation.generateFrameNames('walk/go_', 1, 10, '', 1), 7, true);
    zomBody.sprite = zombie;
    zombie.body = zomBody;
        //zombie.body.velocity.x = -100;
        //zomBody.velocity.x = -100;
    }, this);

Personally I think that the problem lies with the body being set as it is, as this method feels like a buggy workaround to me. The only symptom of the problem I can see within the sprite object is that setting the velocity actually does nothing, whereas elsewhere in the code it works as expected.

I wouldn't be exaggerating if i said i had spent 6 hours on this problem, and I'm about 2 hours away from quitting my job forever and ritually burning my PC.

Any help at all would be great, cheers.

Link to comment
Share on other sites

I would still enable the zombie sprite for Box2D physics (using the normal method of physics.box2d.enable(sprite)), but then I'd just call setRectangle after it, so it gets set to the size you require.

Box2D bodies have anchors set to 0.5, and calling setRectangle is safe because it'll clear the fixtures for you then just re-create them.

Once done you can then use sprite.body.moveLeft, etc as normal.

Link to comment
Share on other sites

Works Perfectly, of course. Cheers Rich!

Its always something that annoyingly small on a long problem like this. Even worse I did manage to get it working directly after submitting the question, and found that the zombie moves perfectly when setting the body as a kinematic object, although you're answer is obviously way better.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...