05.01.08

Flash CS3 / Flex 2 AS3 Error #1118

Posted in Error, Error, errors, Flash, Flex at 9:39 pm by Curtis J. Morley

ActionScript Error #1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject

ActionScript Error #1118 can be caused by working with the Display Object. What happens is that as soon as you put a MovieClip, Sprite, etc... into the DisplayObject Flash/Flex thinks that any parent is of Type DisplayObject. Even though you know perfectly well that the object that contains your Sprite is another Sprite or MovieClip Flex and Flash don't know it. You may have encountered this Flex/Flash Error when you tried to call this.parent from within a Sprite or Flash MovieClip after using addChild() (among others) to display or modify the display of a MovieClip/Sprite on the stage. The compiler doesn't keep track of the Type that the parent is so when you call parent the compiler is confused. Instead it assigns the Type DisplayObject. Your code should still keep working even if you get this error, but there is a reason why the compiler gives you this error. I will show you how to get around this error and how to prevent yourself from getting this error in the future. This AS3 Error can happen any time that you are using any of the following methods:

  • addChild(); Adds a child DisplayObject instance to this DisplayObjectContainer instance.
  • addChildAt(); Adds a child DisplayObject instance to this DisplayObjectContainer instance.
  • contains(): Determines whether a display object is a child of a DisplayObjectContainer.
  • getChildAt(); Returns the child display object instance that exists at the specified index.
  • getChildByName(): Retrieves a display object by name.
  • getChildIndex(): Returns the index position of a display object.
  • setChildIndex(): Changes the position of a child display object.
  • <code>swapChildren(): Swaps the front-to-back order of two display objects.
  • <code>swapChildrenAt(): Swaps the front-to-back order of two display objects, specified by their index values.

Another reason you may get this ActionScript3 Error is by having an external class that is assigned to an object at runtime. Because the class is not predetermined therefore the compiler chokes.

AS3 Error #1118 Solution. You can resolve AS3 Error #1118 by doing one of three things. The first and most highly suggested is to change the way you code. Think from an outside-in perspective when coding rather than an inside-out mentality. The second is to cast the object as the proper Type which explicitly tells the compiler the type of the parent. The third is to just turn off verbose comments in your settings. I don't suggest doing this last fix as it could lead to many more headaches and is horrible when working with a team.

AS3 Error 1118: Fix #1 - Object Oriented Scripting. Code with an outside-in perspective. This means that instead of the child telling the parent what to do the parent tells the child what to do. This may become tricky if you are completing an animation and need an event to happen when the inner movieClip reaches a certain frame. It is still possible and I may post a tutorial on how to do this with Flash in the near future. But this post is not the right place for it. You can pick up Essential ActionsCript 3 by Collin Moock for a great reference and starting point.

AS3 Error 1118: Fix #2 - Cast the object as the type that you know it is. If you know that it is a MovieClip then tell Flash that it is a MovieClip. You do this by Casting the object as shown below.

Bad Code:

this.parent.doSomething();

Good Code:

MovieClip(this.parent).doSomething();

or

(this.parent as MovieClip).doSomething();

AS3 Error 1118: Fix #3 - *Warning* This method will solve the problem temporarily and allow you to publish your code BUT it will cause you many headaches later. You will not get any warnings or error messages and this is a bad thing I Promise! If you are going to proceed down this path make sure to recheck these boxes after you are done publishing your one problem movie. With that much said, The way you do this is to turn off Strict (Verbose) Error Messages in your preferences as shown below.

Go to File >> Publish Settings >> Click on the "Flash" tab >> Next to "ActionScript version:" click the "Settings..."

AS3 Error 1118 -Implicit Coercion

Thanks and as always Happy Flashing,

Curtis J. Morley

4 Comments »

  1. curtismorley.com » Search Engine Optimization Example said,

    June 2, 2008 at 4:34 pm

    […] Common Google Search Terms for Flex /Flash Errors •AS3 Error – #1,2 on Google •Flex Error – #10 on Google •Flex Error Message – #3 on Google •AS3 […]

  2. Sajain Geevar said,

    July 30, 2008 at 2:32 am

    I have a class type say “SampleClass” which is basically a module. It will be then typecasted to DisplayObject. I need cast it back at some point of time. Is it ok? Not working for me? Any solutions?

  3. wagster said,

    May 19, 2009 at 12:10 pm

    Ok – I kinda half get this. I was trying to find out in which menu an item had been clicked. In my clickHandler function, this:

    trace(vertMenu.itemsList.contains(e.target));

    should tell me whether the item that has been clicked is a member of vertMenu.itemsList, but it throws a 1118 instead. After reading the above, I tried:

    trace(vertMenu.itemsList.contains(Sprite(e.target)));

    but as there’s no guarantee that the target is a sprite, this sometimes throws an error as well. However this:

    trace(vertMenu.itemsList.contains(DisplayObject(e.target)));

    works perfectly. Except… I thought you couldn’t cast an object as it’s own ancestor, only its descendent? Anyway, thanks for fixing the problem – if you care to explain how I managed to cast a Sprite as a DisplayObject I would be interested to know!

  4. Richard said,

    September 30, 2011 at 3:31 am

    Hi, I’m getting a 1118 error with the following code.

    function removeBomb(bombName) {
    var toBeRemove:MovieClip = getChildByName(bombName);
    removeChild(toBeRemove);
    }

    bombName is the instance name of a clip that has been added to the stage using addChild();

    Any thoughts?

Leave a Comment