Okay - it's not often I post on forums at all for help - let alone three times recently - but I really appreciate the help here!
As the title says - I've added a child (which is class called "Target" and is an extended Sprite) straight to the main object (entry point). I've also made it somewhat of a Singleton object (the main object, that is) by making a static reference and a GetReference() function to call the main object.
1 - The singleton appears to be working fine - a call to Main.GetInstance().StatusMessage("Test"); works absolutely fine, and outputs to the screen as it should
2 - I've tried, from the child, using this.parent.removeChild(this); - it failed
3 - I've tried:
var d:flash.display.DisplayObject = Main.GetInstance().getChildByName(this.name);
trace(d);
Main.GetInstance().removeChild(d);
d traces as NULL, and of course removeChild fails.
So - how is a child supposed to kill themselves without external involvement? Surely this is something that is required incredible frequently?!
I also thought that was the most elegant way of doing it, but alas it has failed.
Following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at GameGraphics.Gui::Target/::TimerEventFunction()
at flash.utils::Timer/flash.utils:Timer::_timerDispatch()
at flash.utils::Timer/flash.utils:Timer::tick()
trace(this.parent); gives "null".
The object was added with this.addChild(m_Target); from the main object.
Try the following. When you add the child create a reference variable like
var myClip:Object = this;
Later when you want to remove the child use the reference variable instead of using "this". I noticed that not always the reference within a function is to the class itself. For example I got this trace within an eventhandler:
[object global]//for "this"
[object SearchModulLocal]//which is the class itself for myClip, which was declared earlier.
It may solve your problem but the problem could be different, too.
- The right of the People to create Flash movies shall not be infringed. -
The "Target" object is an extended GameSprite object, which is an extended "Sprite" object with a "Loader" object within it (that is, the Loader object is a child of the Sprite object)... Would that cause the problems? Could it be looking at the wrong part?
cancerinform - where would I do that? Within the object itself? The whole point is to remove the object without the parent needing to do it themselves ...
This question was asked before on another forum, and I made the enclosed self-deleting class and fla. Not sure why it worked for me and it doesn't/won't work for you, but here it is.
Is there a difference between Sprite and MovieClip with regards to this?! I've seen a couple uses of MC's and self deleting but assumed that they'd be almost identical in this aspect?
Thanks for uploading that file - I'm looking at it now! Appreciated!
Please, PLEASE accept my apologies - but I think the answer is embarrassment enough!
The delete code is being called by a Timer - a Timer object which is a child of the object being deleted. Foolishly assuming that the Timer object would also stop and be destructed upon removeChild(), the Timer still fired off and was trying to delete it after the initial delete worked perfectly well.
So - will delete this; remove child objects (even if they're created with the "new" operator)?
I had another equally annoying problem a couple of days ago - the code worked something like this:
Connect to Xxx.xxx.xxx.xxx;
OnConnent, go to "IsConnected()";
Socket.Close();
Socket.close(); was *right* at the end of the main function - so of course, Main() was executing, closing the socket, and then an error occured when I tried to read the socket.
Just an additional proof that I'm about as bright an an emo-bulb :-)
I am using that for a button of a Alertbox. However, when you create the child to remove you need to give a name. And regarding parent.parent I don't know how many parents to add. You know better
- The right of the People to create Flash movies shall not be infringed. -
Thank you for all your help! It seems to be working properly now - although, I now have a different problem (!) - the image doesn't display *if* I create the object directly 'on the stack' (ie, this.addChild(new Target()); ) - but I don't know if this is due to the object still not having loaded the image, as each Target() loads a file using an internal loader. Somehow, I need to separate this logic so the Loader (or any containing Bitmap type) uses an already-loaded object!
Fun! But, to be honest, although I'm struggling with certain 'easy' tasks, the overall language has been very pleasant to work with, given that my experience amounts to just a few days of coding and I'm already working on my 'big project' :-) Really appreciate the help, in the future I'm sure I'll be lurking around to offer my own advice!!