1 person found this helpful
Not an easy subject to grasp due to the differences between typical class based coding and prototype based coding. Much like back in AS2.0, scope is your issue.
When you setTimeout(), you run from 'window' scope, not from the scope you think you're running from (inside that frame_0 function). Therefore your 'name' var no longer exists if you dynamically created it there.
If you create yourself a way to manage references (so you don't pollute the global scope) you can get a reference to this object at any time. The reason it works on a mouse click is because that click operates at object scope. In other words when you click on the object on the canvas, "this" returns back to that objects scope. So saying stage.removeChild(this);, the "this" refers directly to what you clicked.
Code seems to work better if you don't understand prototype scope so here's a quick and dirty example of me escaping the encapsulated scope issue by directly creating a new 'ball' object from my library at the top most level (global), which is 'window':
New HTML5 Canvas document, object in library with linkage set to 'ball':
// aquire new 'ball' from library linkage, assign to global (window)
// variable 'myBall', as addChild returns the DisplayObject added
window.myBall = stage.addChild(new lib.ball());
// display on stage (stage is inherited from window)
// set timeout in this context but function will run from
// the window context, not this context (a function inside
// the frame_0 function inside lib.AddRemove)
// I am running from window scope here, luckily I
// created myBall at this scope so I can access it
Because I saved myBall to 'window', I have global access to it. While this works, this isn't a good idea, it just proves the scope issue.
When you use CreateJS directly the scope you create objects from is created by you yourself so you know how to access items. I myself am a bit confused on the scope in which Flash wishes to operate.