views:

40

answers:

3

I've created a static class in Flash which works as the inventory delegate for this game. Flash though keeps giving me this error:

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

I've looked through the code and can't for the life of me find the source of the error. When I run the movie in debug mode it tells me that the error can be found in line 8 in Main(), which is the document class, and currently holds no code besides an empty constructor. Line 8 is where the constructor starts.
The Inventory class looks like so:

package  {  
    import flash.display.MovieClip;
    import flash.utils.getDefinitionByName;

    public class Inventory extends MovieClip{

        private static var instance:Inventory;
        private var invItemQuant:int;

        public function Inventory(pvt:PrivateClass){
            invItemQuant = 0;
        }

        public static function getInstance():Inventory{
            if(Inventory.instance == null){
                Inventory.instance = new Inventory(new PrivateClass());
            }
            return Inventory.instance;
        }

        //Usage: x.addToInventory(itemName);
        //After: item has been added to the inventory
        public function addToInventory(itemName:String){
            var itemType:Class = getDefinitionByName(itemName) as Class;
            var theItem:MovieClip = new itemType();
            theItem.name = itemName;
            invItemQuant++;
            MovieClip(getChildByName("itemSlot" + invItemQuant)).fillSlot(theItem);
        }

        //Usage: x.removeFromInventory(itemName);
        //Before: item is contained in inventory
        //After: item has been removed from the inventory
        public function removeFromInventory(itemName:String){
            for(var i:int = 1; i <= invItemQuant; i++){
                MovieClip(getChildByName("itemSlot" + i)).emptySlot(itemName);
            }
            invItemQuant--;
        }

        //Usage: q = getItemQuant();
        //Before: q is int
        //After: the number of items in inventory is in q
        public function getItemQuant():int{
            return invItemQuant;
        }
    }
}

class PrivateClass {
    public function PrivateClass(){
        trace("PrivateClass called");
    }
}

I made it static so I could access it from stage and add items to the inventory. The Inventory MovieClip is then made of equally big slots named itemSlot1, itemSlot2, etc.
The inventory slots are then linked to this class:

package  {
    import flash.display.MovieClip;

    public class InvSlot extends MovieClip {

    public function InvSlot() {
        }

        //Usage: x.fillSlot(itemName);
        //Before: slot is empty
        //After: object theItem has been put into the slot
        public function fillSlot(theItem:MovieClip){
            this.addChild(theItem);
        }

        //Usage: x.emptySlot(itemName);
        //Before: movieclip itemname may exist in this object
        //After: movieclip itemname does not exist in this object
        public function emptySlot(itemName:String){

        }
    }
}

I haven't made the emptySlot method yet. The fillSlot method just adds the received movieClip to it self.
Finally I have the stageItem which when interacted with (currently just clicked) should be added to the inventory.

package  { 
    import flash.events.MouseEvent;

    public class StageItemRubberChicken extends StageItemBase{

        public function StageItemRubberChicken() {
            description = "It's a chicken, of the rubbery kind";
            descriptiveName = "Rubber Chicken";
            itemName = "ItemRubberChicken";
            this.addEventListener(MouseEvent.CLICK, interactWithItem, false, 0, true);
        }

        private function interactWithItem(e:MouseEvent){
            var mainInventory:Inventory = Inventory.getInstance();
            mainInventory.addToInventory(itemName);
            this.parent.removeChild(this);
        }
    }
}
+2  A: 

Don't let the C++-ish syntax fool you. Actionscript isn't C++.

For some reason, when you write an AS class, you have two rules that you must follow:

  • you may only have one constructor
  • the constructor must have the same arguments as the superclass's constructor.

Thus, the problem with your Inventory class is that its constructor takes an argument that doesn't exist in the MovieClip constructor.

Kelsey Rider
Ahhh, I see. I'll have to unlink my Inventory class from my on stage MovieClip. Cheers.
Bjorninn
+1  A: 

Allow me to disagree here. I know the question is marked as answered , but Inventory looks like a Singleton to me so if you instantiated it in Main like this:

new Inventory();

you will get an error, because Inventory expects an argument of type PrivateClass which , by the way , is not accessible from the Main class. This is what the error means

Argument count mismatch on Inventory(). Expected 1, got 0. 

the only way to create an instance of Inventory is to do the following:

var inventory:Inventory = Inventory.getInstance();

Also, sorry Kelsey, but stating that the subclass constructor must have the same arguments as the superclass constructor is simply not accurate.

PatrickS
A: 

Just found out what was wrong by following PatrickS logic. What I had done was create a Movieclip which had some inventory slot graphics and link it with the Inventory class. I had then dragged an instance of the MovieClip onto the stage.

What Flash did then was probably to use the default constructor instead of "Inventory.getInstance();" to create that instance.

Creating the inventory with

mainInventory:MovieClip = Inventory.getInstance();
addChild(mainInventory);

in Main() does the job of fixing this. One has to be careful I guess when doing gymnastics in code and then using the drag and drop Flash timeline.

Bjorninn