NITWIT Posted September 26, 2017 Share Posted September 26, 2017 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/ Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted September 26, 2017 Share Posted September 26, 2017 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. Quote Link to comment Share on other sites More sharing options...
NITWIT Posted September 26, 2017 Author Share Posted September 26, 2017 .Thanks Ivan, I know my code is a mess. 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! Quote Link to comment Share on other sites More sharing options...
alex_h Posted September 26, 2017 Share Posted September 26, 2017 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. NITWIT 1 Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted September 26, 2017 Share Posted September 26, 2017 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 NITWIT 1 Quote Link to comment Share on other sites More sharing options...
NITWIT Posted September 26, 2017 Author Share Posted September 26, 2017 Ahhh I see. I didn't realize the object would still be there after I removed the sprite. That's good to know. Thanks! 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.