AS3: global var of type SimpleButton changes to DisplayObject for unknown reason, won't let me access .upState.textColor!-Collection of common programming errors

This is a problem that’s best explained in code.

I don’t see how active_button.upState, which I know is a TextField (see trace statements), mysteriously turns into a DisplayObject when I try to access the textColor property.

I’ve included the error messages below for reference.

Also, why is it that when I have an object that I know is a SimpleButton (again, see traces) I need to cast it to SimpleButton in order to store it in a var? That doesn’t make any sense to me.

All help is much appreciated. Thanks.

public class Menu extends MovieClip
    private var active_button:SimpleButton;

    public function Menu() 
        // menu_list is a collection of SimpleButtons, I make the first one the 'active_button' and give each a MOUSE_DOWN event listener.
        trace( menu_list.getChildAt( 0 )); // writes [object SimpleButton]
        active_button = SimpleButton( menu_list.getChildAt( 0 )); // Cast is required here. Otherwise throws Error 1118. Strange. Why is that?

        for( var i:Number = 0; i < menu_list.numChildren; i++ )
            menu_list.getChildAt( i ).addEventListener( MouseEvent.MOUSE_DOWN, menuClick );

    private function menuClick( e:Event ) : void
        trace( ); // writes [object SimpleButton]
        active_button = SimpleButton( ); // Cast is required here. Otherwise throws Error 1118. Still Strange.
        trace( active_button ); // writes [object SimpleButton]. Normal.
        trace( active_button.upState ); // writes [object TextField]. Normal.
        active_button.upState.textColor = 0xAAAAAA; // Throws Error 1119. WTF?! textColor is a perfectly valid property of active_button.upState. Why is it suddenly type DisplayObject?


1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:SimpleButton.

1119: Access of possibly undefined property textColor through a reference with static type flash.display:DisplayObject

EDIT: I’ve condensed my question a little bit and posted as

  1. Anything that is pulled from the displaylist is classed as the lowest possible dinominator (eg. displayObject), as although SimpleButton is a displayObject, not all displayObjects will be SimpleButtons. This is probably the reason for your second problem too, just slightly different in execution. is there a direct reference in SimpleButton that states upState is a TextField?

    try switching off Strict Mode of compilation and see if it helps. see Type Checking Livedocs for more information

Originally posted 2013-11-09 19:44:35.