错误2007我无法通过使用其他类变量在主时间轴上调用类变量

时间:2013-12-13 17:55:05

标签: actionscript-3 flash class events listener

我正在创建一个库存系统,它几乎可以运作。我的场景中有两个对象是'apple'和'pear'。当你对它们进行MOUSE_MOVE时,你可以看到它们的名字。您可以点击它们,它们会消失,它们的图标会显示在库存中。

我需要的是当我对项目进行MOUSE_MOVE时也会看到itemName。正如我对物体本身所做的那样。但我无法在apple.itemIcon或pear.itemIcon上设置eventListener并在函数中调用itemName。我只是不知道写什么。我在Event,Listeners和BaseClasses / Classes方面遇到了很多问题,AS3中的一些东西对我来说真的很难理解。

这是我的主要时间轴代码:(由于showItemNameG中的空文本,此代码提供了Error2007)

//INVENTORY
import flash.events.MouseEvent;
import inventory.inventorySystem;
import inventory.itemC;
import flash.text.TextField;

var IS:inventorySystem = new inventorySystem();
var IT:itemC;
var itemNameBox:String;

apple.itemName = "Apple";
apple.itemIcon = new AppleIcon();
apple.addEventListener(MouseEvent.MOUSE_MOVE, showItemNameF);
apple.addEventListener(MouseEvent.MOUSE_OUT, hideItemNameF);
apple.addEventListener(MouseEvent.MOUSE_DOWN, pickUpItem);

function showItemNameG(e:MouseEvent):void{
    itemNameBox = itemC(e.currentTarget).itemName;   <<<<
    stage.addChild(infoBoxObject);
    infoBoxObject.infoBox.text = itemNameBox;
    infoBoxObject.x = mouseX+12;
    infoBoxObject.y = mouseY;
}

pear.itemName = "Pear";
pear.itemIcon = new PearIcon();
pear.addEventListener(MouseEvent.MOUSE_MOVE, showItemNameF);
pear.addEventListener(MouseEvent.MOUSE_OUT, hideItemNameF);
pear.addEventListener(MouseEvent.MOUSE_DOWN, pickUpItem);

function pickUpItem(e:MouseEvent):void{
    var index = IS.addObject(itemC(e.currentTarget));
    itemC(e.currentTarget).x = -145;
    itemC(e.currentTarget).y = 61;
    addChild(itemC(e.currentTarget).itemIcon);
    itemC(e.currentTarget).itemIcon.x = 38.15+(index*64);
    itemC(e.currentTarget).itemIcon.y = 340.65;
    itemC(e.currentTarget).itemIcon.addEventListener(MouseEvent.MOUSE_MOVE, showItemNameG);   <<<<
}

function showItemNameF(e:MouseEvent):void{
    itemNameBox = itemC(e.currentTarget).itemName;
    stage.addChild(infoBoxObject);
    infoBoxObject.infoBox.text = itemNameBox;
    infoBoxObject.x = mouseX+12;
    infoBoxObject.y = mouseY;
}

function hideItemNameF(e:MouseEvent):void{
    infoBoxObject.x = -145;
    infoBoxObject.y = 61;
}

//CURSOR

Mouse.hide();
var cursor:cursorImage = new cursorImage();
stage.addChild(cursor);
cursor.startDrag(true);

这是我的inventorySystem类:

package inventory{

    public class inventorySystem{

        private var slot:Array;
        public function inventorySystem(){
            slot = new Array(10);
            for(var k=0; k<10; k++){
                slot[k]=null;
            }
        }

        public function addObject(it:itemC):int{
            for(var i=0;i<10;i++){
                if(slot[i]==null){
                    slot[i] = it;
                    return i;
                }
            }
            return -1;
        }
        public function getObject(i:int):itemC{
            return slot[i];
        }

        public function useObject(i:int){
            slot[i] = null;
        }
    }

}

这是我的itemC类:

package inventory{
    import flash.display.MovieClip;

    public class itemC extends MovieClip{

        public var itemName:String = "AJBHAKB";
        public var itemIcon:MovieClip;

    }

}

修改

解决方法是将图标'item'作为基类。然后我必须在将它们添加到舞台时将它们命名。为此,我只更改了pickUpItem函数:

function pickUpItem(e:MouseEvent):void{
    var index = IS.addObject(itemC(e.currentTarget));
    itemC(e.currentTarget).x = -145;
    itemC(e.currentTarget).y = 61;
    addChild(itemC(e.currentTarget).itemIcon);
    itemC(e.currentTarget).itemIcon.x = 38.15+(index*64);
    itemC(e.currentTarget).itemIcon.y = 340.65;
    itemC(e.currentTarget).itemIcon.name = itemC(e.currentTarget).name+"Icon";
    itemC(e.currentTarget).itemIcon.addEventListener(MouseEvent.MOUSE_MOVE, showItemNameF);
    itemC(e.currentTarget).itemIcon.addEventListener(MouseEvent.MOUSE_OUT, hideItemNameF);
    itemC(e.currentTarget).itemIcon.itemName = itemC(e.currentTarget).itemName;
}

1 个答案:

答案 0 :(得分:0)

您正在将showItemNameG侦听器添加到错误的对象,您应该将其添加到itemC的对象,而不是将其添加到子影片剪辑。如果您真的必须这样做,请拨打他们的parent媒体资源,根据您的itemC班级文件,该资产将成为itemC的直接子女。

function showItemNameG(e:MouseEvent):void{
    var ic:itemC=e.currentTarget as itemC;
    if (!ic) ic=(e.currentTarget as DisplayObject).parent as itemC;
    // here, first say current target is a DisplayObject, then get parent and coerce type
    if (!ic) {
        // oops! neither parent nor current target is itemC
        trace(e.currentTarget,(e.currentTarget as DisplayObject).parent);
        return;
    }
    itemNameBox = ic.itemName;  // process "ic" as the detected item
    stage.addChild(infoBoxObject);
    infoBoxObject.infoBox.text = itemNameBox;
    infoBoxObject.x = mouseX+12;
    infoBoxObject.y = mouseY;
}