问题描述
这是一个问题,最好在代码中解释。
This is a problem that's best explained in code.
我没有看到 active_button.upState
,我知道是一个TextField当我尝试访问 textColor
属性时,神奇地变成一个DisplayObject。
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.
另外,为什么我有一个对象,我知道是一个SimpleButton(再次,看到跟踪)我需要把它转换到SimpleButton顺序把它存储在var?这对我没有任何意义。
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?
}
}
错误:
1118:静态类型的隐式强制值flash.display:DisplayObject到可能不相关的类型flash.display:SimpleButton。
1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:SimpleButton.
1119:Access可能未定义的属性textColor通过静态类型的引用flash.display:DisplayObject
1119: Access of possibly undefined property textColor through a reference with static type flash.display:DisplayObject
编辑:我已经压缩了我的问题,并发布为
I've condensed my question a little bit and posted as AS3: Why does datatype automatically change from TextField to DisplayObject on its own?
推荐答案
从显示列表中提取的任何东西都被归类为最低可能的dinominator ,因为虽然SimpleButton是一个displayObject,不是所有的displayObjects都将是SimpleButtons。
这可能是你的第二个问题的原因,只是执行稍有不同。在SimpleButton中有一个直接引用,说明upState是一个TextField?
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?
尝试关闭严格的编译模式,看看它是否有帮助。
请参阅 for more information
try switching off Strict Mode of compilation and see if it helps.see Type Checking Livedocs for more information
这篇关于AS3:类型为SimpleButton的全局变量更改为DisplayObject的未知原因,不会让我访问.upState.textColor!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!