Henrique

Phaser Issues with Typescript - Group Body

Recommended Posts

Hi! I have a little issue or misunderstanding. I do not know yet, help me figure it out.

Look at the following code:

// inside state create

this.game.physics.arcade.gravity.y = 150;

// create a simple group with a sprite into
this.groupTest = new Phaser.Group(this.game);
this.groupTest.enableBody = true;
this.groupTest.add(this.game.add.sprite(0, 0, 'simon'));

// f5 here : OK - Noting happen

// or p2
this.game.physics.enable(this.groupTest, Phaser.Physics.ARCADE);
			
// f5 here : OK - group and sprite inside fall like a rock

// body undefined
this.groupTest.body.velocity.x = 0;

If I change the group by sprite it works, BUT, I need it to be with group.

I know that in pure javascript inject variables (like body), it would work without any problems. But I'm doing it in Typescript and in Phaser.Group has no body.

How to make it work?

thnks! =D

Share this post


Link to post
Share on other sites

Hi, body on group has no sense. Group is just collection of sprites, each of them can have body.

Calling enableBody on group does not set body for group, but says, that you want to automatically set body for all (new or added) sprites in group - from source:

    /**
    * If true all Sprites created by, or added to this group, will have a physics body enabled on them.
    *
    * If there are children already in the Group at the time you set this property, they are not changed.
    *
    * The default body type is controlled with {@link #physicsBodyType}.
    * @property {boolean} enableBody
    */
    this.enableBody = enableBody;

 If you need more complex body, you have to solve it on physics engine level - like creating complex collider or connecting bodies with joints. These options are not available in simple Arcade physics. Arcade physics is good for platform games or games where you are happy with AABB collisions.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.