Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 As3错误:错误1009:无法访问空对象引用的属性或方法_Actionscript 3_Class - Fatal编程技术网

Actionscript 3 As3错误:错误1009:无法访问空对象引用的属性或方法

Actionscript 3 As3错误:错误1009:无法访问空对象引用的属性或方法,actionscript-3,class,Actionscript 3,Class,我的库存系统好像出了问题 这是我的班级: package { import flash.display.*; public class InventoryDemo extends MovieClip { var inventory:Inventory; public function InventoryDemo() { inventory = new Inventory(this);

我的库存系统好像出了问题

这是我的班级:

package 
{
    import flash.display.*;

    public class InventoryDemo extends MovieClip
    {
        var inventory:Inventory;

        public function InventoryDemo()
        {
            inventory = new Inventory(this);
            inventory.makeInventoryItems([d1,d2]);
        }
    }
}
我已经在第二个关键帧中放置了d1和d2对象

这是儿童班:

package 
{
    import flash.display.*;
    import flash.events.*;

    public class Inventory
    {
        var itemsInInventory:Array;
        var inventorySprite:Sprite;

        public function Inventory(parentMC:MovieClip)
        {
            itemsInInventory = new Array  ;
            inventorySprite = new Sprite  ;
            inventorySprite.x = 50;
            inventorySprite.y = 360;
            parentMC.addChild(inventorySprite);

        }
        function makeInventoryItems(arrayOfItems:Array)
        {
            for (var i:int = 0; i < arrayOfItems.length; i++)
            {
                arrayOfItems[i].addEventListener(MouseEvent.CLICK,getItem);
                arrayOfItems[i].buttonMode = true;
            }
        }

        function getItem(e:Event)
        {
            var item:MovieClip = MovieClip(e.currentTarget);
            itemsInInventory.push(item);
            inventorySprite.addChild(item);
            item.x = itemsInInventory.length - 1 * 40;
            item.y = 0;
            item.removeEventListener(MouseEvent.CLICK,getItem);
            item.addEventListener(MouseEvent.CLICK,useItem);
        }

        function useItem(e:Event)
        {
            var item:MovieClip = MovieClip(e.currentTarget);
            trace(("Use Item:" + item.name));
        }
    }
}

当我在一个只有d1和d2的黑色项目中尝试它时,代码是有效的。有人能帮我解决这个问题吗?

您的错误表明d1和d2不是在您调用时创建的

inventory.makeInventoryItems([d1,d2]);
建议在第二个关键帧中创建d1和d2,但如果InventoryDemo是DocumentClass,则它将在到达第二个关键帧之前运行

因此,如果要使用d1和d2,您需要将它们移动到第一帧,或者在电影的第二帧出现之前不尝试使用它们

如果要将d1和d2保持在第二帧上,则需要将调用从构造函数中取出,并放入在第二帧上调用的新函数中:

public function InventoryDemo()
{
    //Do nothing here
    //inventory = new Inventory(this);
    //inventory.makeInventoryItems([d1,d2]);
}

public function initialiseInventory():void
{
    //Initialise you inventory here.
    inventory = new Inventory(this);
    inventory.makeInventoryItems([d1,d2]);
}
然后在时间线的第二帧调用函数:

initialiseInventory();
stop();

知道错误发生在代码的何处对我们很有用。当我进行调试时,它说:无法访问null对象引用的属性或方法。在Inventory/MakeInventoryItemsMg非常感谢,我可以问另一个问题吗…?,我如何使用物品…比如d1是开门的钥匙我恐怕你的问题不够具体。如果你有一个特定的问题,你找不到答案,然后把它作为一个新的问题,然后我们可以回答你。