【问题标题】:Error of implicit coercion on a TextFieldTextField 上的隐式强制错误
【发布时间】:2011-11-02 18:06:51
【问题描述】:

我认为我有最常见的动作脚本错误。在下面的代码中,我有一个包含一些 TextField 的 MovieClip,我想为它们设置动画。当我将类分配给 MovieClip 时,我收到此错误 1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.text:TextField.。当我 trace 孩子们时,我得到 [Object TextField] 并且如果我将它放在第一帧并将其应用于动态文本,那么代码可以正常工作,所以为什么当我尝试将此代码应用于 a 的孩子时会出现此错误影片剪辑?

有没有可能忘记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;

【问题讨论】:

  • 您将孩子投射为对象。如果 TextAnim 需要一个文本字段,请将其转换为文本字段。看看是否有效
  • 如果我将 private var child : Object; 更改为 private var child : TextField; 我得到 Type was not found or was not a compile-time constant: TextField. 错误。
  • 你需要做import flash.text.*;
  • 谢谢!现在可以使用了!

标签: flash actionscript-3 textfield


【解决方案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.
}

【讨论】:

    【解决方案2】:

    您可以在获取对象时将 child 转换为 Object 或更改 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      • 2012-10-24
      • 2011-02-21
      相关资源
      最近更新 更多