【问题标题】:Constructor argument error构造函数参数错误
【发布时间】:2014-07-16 13:33:55
【问题描述】:

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

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

这是我的默认项目类:

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,谢谢大家

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

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

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

    由于 this 是在构造函数运行之前创建的,因此 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
    

    【讨论】:

    • 您否决了 MNOPYZ 的详细答案。我不喜欢这种行为。我也否决了你的回答。
    【解决方案2】:

    在我看来,您好像是在将一个项目拖到某个东西上,如果我误解了,请见谅。

    但是,在这种情况下,您可以使用以下内容:

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

    再次抱歉,如果我误解了。祝你的程序好运!

    【讨论】:

    • 感谢您的回答,但我最大的问题是类型和 id,因为如果我在我的插槽类 flash 中执行 e.target.id 将返回我的插槽的 id 而不是我的项目的 id 你可以在此链接中找到脚本freeactionscript.com/2011/07/…
    • 感谢您的回答,但我最大的问题是类型和 id,因为如果我在我的插槽类中执行 e.target.id,flash 将返回我的插槽的 id,而不是我想要的项目的 id要知道我的拖动翻转函数的 isdraggin 的值....并且在调用该函数之前我需要 4 个参数。它在那里我被卡住了,我的课程知识有限你可以在这个链接找到脚本freeactionscript.com/2011/07/…
    猜你喜欢
    • 2014-07-06
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    相关资源
    最近更新 更多