我认为我有最常见的动作脚本错误。在下面的代码中我有一个带有一些TextFields的MovieClip,我想给它们设置动画。当我将类分配给MovieClip时,我收到此错误1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.text:TextField.
。当我trace
孩子我得到[Object TextField]
并且代码工作正常如果我将它放在第一帧并将其应用于动态文本,那么为什么我在尝试将此代码应用于此时会出现此错误MovieClip的孩子?
是否有机会忘记import
任何必要的图书馆?
我已经使TextFields动态化,我已经嵌入了角色并为动画设置了反别名。
package AScripts
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import com.greensock.*;
import com.greensock.easing.*;
import flupie.textanim.*;
public class TextFade extends MovieClip {
private var child : Object;
public function TextFade( )
{
/* for (var i : int = 0; i < numChildren; i++ ) {
child = getChildAt( i );
trace( child );
*/
child = getChildAt( 0 );
var txtanim:TextAnim = new TextAnim( child ); // <-- Error
/* TextAnim expects a TextField as argument */
txtanim.mode = TextAnimMode.RANDOM;
txtanim.split = TextAnimSplit.WORDS;
txtanim.effects = myEffect;
txtanim.start();
}
function myEffect( block:TextAnimBlock ) : void
{
TweenLite.to( block , .5 , {alpha : 0 , delay : Math.random( ) * 1 } );
}
}
}
更新:我提出了改变并付诸实施。
import flash.text.*;
private var child : TextField;
child = getChildAt( i ) as TextField;
答案 0 :(得分:1)
当您获得对象或更改子var的声明时,您可以将子项转换为Object:
// cast as TextField
public function TextFade( )
{
child = getChildAt( 0 ) as Object;
// ... rest of method
// change declaration of child
public class TextFade extends MovieClip {
private var child : TextField;
答案 1 :(得分:1)
试试这个:
try
{
// Need to explicitly cast the child to TextField otherwise
// we are making an implicit cast and will get an error.
var textField:TextField = child as TextField;
var txtanim:TextAnim = new TextAnim( textField);
}
catch (error)
{
// unable to cast to a textfield, handle this error if necessary.
}