Jump to content

How to avoid Physics Body when creating a new Sprite?


Taleforge
 Share

Recommended Posts

Hi,

 

Iam creating a group of bullets with:

this.enemyBullets = this.game.add.group();this.enemyBullets.enableBody = true;this.enemyBullets.physicsBodyType = Phaser.Physics.P2JS;for (var i = 0; i < 100; i++) {    var bullet = this.enemyBullets.create(0, 0, 'bullet', 0, false);    bullet.body.setCollisionGroup(this.game.aa.collisiongroups.enemyFire);    bullet.body.collides(this.game.aa.collisiongroups.friend);    bullet.body.mass = 0.5;    bullet.anchor.setTo(0.5);    bullet.outOfBoundsKill = true;}

The sprite is not visible, but the physics body seems to be active. When i go to the coordinates 0,0 I collide with the invisible bullets.

 

How can I avoid this?

 

Link to comment
Share on other sites

I think i was explaining my problem a bit too bad. I want the Sprite to be invisible and have no body. Thats why i set the last parameter of

this.enemyBullets.create(0, 0, 'bullet', 0, false);

to false -> it says that the just created bullet should not exists. And that method does what I want. it creates a invisible sprite.

My only problem ist now, that the invisible bullets get a body, that seems to effect the world even if its sprite does not exist.

 

I want to create a group of Sprites that have physics, but i dont want them to be in the world at that moment. I will reset the bullets when an enemy fires - than the bullet should have a body that affects the world.

Link to comment
Share on other sites

When Sprite.exists is set to false its body is removed from the world. However I don't believe that the body existed in the world at the point Group sets it, I think it gets added in the next update. I'll need to do some tests to confirm, but you could try setting exists = false on all Group children but outside of your for loop. Actually it'd be interesting to stick a console.log(sprite.body.data.world) in there to see if that's been populated yet or not. But I've made a note to look into it more next week.

Link to comment
Share on other sites

Ok I have updated the way this works, so for a P2 Body is sprite.exists is false then it doesn't add the body to the world, but it still creates it. This means when you next do Sprite.exists = true it can just add it into the world and not spend time generating it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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