Jump to content

Removing Sprite on Collide


Recommended Posts



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...





Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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