Sunday, May 1, 2011

Constructor arguments problem ActionScript 3

Hi, I have a custom class defined in Actionscript and I want to make an instance of it in the main document of Flash application. However, after calling the constructor with one argument, Flash gives me this error:

Error #1063: Argument count mismatch on coa.application::MenuItem(). Expected 1, got 0.

This is my class:

public class MenuItem extends MovieClip{
 var button:SimpleButton;

 public function MenuItem(buttonLoc:uint) {

  button = new InvBtn();
  this.addChild(button);
  button.x=-81;
  button.y=buttonLoc*33;
  button.addEventListener(MouseEvent.CLICK, mybringToFront);
 }
}

And this is my attempt to call its constructor:

var menu1:MovieClip = new MenuItem(3);

Any idea, whats wrong?

From stackoverflow
  • Apologies, I can't comment yet, or I'd put this in a comment.

    Are you sure that:

    var menu1:MovieClip = new MenuItem(3);

    is the only place that you're constructing a new MenuItem? You don't by any chance have the MenuItem class attached to some instances on the stage?

    I changed your code to this (just so I could run it) and it works fine:

    package{
        import flash.display.MovieClip;
        import flash.display.SimpleButton;
        import flash.events.MouseEvent;
        public class MenuItem extends MovieClip{
    
                var button:SimpleButton;
    
                public function MenuItem(buttonLoc:uint) {
    
                        button = new SimpleButton();
                        this.addChild(button);
                        button.x=-81;
                        button.y=buttonLoc*33;
                        button.addEventListener(MouseEvent.CLICK, mybringToFront);
                }
    
          public function mybringToFront(event:MouseEvent):void{
           trace('blah');
          }
        }
    }
    
    Dungeo : oops, yes, you are right, there was an instance of that symbol on the stage, but i didnt realize it until reading this ;) thanxalot
  • Like quoo said, most likely you have an instance of the object that the class is attached to on stage. To test for that do this:

    
    public class MenuItem extends MovieClip{
            var button:SimpleButton;
    
            // I changed it to int, cuz uint is extremely slow for any math
            // other than bitwise operators, int is fast as long as no fractions
            public function MenuItem(buttonLoc:int = -1) {
                    if (buttonLoc == -1)
                        trace("On stage instance found! Location: "+x+", "+y);
    
                    button = new InvBtn();
                    this.addChild(button);
                    button.x=-81;
                    button.y=buttonLoc*33;
                    button.addEventListener(MouseEvent.CLICK, mybringToFront);
            }
    }
    
    

0 comments:

Post a Comment