czajkovsky Posted June 7, 2015 Share Posted June 7, 2015 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 More sharing options...
rich Posted June 8, 2015 Share Posted June 8, 2015 Trace out "this" from within _coinCollision - I'd be curious to know what object you get back, as the error is a typical JS scope one, so I'd check to see what the scope actually is. Link to comment Share on other sites More sharing options...
kandizzy Posted June 24, 2015 Share Posted June 24, 2015 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 More sharing options...
Recommended Posts