AS3 - 获得错误#1063,预期1,即使它有效,也得到0?

时间:2011-11-11 18:02:01

标签: flash actionscript-3 this

AS3 newby在这里!

我的舞台上有一个带有“ThePlayer”类的movieclip(thePlayer),其中movieclip是一个带有StopBtn类的SimpleButton。这两个类都保存在一个名为“classes”的文件夹中。

'ThePlayer'类将声音加载到声道中然后播放,从'StopBtn'类开始,我为影片剪辑中舞台上的按钮添加一个事件监听器,然后停止声音通道var set'来自“StopBtn”类的ThePlayer'。

要做到这一点,我将'this'传递给StopBtn()函数,以便能够控制'ThePlayer'中设置的soundchannel变量

该按钮似乎运行正常,但我在输出面板中出现此错误:

ArgumentError: Error #1063: Argument count mismatch on classes::StopBtn(). Expected 1, got 0.
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at classes::ThePlayer()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()

我相信它认为StopBtn类中没有任何东西传递给StopBtn()函数,但是当我运行文件并按下舞台上的停止按钮时,它可以工作......

现在已经把头撞到了墙上4天了:(

这是我的两个类的代码:

ThePlayer.as

package classes
{

import flash.display.*;
import flash.media.*;
import flash.net.*;
import flash.events.*;

public class ThePlayer extends MovieClip
{

    //Link vars to classes for accessing vars here in different classes
    public var _stopBtn:StopBtn;

    // Public and private Vars
    public var mySound:Sound = new Sound();
    public var mySoundChannel:SoundChannel = new SoundChannel();
    public var trackPosition:Number = 0;
    public var isPlaying:Boolean = false;


    //Constructor function
    public function ThePlayer()
    {
        _stopBtn = new StopBtn(this);

        mySound.load(new URLRequest("mp3_files/song.mp3"));
        mySoundChannel = mySound.play();
        isPlaying = true;
    }
}
}

StopBtn.as

package classes
{

import flash.display.*;
import flash.media.*;
import flash.net.*;
import flash.events.*;

public class StopBtn extends SimpleButton
{

    var playerClass:ThePlayer;

    public function StopBtn(recivedPlayerClass:ThePlayer)
    {
        playerClass = recivedPlayerClass;
        playerClass.addEventListener(MouseEvent.CLICK, stopButtonFunction);
    }

    function stopButtonFunction(event:MouseEvent):void
    {
        playerClass.trackPosition = 0;
        playerClass.mySoundChannel.stop();
        playerClass.isPlaying = false;
        //playerClass.pauseBtn.visible = false;
        //playerClass.playPause.playBtn.visible = true;
    }

}
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我想StopBtn是在Flash IDE中创建的,或者IDE无法知道哪个参数必须传递给StopBtn构造函数,因此错误。

例如,您可以为构造函数创建一个默认参数,或者创建一个没有参数但使用额外init方法的参数:

public class StopBtn extends SimpleButton {
 var playerClass:ThePlayer;

 public function StopBtn(recivedPlayerClass:ThePlayer = null) // default constuctor
 { 
   if (recivedPlayerClass!=null) {
     playerClass = recivedPlayerClass;
     playerClass.addEventListener(MouseEvent.CLICK, stopButtonFunction);
   } 
 }
//...

public class StopBtn extends SimpleButton {
 var playerClass:ThePlayer;

 public function StopBtn() {
 }

 public function init(recivedPlayerClass:ThePlayer):void {
     playerClass = recivedPlayerClass;
     playerClass.addEventListener(MouseEvent.CLICK, stopButtonFunction);
 }
//...