【问题标题】:Error #1063: Argument count mismatch on object in Constructor错误 #1063:构造函数中对象的参数计数不匹配
【发布时间】:2012-10-27 05:15:18
【问题描述】:

我的问题是这样的问题:ArgumentError: Error #1063

不同之处在于,在我的文档类加载后我得到了这个错误,我点击了一个按钮来启动 Game() 类。 PowerUp() 类会自动生成错误,而不会被程序调用。我发现 PowerUp() 类构造函数中的第一个参数没有接收到值。

public function PowerUp(tar:Object,s:Number=1, a:String="bonusShots",t:String="Multi Shot", l:int=100):void {/// 更多代码

我需要将舞台上的影片剪辑“thePlayer”放入 PowerUp() 类构造函数的第一个参数中。

当我将“thePlayer”添加到构造函数中时,我得到另一个错误“object not a compile time constant”。

 var thePlayer: MovieClip=thePlayer;

公共函数 PowerUp(tar:Object=thePlayer,s:Number=1, a:String="bonusShots",t:String="Multi Shot", l:int=100):void {/// 更多代码

问题:如何将“thePlayer”电影剪辑合并到 PowerUp 类的构造函数中?

这是 PowerUp 类的代码

public class PowerUp extends Sprite{


        protected var type:String;// type of power up

        protected var strength:Number;// power of power up

        protected var attribute:String;// attribute effected by power up

        protected var target:Object;// target to buff/boost/perk up/ empower/improve/enhance

        protected var lifespan:int;// the life span of a perk

        protected var maxLifeSpan:int; // max time to live grahic

        const BAR_WIDTH=100;// width of rect

        const BAR_HEIGHT=15;// height of rect



    public function PowerUp(tar:Object,s:Number=1, a:String="bonusShots",t:String="Multi Shot", l:int=100):void {


            type=t;

            strength=s;

            attribute=a;

            target=tar;

            lifespan=l;

            maxLifeSpan=l;

            perkName.text=t;
            perkName.blendMode="invert";

            // apply perk to target
            target[attribute]+=strength;

        }//  end constructor 

【问题讨论】:

  • 您在舞台上的某个地方有 PowerUp 资产实例吗?如果是,则使用 0 参数对其进行初始化。检查这一点:将默认值添加到“tar”参数:public function PowerUp(tar:Object=null,... 并将跟踪语句放入构造函数中。也添加一个空保护:if (!tar) return; 并检查运行 SWF 时出现了多少跟踪。
  • 谢谢,if(!tar) 返回;工作!

标签: actionscript-3 flash compiler-errors


【解决方案1】:
var thePlayer: MovieClip=thePlayer;

在这里,您将变量分配为自身。尝试使用类似的东西:

var _thePlayer:Player = new Player();

玩家必须存在于某处,无论是作为类还是作为共享库项目。

如果您总是将 Player 对象传递给 PowerUp 类,您不妨将第一个参数转换为 Player。

public function PowerUp(tar:Player,

【讨论】:

    【解决方案2】:

    您不能为自动构造的类的构造函数提供参数。如您所见,您只能将编译时常量用作默认值。换句话说,只有编译器在解析代码时才将其理解为值。

    您最好的选择是避免在构造函数中传递参数,并将其视为运行时所需的技术函数,而是在您可以调用自己的其他函数中执行初始化工作。

    例如,假设这是您的文档类:

    public class DocumentClass extends Sprite {
        public function DocumentClass() {
            super();
        }
        public function powerUp(tar:MovieClip):void {
            this.tar = tar;
        }
    }
    

    在需要初始化此类的其他地方,您可以调用powerUp 函数。

    【讨论】:

      猜你喜欢
      • 2019-03-01
      • 2011-06-25
      • 2012-09-08
      • 1970-01-01
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-13
      相关资源
      最近更新 更多