flowabuse Posted July 4, 2015 Share Posted July 4, 2015 I'm having an issue when using phsics.p2.enable on a sprite object. It shows me an error: Uncaught TypeError: Cannot read property 'set' of undefined. ( on object.anchor.set(0.5); ). Using physics.p2.enableBody works fine but I don't know if its the same as .enable, I don't want to have further problems. Here is the code: var Ball = function (game, x ,y ) { Phaser.Sprite.call(this, game, x, y); var graphics = game.add.graphics(0, 0); graphics.beginFill(0xffffff); graphics.lineStyle(1, 0x000000, 0.5); graphics.drawCircle(0, 0, BALL_RADIUS); graphics.endFill(); //this.anchor.setTo(0.5, 0.5); this.addChild(graphics); game.add.existing(this); game.physics.p2.enable(this, true); //game.physics.p2.enableBody(this, true); this works }; Ball.prototype = Object.create(Phaser.Sprite.prototype); Ball.prototype.constructor = Ball; Ball.prototype.update = function() { }; Link to comment Share on other sites More sharing options...
substandardgaussian Posted July 4, 2015 Share Posted July 4, 2015 Just look at the source, there's a link to the related property/function for all of the documentation in Phaser. p2.enable calls p2.enableBody. p2.enable is basically a wrapper around p2.enableBody that handles recursion/enabling multiple bodies in groups/arrays. What's actually happening is that you have an odd construction by having the graphics object be a child of the sprite. It's trying to enable a body on the graphics object and failing. That's why enableBody works: it doesn't try to recursively apply "enable" on the children of this. p2.enable does do that. It should be fine in theory, because the graphics object inherits from PIXI.DisplayObjectContainer, and the "children" of a DisplayObjectContainer is an array of DisplayObjectContainers, but P2 in particular seems to expect objects to have an anchor point, which is not a native property of a DisplayObjectContainer. You can usegame.physics.p2.enable(yourObject, debug, false);to tell P2 not to recursively make bodies on yourObject's children. You could just enableBody, but for consistency you should probably use the provided wrapper that interacts appropriately with groups/arrays. The fact that P2 assumes something wrong about an otherwise perfectly valid object should probably be considered a bug. There's no guarantee that the child nodes of a DisplayObjectContainer have an anchor point. flowabuse 1 Link to comment Share on other sites More sharing options...
Recommended Posts