Strict Mode and Casting in Flash Actionscript 3

Today I came across a programming error when I tried to find the z-index of the movieclip (my_mc) when it is being clicked. Below was my code:

my_mc.addEventListener(MouseEvent.CLICK, doClick);
function doClick(e:MouseEvent):void
{
 trace (getChildIndex(e.target)); // throw a compiled time error
}

When I tested the movie, Flash returned a complied time error- 1118:Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.

 I was scratching my head and couldn’t quite figure out what when wrong. It took me a while to realise that Flash was giving me the error because the complier didn’t know that e.target in this case was indeed a movieclip. When I turned off the Strict Mode error warning at the Actionscript 3 Settings at the Publishing Settings page, Flash compiled the movie successfully and everything ran fine.

Naturally I was not satisfied having to ignore the error in order to run the movie. So I did a little more research and found that I could explicity type cast an object if the compiler didn’t know what the object type was. There were bascially two ways to solve the above problem. Instead of:

trace (getChildIndex(e.target));

Replace that with:

trace (getChildIndex(e.target as MovieClip));

Or with:

trace (getChildIndex(MovieClip(e.target)));

 So now I have the proper code that meets the strict typing requirement and can be compiled successfully. 🙂

Advertisements

~ by brianchau on December 7, 2007.

14 Responses to “Strict Mode and Casting in Flash Actionscript 3”

  1. worth noting that you get 2 different results with the 2 different methods of casting, if the cast fails.

    Using the ‘as’ keyword the result of the cast would be null and the movie would continue to run. Using a standard cast the player would throw an error.

  2. Thanks Tink. Good point.

  3. very helpful. thank you.

  4. Googled “flash error 1118”, this is the exact answer I was looking for. How to do an explicit typecast. Thank you!

  5. A great help even after almost one year. Thank you 🙂

  6. I also received this error when trying to apply e.target as a mask to another mc: map.mask = e.target; which I fixed after reading your example as map.mask = MovieClip(e.target);

    Thanks

  7. Thank you so much I was exactly in the same situation !!!

  8. Thanks! Had this same problem and casting did the trick. One question, does it slow down your movie? I’m calling an update() function on an object every frame and since it’s a subclass I had to cast it…

  9. hmm this helps me, but then again, it doesn’t 😦 When I run the code shown below, I get an argumenterror #2025… Can you help me? Thanks

    function openPagedoneLoading(e:Event):void {
    countOpenedPages = 0;
    var page:MovieClip = new MovieClip();
    page = MovieClip(openPageLoader.content);
    openPageLoader.unload();
    stage.addChild(page);
    page.x = whichItemClicked * 200;
    page.addEventListener(MouseEvent.MOUSE_DOWN, onMovePage);
    page.addEventListener(MouseEvent.MOUSE_UP, onMovePageStop);
    trace(page.name);
    countOpenedPages++;
    }

    function onMovePage(e:MouseEvent):void {
    setChildIndex(e.target as MovieClip), numChildren – 1);
    e.target.startDrag();
    }

  10. Thanks Brian — I was battling with a 1118 error and this got me out of trouble on a current project.

  11. Lovely tip!! Faced a lot of problems with that error but didn’t know such a solution existed!!!

  12. No still you gets error, when click on the button instead of MovieClip Used This syntax :
    trace (getChildIndex(SimpleButton(e.target)));

  13. thanks it works!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: