【问题标题】:Error 2007 I cannot call a class variable on main timeline by using other class variable错误 2007 我无法使用其他类变量调用主时间轴上的类变量
【发布时间】:2013-12-13 17:55:05
【问题描述】:

我正在创建一个库存系统,它几乎可以工作。我的场景中有两个对象是“苹果”和“梨”。当您对它们进行 MOUSE_MOVE 时,您可以看到它们的名称。你可以点击它们,它们就会消失,它们的图标会出现在库存中。

当我在项目上 MOUSE_MOVE 时,我需要看到 itemName。就像我对物体本身所做的那样。但我无法在 apple.itemIcon 或 pear.itemIcon 上设置 eventListener 并在函数中调用 itemName。我只是不知道该写什么。我在事件、监听器和 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;

    }

}

编辑

解决方案是将图标“项目”作为基类。然后,当它们被添加到舞台时,我必须为它们命名。为此,我只更改了 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;
}

【问题讨论】:

    标签: actionscript-3 flash class events listener


    【解决方案1】:

    您将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;
    }
    

    【讨论】:

    • 嗯,首先感谢您尝试帮助我。我尝试了您的代码,它给了我 AppleIcon 作为目标和 Timeline 作为父级。我仍然需要在不给出具体名称的情况下指向 AppleIcon。然后我想出了将图标的基类更改为“项目”和编辑功能的想法,正如您在我的主帖中看到的那样。
    • 幸好你找到了解决方案。因此,您将图标本身而不是 itemC 实例添加到舞台,这就是您无法访问该项目的原因。更好的方法是将所有项目管理包含在itemC 类中,包括侦听器,并将itemC 实例添加到舞台而不是添加图标,并将图标添加到itemC 实例以提供项目的视觉效果。你当然可以坚持你的方法。
    • 嗯,是的,可能最好的解决方案是将图标的类设置为“项目”,因为它会产生很多调用问题。我真的需要更多时间在 AS3 上,因为很多事情我还不清楚。我可以找到解决方案,但我基本上无法编写代码。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2012-02-03
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多