zurcxer Posted March 21, 2014 Share Posted March 21, 2014 Hi! I'm a newbie in HTML5 and panda.js. How will I remove a sprite on collide. I am studying your sample game Flying dog, and I would like to place an image on your goalBody with a Sprite. (Just like what you did on topSprite and bottomSprite). But I am having an error if I add this code on collide.this.goalSprite = new game.Sprite(game.system.width + this.width / 2 + this.width + game.scene.player.body.shape.width, topHeight + this.height / 2, 'media/someimage.png', { anchor: {x: 0.5, y: 0.5}, });game.scene.goalContainer.addChild(this.goalSprite);this.goalBody.collide = function() { game.scene.world.removeBody(this); game.scene.goalContainer.removeChild(this.goalSprite); //this is the line I added game.scene.addScore(); return false; };Error:Uncaught Error: undefined The supplied DisplayObject must be a child of the caller [object Object] Am I doing it right? Or is there better approach on this? BTW, Panda.js is really amazing... Thanks, Quote Link to comment Share on other sites More sharing options...
enpu Posted March 21, 2014 Share Posted March 21, 2014 Your problem is that this inside your collide function is not referencing to the class. One solution would be to bind the collide function into classes own function, like:Player = game.Class.extend({ init: function() { game.scene.container.addChild(this.sprite); this.body.collide = this.collide.bind(this); }, collide: function() { game.scene.container.removeChild(this.sprite); }});Does that make sense to you? zurcxer 1 Quote Link to comment Share on other sites More sharing options...
zurcxer Posted March 21, 2014 Author Share Posted March 21, 2014 Works great! But how do you check if the container still has its child. In pixijs they say its like var isChild = Stage.contains(displayObject) Thanks, Quote Link to comment Share on other sites More sharing options...
zurcxer Posted March 21, 2014 Author Share Posted March 21, 2014 Hi enpu! I add the following lines on your sprite.js (Container class). I dont know if contains method exist in other class. /** * Check if children exist */ contains: function(obj){ return (this.children.indexOf( obj ) !== -1); }But its not 100% working sometimes I'm receiving an error. I think it occurs when calling the removeChild() method twice. Uncaught Error: [object Object] The supplied DisplayObject must be a child of the caller [object Object] Quote Link to comment Share on other sites More sharing options...
enpu Posted March 21, 2014 Share Posted March 21, 2014 You can check if sprite has parent with:if(this.sprite.parent) this.sprite.parent.removeChild(this.sprite); Quote Link to comment Share on other sites More sharing options...
zurcxer Posted March 21, 2014 Author Share Posted March 21, 2014 Awesome... Thank you for your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.