AS3:我的符号的新实例覆盖了以前实例的属性

时间:2015-08-25 16:13:04

标签: actionscript-3

所以我正在使用Flash开发一个项目,我遇到了一个非常奇怪的问题。

我项目中的第二个框架上有一系列Actions,它们创建了一个符号的多个副本,我在MessageActions数组中命名了MessageAction。出于某种原因,如果我创建多个MessageActions,舞台上的所有MessageActions将采用最近制作的MessageAction的属性,并将作为该实例的副本,以便我对该实例所做的任何事情也将完成他们也是。具体来说,我有一个OnClick事件“选择”消息动作,无论我点击哪一个,所有这些都将被选中,因为它们都反映了我做的最后一个。

我用来创建MessageActions的代码是:

var ActionArray:Array = new Array();

ActionArray[0] = new MessageAction(0);
addChild(ActionArray[0]);
ActionArray[0].x = 73;
ActionArray[0].y = 565;

ActionArray[1] = new MessageAction(1);
addChild(ActionArray[1]);
ActionArray[1].x = 73;
ActionArray[1].y = 615;

ActionArray[2] = new MessageAction(2);
addChild(ActionArray[2]);
ActionArray[2].x = 73;
ActionArray[2].y = 665;

ActionArray[3] = new MessageAction(3);
addChild(ActionArray[3]);
ActionArray[3].x = 533;
ActionArray[3].y = 565;

ActionArray[4] = new MessageAction(4);
addChild(ActionArray[4]);
ActionArray[4].x = 533;
ActionArray[4].y = 615;

ActionArray[5] = new MessageAction(5);
addChild(ActionArray[5]);
ActionArray[5].x = 533;
ActionArray[5].y = 665;

符号的代码如下:

package {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.Event;

    public class MessageAction extends MovieClip {

        //Message Action Values Key:
        // 0- Research
        // 1- Email Manager
        // 2- Get pricing from vendor
        // 3- Get pricing from distributor
        // 4- Get pricing from partner page
        // 5- Quote customer

        public static var ActionType: int = -1;
        public static var ActionName: Array = new Array("Research", "Email Manager", "Get pricing from vendor", "Get pricing from distributor", "Get pricing from partner page", "Quote Customer");
        public static var Selected:Boolean = false;
        public static var IsActive:Boolean = true;

        public function MessageAction( MyType:int ) {
            ActionType = MyType;
            this.addEventListener(Event.ENTER_FRAME, onUpdate);
            this.addEventListener(MouseEvent.CLICK, clickHandler);
        }

        public function setType(MyType: int): void {
            ActionType = MyType;
        }

        public function Activate(): void {
            IsActive = true;
        }

        public function Deactivate(): void {
            IsActive = false;
        }

        function clickHandler(me: MouseEvent): void {
            if(IsActive == true){
                if (Selected == false) {
                    Selected = true;
                } else {
                    Selected = false;
                }
            }
        }

        function onUpdate(e: Event): void {

            if (IsActive == true && ActionType != -1) {
                alpha = 1;
                ActionText.text = ActionName[ActionType];
            } else {
                ActionText.text = "---";
                alpha = .5;
            }


            if (Selected == false) {
                gotoAndStop(1);
            } else {
                gotoAndStop(2);
            }
        }
    }

}

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

您对“静态”的使用意味着变量保持在类级别,并且每个实例都将共享它。将其更改为一个,并为所有更改。例如,将Selected设置为true可以将所有实例的Selected设置为true。如果您希望每个实例的行为不同,只需删除“static”关键字即可。这将导致变量作用于它们自己的实例。

而不是......

    public static var ActionType: int = -1;
    public static var ActionName: Array = new Array("Research", "Email Manager", "Get pricing from vendor", "Get pricing from distributor", "Get pricing from partner page", "Quote Customer");
    public static var Selected:Boolean = false;
    public static var IsActive:Boolean = true;

...尝试...

    public var ActionType: int = -1;
    public static var ActionName: Array = new Array("Research", "Email Manager", "Get pricing from vendor", "Get pricing from distributor", "Get pricing from partner page", "Quote Customer");
    public var Selected:Boolean = false;
    public var IsActive:Boolean = true;