ADR Posted November 7, 2016 Share Posted November 7, 2016 Hey everybody, First of all big thanks to Pixi.js people for the awesome library. I have been using Pixi to develop a simple game and I stumble upon a small bug or I may be doing this wrong. So here is the problem, I got a sprite which contains child's sprites. And I want to destroy it but not the children since I was handling it manually because I need to save the state of each child sprite and then call destroy within them self. So before I calling destroy method I call a custom method on my object, deletingObject.onDestroy(); and then call deletingObject.destroy({ children: false, texture: false, baseTexture: false }); but this still destroys all the child within this object, I'm using https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.1.1/pixi.min.js as my Pixi version. and I have attached a screen shot from core developer tools. Am I doing the function call wrong or is there something missing. Thanks. Quote Link to comment Share on other sites More sharing options...
xerver Posted November 7, 2016 Share Posted November 7, 2016 I think this may just be your sourcemaps messing up, according to your screenshot `destroyChildren` is false, and `i` is undefined. Meaning you are most likely *not* executing the highlighted line. If `destroyChildren` is false, it would never get past that IF statement. Quote Link to comment Share on other sites More sharing options...
ADR Posted November 7, 2016 Author Share Posted November 7, 2016 Thanks for the quick reply, Yes you were correct, sourcemaps was messing up the debugs. I found the problem, First of all, I thought when call deletingObject.destroy({ children: false, texture: false, baseTexture: false }); All the children will be still visible on the stage, but little bit digging make me realise that when you delete the parent all the child objects will have null parents hence it won't show on the stage. so it wasn't any bug just me been not fully understanding the scene graph. Anyway, I manage to fix my problem simply by getting all children and setting their parent to the current grandparent. onDestroy = function () { // Get all the children for (var i = 0; i < this.children.length; i++) { // Reset the parent this.children[i].parent = this.parent; } // Destroy this object this.destroy({ children: false, texture: false, baseTexture: false }); } Quote Link to comment Share on other sites More sharing options...
xerver Posted November 7, 2016 Share Posted November 7, 2016 Don't directly assign parent, use the addChild methods of the parent. Otherwise, those objects wont exist in the parent's `children` array and you will have issues. Additionally, your code (when calling destroy) will reset the parent of the children to `null`. The parent property is marked as readonly for a reason Do this instead: var children = this.removeChildren(); for (var i = 0; i < children.length; ++i) { this.parent.addChild(children[i]); } this.destroy(false); That will properly remove the children, properly add them to the parent, then destroy without affecting children/texture/basetexture. ADR 1 Quote Link to comment Share on other sites More sharing options...
ADR Posted November 7, 2016 Author Share Posted November 7, 2016 Thanks for the update mate. I will use this 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.