views:

279

answers:

1

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( e.target ); // writes [object SimpleButton]
        active_button = SimpleButton( e.target ); // 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?
    }
}

ERRORS:

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 http://stackoverflow.com/questions/2165095/as3-why-does-datatype-automatically-change-from-textfield-to-displayobject-on-it

+1  A: 

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

shortstick
yes, upState returns a DisplayObject which of course does not have a textColor property. Try casting the upState to a textfield.
Allan
upState, in this example, returns a TextField because that's what contained in the upState movie. See my last trace() statement.
Thomas
fyi, var test = TextField( active_button.upState ); creates this error: Error #1056: Cannot create property upState on flash.text.TextField. at Menu/menuClick()
Thomas
the question is not what it actually returns (eg a TextField), its what flash expects to find in your code (a DisplayObject) therefore you need to cast the Display object using the 'as' keyword in a way like this: var test = (active_button.upState as TextField).textColor
shortstick