TextField 上的隐式强制转换错误

发布于 2024-12-13 03:27:16 字数 1541 浏览 0 评论 0原文

我认为我有最常见的动作脚本错误。在下面的代码中,我有一个 MovieClip,里面有一些 TextFields,我想为它们设置动画。当我将类分配给 MovieClip 时,我收到此错误 1118: Implicit coercion of a将静态类型 Object 的值转换为可能不相关的类型 flash.text:TextField.。当我 trace 孩子时,我得到 [Object TextField] 并且如果我将它放在第一帧上并将其应用到动态文本,代码可以正常工作,所以为什么我得到这个当我尝试将此代码应用于 MovieClip 的子级时出错?

是否有可能忘记导入任何必要的库?

我已经使文本字段动态化,我已经嵌入了字符并设置了动画的抗锯齿。

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;

I think i have the most common actionscript error.In the code below i have a MovieClip with some TextFields inside and i want to animate them.When i assign the class to the MovieClip i get this error 1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.text:TextField.. When i trace the childs i get [Object TextField] and the code works fine if i place it on the first frame and apply it to a Dynamic Text so why i get this error when i try to apply this code to the childs of a MovieClip ?

Is there a chance to have forgot to import any necessary library ?

I have made the TextFields dynamic,i have embed the characters and set anti-alias for animation.

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 } );
    }   
  }
}

UPDATE : I made the suggestion changes and worked.

import flash.text.*;
private var child : TextField;
child = getChildAt( i ) as TextField;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆蝶 2024-12-20 03:27:16

试试这个:

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.
}

Try this:

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.
}
分开我的手 2024-12-20 03:27:16

当您获取对象或更改子变量的声明时,您可以将 child 强制转换为对象:

// 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;

You can just cast child as an Object when you get the object or change the declaration of the child var:

// 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文