Jump to content

stage.removeChild() not working as expected


NITWIT
 Share

Recommended Posts

Hi, I'm just getting started with Pixi.

I made a little sprite that walks around and shoots things. Whenever I bump my sprite into the bad guys, I want him to disappear. So I ran stage.removeChild(mySprite) and he disappeared!

But... He still shoots when I click and he still moves around with WASD. So it just makes the sprite invisible? 

Here's a link: http://www.med.wadholm.com/Kaitlyn/sprites/pixi/bunny/

 

Link to comment
Share on other sites

PIXI works like its supposed too. 

I cant even count the number of problems with your code that are not related to pixi.

1) shoot() method is called even if player is dead.

2) looks like gameloop() adds keyboard event listeners every frame

3)... just find someone to explain you all the problems in "coding and design" section

If you really think that there's a bug with pixi, make a clean example that reproduces that bug but has only 10-30 lines of code.

Link to comment
Share on other sites

.Thanks Ivan,

I know my code is a mess. :blink: I'll clean it up and I imagine that will fix a lot of bugs.

To be clear, I don't think there's a bug with Pixi -- it's definitely on my end. I guess I'm just wondering what the removeChild() function does exactly. 

For example, If I create a sprite and give it an x position.

 mySprite.x =100;

And then I remove the sprite.

stage.removeChild(mySprite);

And then I ask for mySprite's x position.

console.log(mySprite.x);

Would the browser still know the x position? Would it print 100 to the console? or undefined maybe?

Does the browser keep data about a sprite even after you remove it? 

Sorry if these are really dumb questions. Maybe there's a Pixi newb section I should post to instead. Thanks!

Link to comment
Share on other sites

It's not a dumb question, don't worry. What you need to know is that a sprite will still exist as long as there is a variable that references it that remains in scope somewhere. So you might remove it from the display list but if there is a reference to the sprite then it still exists, and its x property still has a value of 100. It's just the same as any other javascript object. So yes, you can still console log its properties if you have a reference to it, even after it has been removed from the stage.

Link to comment
Share on other sites

mySprite is just JS object that stores its transform (position, scale, rotation) and texture. You can add it to stage and it'll be rendered, you can remove it, you can add it again, but those variables exist anyway. PIXI objects have special method destroy() but in your case after the destroy everyting will just crash when user clicks, because you dont have any checks that object is alive.

You can at least check "sprite.parent !== null" , please consult pixi docs about "parent" variable.

Don't worry, you'll get everything eventually :)

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.

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

Loading...
 Share

  • Recently Browsing   0 members

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