Copy link to clipboard
Copied
In the code below I have several MovieClips that are all TheBeetle(). They are in another MovieClip called gamelevel and also pushed in an array called bArray. previously I have indexed them in the gamelevel but after the event listener is called I cannot index them anymore and receive the error "1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.". As the user clicks them, they die (and change the frame) and the dead body goes under other alive bodies, thats the reason I need to index them to 1 as they die. I understand what the error says but how can i do what i need to do?
The code works just fine but it wouldn't in the two lines i've mentioned in it, so take a look please:
public function clicked (event:MouseEvent)
{
if (event.target is TheBeetle && event.target.currentFrame <= 2)
{
var mc:Object = event.target
// TheBeetle is actually a MovieClip but i cannot write: var mc:MovieClip = event.target, if i do i receive 1118
if (mc.currentFrame == 1)
{
mc.gotoAndStop (Math.floor(Math.random() * 3 + 4));
}
else
{
mc.gotoAndStop (3);
}
mc.filters = null;
// Here i need to index the TheBeetle as i did before like gamelevel.setChildIndex(mc,1) but i'd receive 1118!
bArray.splice (bArray.indexOf(mc),1);
if (bArray.length == 0)
{
removeEventListener (Event.ENTER_FRAME,frameHandler);
waveTimer.removeEventListener (TimerEvent.TIMER_COMPLETE, changeLocation);
}
}
}
cast it:
var mc:MovieClip=MovieClip(event.target);
Copy link to clipboard
Copied
cast it:
var mc:MovieClip=MovieClip(event.target);
Copy link to clipboard
Copied
I LITERALLY CANNOT LIVE WITHOUT YOU:D U R A LIVING ANSWERING MACHINE
for (var i:int = 0; i <= stars; i++)
{
THANKS MAN
}
Copy link to clipboard
Copied
and can u explain why should i define it as a movieclip while it already is? thx
Copy link to clipboard
Copied
you're welcome.
and you're correct. it's not clear why flash should have to be told your event.target is a movieclip, but it does.
flash easily loses track of object types. the simplest example is using 'root'. under no circumstance (that i've found) can flash remember that 'root' is a movieclip. (it can be a sprite if you create a document class that extends sprite, but otherwise it's a movieclip.)
anyway, i assume it would make flash slower and use more memory if it were to keep track of object types so that might be a reason why flash frequently needs help remembering. but i don't really know if that's true.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now