构造函数参数错误

时间:2014-07-15 13:16:56

标签: actionscript-3

我的英语很差,因为这不是我的主要语言,但我会尽我所能。

我需要帮助构造函数的参数,因为我不知道从哪里获取所有这些信息。

这是我的defaultitem类:

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

}

}

这是我的项目类:

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



}

}

问题在这里public var defaultitem:DefaultItem = new DefaultItem(id,_type,x,y); 我不确定id和_type是否正常工作。我想知道从哪里获取所有这些信息,因为我需要调用函数isdragging,如果我使用var id他定位项目而不是defaultcountainer id谢谢你们

2 个答案:

答案 0 :(得分:2)

您正在创建DefaultItem的实例,但传递错误的参数。

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

由于这是在构造函数运行之前创建的,因此id为null,_type不是String且为null且x,y超出范围。

应该是:

public var defaultitem:DefaultItem;

然后在Slot构造函数中:

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

答案 1 :(得分:1)

在我看来,就像你把物品拖到某物上一样,对不起,如果我误解了。

但是,在这种情况下,你可以使用类似的东西:

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

如果您将项目的ID和类型存储在某处,则会出现这种情况。 此外,对于x和y,这些取决于您想要放置它们的位置。

例如,如果你有一个角色而你想给他一把剑,那就像是:

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

再次,抱歉,如果我误解了。祝您的计划好运!