Martin Dubois
Martin Dubois

Reputation: 47

Constructor argument error

my english is poor because this is not my main language but i'll do my best.

I need help with the argument to the constructor because i dont know where to take all these information.

here my defaultitem class:

public class DefaultItem extends MovieClip
    {
        private var _id:String;
        private var _lastX:int;
        private var _lastY:int;
        private var _isStackable:Boolean = false;
        private var _type:String;
        private var _isDragging:Boolean = false;
        private var _currentContainer:DefaultContainer;
        private var _lastContainer:DefaultContainer;

    public function DefaultItem($id:String, $type:String, $x:int, $y:int)
    {
        stop();

        id = $id;           
        type = $type;

        x = $x;
        y = $y;
        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);


    }

    private function onAddedToStage(e:Event):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        init();
    }

    public function init():void
    {
        buttonMode = true;
        mouseChildren = false;

        _lastX = x;
        _lastY = y;

        addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
        this.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
//resolve drag bugs       
    }

    /**
     * Mouse Event Handlers
     */     
    private function onMouseDownHandler(e:MouseEvent):void 
    {
        isDragging = true;
                    this.mouseEnabled = false;
                    dispatchEvent(new ItemEvent(ItemEvent.ITEM_PICKED_UP, this));
    }

    private function onMouseUpHandler(e:MouseEvent):void 
    {
        // check if item is being dragged
        if (isDragging)
        {
            isDragging = false;
            this.mouseEnabled = true;
            dispatchEvent(new ItemEvent(ItemEvent.ITEM_DROPPED, this));
        }
    }


    /**
     * Getters & Setters
     */
    public function get id():String { return _id; }

    public function set id(value:String):void 
    {
        _id = value;
    }

    public function get lastX():int { return _lastX; }

    public function set lastX(value:int):void 
    {
        _lastX = value;
    }

    public function get lastY():int { return _lastY; }

    public function set lastY(value:int):void 
    {
        _lastY = value;
    }


    public function get currentContainer():DefaultContainer { return _currentContainer; }

    public function set currentContainer(value:DefaultContainer):void 
    {
        _currentContainer = value;
    }

    public function get lastContainer():DefaultContainer { return _lastContainer; }

    public function set lastContainer(value:DefaultContainer):void 
    {
        _lastContainer = value;
    }

    public function get type():String 
    {
        return _type;
    }

    public function set type(value:String):void 
    {
        _type = value;
    }

    public function get isDragging():Boolean 
    {
        return _isDragging;
    }

    public function set isDragging(value:Boolean):void 
    {
        _isDragging = value;
    }

    /**
     * Destroys item
     */
    public function destroy():void
    {
        buttonMode = false;
        removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
        removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
        this.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
    }

}

}

here my item class:

public class Slot extends DefaultContainer
    {
        // vars
        private var _id:String;     
        private var _item:DefaultItem;
        private var _type:DefaultItem;
        //private var isdragging:DefaultItem; 
        public var defaultitem:DefaultItem = new DefaultItem(id, _type, x, y);
        //   trace(DefaultItem.getisDragging());
        //trace(DefaultItem.getisDragging());
        /**
         * Constructor
         * 
         * @param   $id Slot id
         */



    public function Slot($id:String) 
    {

                addEventListener(MouseEvent.ROLL_OUT, onMouseOutHandler);
                addEventListener(MouseEvent.ROLL_OVER, onMouseOverHandler);

        id = $id;
        setLabel($id);
        stop();
    }

    /**
     * Slot Methods
     */
    public function getItem():DefaultItem { return _item; }

    public override function addItem($item:DefaultItem):void 
    {
        _item = $item;
        addChild(_item);

        //
        this.gotoAndStop(2); //active slot
    }

    public override function removeItem($item:DefaultItem):void
    {
        removeChild(_item);
        _item = null;

        this.gotoAndStop(1);  //default slot
    }

    public function hasItem():Boolean
    {
        if (_item == null)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    private function onMouseOutHandler(e:MouseEvent):void {
                trace("mouseOutHandler");
         this.gotoAndPlay("out");
    }

    private function onMouseOverHandler(e:MouseEvent):void {
              trace("mouseoverHandler");
    //    if (!isDragging)
        //{
            //trace("drag = "+ isDragging);
                 this.gotoAndPlay("over");
        //  }
        //else {
            //trace("drag = " + isDragging );
            //this.gotoAndPlay("dragUp");
        //     }
    }


    /**
     * Getters & Setters
     */
    public function get id():String { return _id; }

    public function set id(value:String):void 
    {
        _id = value;
    }

    public function setLabel($label:String):void
    {
        this.label.text = $label;
    }


    /**
     * Destroy
     */
    public function destroy():void
    {
        removeItem(_item)
    }



}

}

the problem is here public var defaultitem:DefaultItem = new DefaultItem(id, _type, x, y); i'm not sure id and _type is working good. I wanna know where to get all these information because i need to call the function isdragging and if i use the var id he target the item and not the defaultcountainer id thanks guys

Upvotes: 1

Views: 70

Answers (2)

MNOPYZ
MNOPYZ

Reputation: 55

It seems to me like you're dragging an item onto something, sorry if I've misunderstood.

But, in that case, you can use something like:

var itemID:String = e.target.id;
var itemType:String = e.target._type;

That's in case you have the item's id and type stored somewhere. Also, as for the x and y, those depend on where you want to put them.

For example, if you had a character and you wanted to give him a sword, it would look something like:

//somewhere in your code:
weapon.id = "Iron Sword";
weapon._type = "Sword";

//And then when you get to the default item part...
var itemId:String = e.target.id;
var itemType:String = e.target._type;
var defaultitem:DefaultItem = new DefaultItem(itemID,itemType, character.x, character.y);

Again, sorry if I misunderstood. Best of luck with your program!

Upvotes: 1

BotMaster
BotMaster

Reputation: 2223

You are creating and instance of DefaultItem but passing wrong arguments.

public var defaultitem:DefaultItem = new DefaultItem(id, _type, x, y);

Since this is created prior to constructor running, id is null, _type is not a String and is null and x, y are out of scope.

It should be:

public var defaultitem:DefaultItem;

Then in Slot constructor:

id = $id;
defaultitem = new DefaultItem(id, _type, x, y);
//but _type is still not a String and is still null

Upvotes: 2

Related Questions