Jump to content

Adding Physics to Graphics Object


Jfault
 Share

Recommended Posts

Hi everyone!

I'm using Phaser for a school projects, and our team needs a lot of rectangles of varying sizes and colors, but they need to have collision. I've tried creating a Graphics object and adding it as a child of a sprite. The sprite.addChild function works without a hitch. However, whenever I try to enable physics on the sprite, I get an error: (all the objects render, (the player, the rectangle, etc) but nothing moves)

 

3dee3f0def.png

Here is the code in question:

walls = this.add.graphics(0,0);walls.boundsPadding = 0;walls.beginFill(0xFFCCAA,  1);walls.drawRect(0,game.world.height-30,game.world.width,30);con = this.add.sprite(0,0,null);con.addChild(walls);game.physics.enable(con, Phaser.Physics.ARCADE);con.body.immovable = true;

If I comment the last two lines out, the player moves as expected, though it does not collide with the platform.

What's up, and how can I fix this?

Thanks.

Link to comment
Share on other sites

Well I am not sure exactly how to help you since it is showing an error on a piece of code that you didnt post.... but if you comment out those last two lines and the code works there could be an error there as well.

 

Try changing

 

  • game.physics.enable(con, Phaser.Physics.ARCADE);

to

 

game.physics.arcade.enableBody(con);

 

as for collisions you need to  update method

 

// in update

 

game.physics.arcade.collide(con, whatever other sprite you want it to collide with);

 

 

let me know if this helped.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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