Jump to content

Creating objects after collision


czajkovsky
 Share

Recommended Posts

Hey Guys,

 

I have huge problem with creating objects after collision.

 

In my create function I create some objects:

this.engine.coins.enableBody = true;this.engine.coins.physicsBodyType = Phaser.Physics.BOX2D;for (var i = 0; i <= 10; i++) {  _sprite = @engine.coins.create(@game.world.randomX, @game.world.randomY, 'coin');  _sprite.body.setCollisionMask(Spirit.COLLISIONS_MASKS.coin); // bitmask  _sprite.body.setCollisionCategory(Spirit.COLLISIONS_CATEGORIES.coin); // bitmask}

This code works well. Objects are created and they collide with player. Later in Player class I have something like this:

this.sprite.body.setCategoryContactCallback(Spirit.COLLISIONS_CATEGORIES.coin, this._coinCollision, this);

And _coinCollision is triggered correctly, but when I run

_sprite = this.engine.coins.create(this.game.world.randomX, this.game.world.randomY, 'coin')

I only get error:

box2d-plugin-full.js:34995 Uncaught TypeError: Cannot set property 'world' of null)

This line is:

34990: /**34991: * @property {box2d.b2Body} data - The Box2D body data.34992: * @protected34993: */34994: this.data = this.world.world.CreateBody(this.bodyDef);34995: this.data.world = this.world.world;34996: this.data.parent = this;

Any tips on this? Or maybe I'm doing something wrong from the beginning? What I'm trying to achieve is create new object (add object to group) on collision. Thanks a lot!

Link to comment
Share on other sites

  • 3 weeks later...

I had this problem as well and it was because the Box2d world is locked during callbacks. To resolve this I set some boolean to true in the _coinCollision function and then on the next update I respawned or created new objects as necessary and set the boolean back to false.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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