Actionscript 3: setChildIndex
- Started
- Last post
- 9 Responses
- CyBrain
This one is so simple I can't believe it won't work.
Here are my two lines of code. (Actually 1, The first one is just a trace)
http://pastebin.com/f7ed21e46setChildIndex is supposed to work with two parameters: a display object and an integer for depth in the display stack. My object (e.target) traces to one my images that I want to move to the top,
So, what's with the type coercion error.
1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.
- CyBrain0
This code may be easier to follow.
http://pastebin.com/m366af2d8
- Autokern0
What is happening basically is:
the event.target gives you a generic object while to use the setChildIndex you need to use a display object.
So, it's about recasting the event.target, that you know to be a display object as one.
- CyBrain0
Once I put
import flash.display.DisplayObject;
at the top of my class, that worked. Thanks. I thought MovieClips were considered DisplayObjects.
- CyBrain0
I think Actionscript 3 is racist. There's a lot of profiling going on.
- Autokern0
They are, but again, event.target returns a generic object
so whatever you do inside a event handling function using event.target
flash considers it as a generic object, throwing a coercion error.
You have to cast the event target to his proper type to work with it properly.
- maximillion_0
try event.currentTarget
- dparra0
try this, thos should work:
this.setChildIndex(e.target as DisplayObject, this.numChildren - 1);
- era4O40
yeah. use e.currentTarget
or cast it: DisplayObject(e.target).You don't need a Display Object, though. SetChildIndex takes any Sprite.