2
votes

AS3 newby here!

I have a movieclip (thePlayer) on my stage with the class 'ThePlayer' and inside that movieclip is a SimpleButton with the class StopBtn. Both classes are saved in a folder called 'classes'.

'ThePlayer' class loads a sound into a soundchannel and then plays, from the 'StopBtn' class i am adding an event listener for the button that is already on the stage inside the movieclip and then stopping the soundchannel var set in 'ThePlayer' from the class 'StopBtn'.

To do this i am passing 'this' to the StopBtn() function to be able to controll the soundchannel variable set in 'ThePlayer'

The button seems to work perfectly, but im getting this error in the output panel:

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()

I believe that it thinks there is nothing being passed to the StopBtn() function in the StopBtn class, but when i run the file and press the stop button on the stage, it works...

Been banging my head against the wall with this for 4 days now :(

Here is my code for both classes:

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

}
}

Thanks for your help!

1
I believe you're creating two StopBtns, the first is the one you defined on the stage inside the movieclip - it gets created and the constructor gets no parameters, hence the error. The second is the one instantiated with your new keyword but is never added as a child. If you want to supress the error and not fix the code, you might make the recivedPlayerClass an optional parameter with: public function StopBtn(recivedPlayerClass:ThePlayer = null), then check for null and return; - Steve Lewis

1 Answers

1
votes

I guess StopBtn is created into Flash IDE, or the IDE has no way to know which parameter have to be passed to the StopBtn constructor hence the error.

What you can do for example is create a default argument for the constructor or one with no parameter but with an extra init method:

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

or

public class StopBtn extends SimpleButton {
 var playerClass:ThePlayer;

 public function StopBtn() {
 }

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