Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.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
Flash 显示索引是否影响对象的范围?_Flash_Actionscript 3_Actionscript - Fatal编程技术网

Flash 显示索引是否影响对象的范围?

Flash 显示索引是否影响对象的范围?,flash,actionscript-3,actionscript,Flash,Actionscript 3,Actionscript,只是想知道索引是否会影响对象的范围,因为我正在创建一个游戏,出于某种原因,我得到了如下错误 TypeError: Error #1009: Cannot access a property or method of a null object reference. at com.objects::Torret/updateObject() at com.objects::EngineApi/loop() 取决于我把我的物体放在舞台上的位置。我可以防止这个错误发生。我所要做的就是

只是想知道索引是否会影响对象的范围,因为我正在创建一个游戏,出于某种原因,我得到了如下错误

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at com.objects::Torret/updateObject()
    at com.objects::EngineApi/loop()
取决于我把我的物体放在舞台上的位置。我可以防止这个错误发生。我所要做的就是修改或删除以下代码

eApi.setChildIndex(this, (eApi.numChildren - 1));
在我的舞台上有源源不断的物体来来去去,所以这个代码可以防止我的物体掉到一个新物体下面。只有一个对象抛出这个错误,那就是我的炮塔类。另一类是我的船类。炮塔类拥有对舰船类的引用,因此它可以向其射击

炮塔类是唯一抛出此错误的类。下面是我两个类的代码。是的,我知道我把塔楼拼错了。谢谢

package com.objects{
    import flash.events.Event;

    public class Torret extends gameObject{

        public var currentAngle:Number;
        protected var newAngle:Number;
        public var shoot:Boolean = false;
        public var autoRotate:Boolean = false;
        public var updated:Boolean = false;
        public var target:Avatar;
        public var enemyLock:Boolean = false;
        public var attackDelay:Number;
        public var delay:Boolean = false;
        public var wielder;
        public var smokeDelay:Number = 500;

        public function Torret():void
        {
            health = 1;
            maxHealth = 1;
            currentAngle = rotation;
            newAngle = currentAngle;
            lastTime = getTime();
        }

        public function Hit(dmg:Number = .01):void {
            if(health > 0)
                health -= dmg;

            if(health < 0)
                health = 0;
        }

        override public function updateObject():void
        {
            eApi.setChildIndex(this, (eApi.numChildren - 1));
            if(health <= 0)
            {
                dead = true;
                blowUp();
            }

            if(y > 949)//If boss doesnt work, this is why
            {
                garbage = true;
            }
            if(!dead)
            {

                if(wielder)
                {
                    scaleX = wielder.scaleX;
                    scaleY = wielder.scaleY;

                }
                if(currentAngle != newAngle || autoRotate == true)
                {
                    rotation += 3;
                    currentAngle = rotation;
                    updated = false
                }
                else
                {
                    updated = true
                }

                if(shoot)
                {
                    if((getTime() - lastTime) > attackDelay && delay == true)
                    {
                        var stingerlaser = new StingerLaser();
                        stingerlaser.laserDir = rotation;
                        stingerlaser.x = x;
                        stingerlaser.y = y;
                        eApi.addGameChild(stingerlaser);
                        lastTime = getTime();
                    }
                }

                if(enemyLock)
                {
                    var dx = target.x - x;
                    var dy = target.y - y;
                    var angle = Math.atan2(dy,dx);
                    rotation = angle * 180/Math.PI;
                }
            }
        }

        protected function blowUp():void
        {
            if((getTime() - lastTime) > smokeDelay)
            {
                var smoke:MissileSmoke = new MissileSmoke();
                smoke.x = x;
                smoke.y = y;
                smoke.dir = -1;

                eApi.addGameChildAt(5,smoke);
                eApi.setChildIndex(smoke, (eApi.numChildren - 4));
                lastTime = getTime();
            }

        }

        protected function degreesToRadians(degrees:Number):Number {
            return degrees * Math.PI / 180;
        }

        protected function rotate(angle:Number):void
        {
            newAngle = angle;
        }
    }
}

让你们知道,updateObject是我所有对象的循环。我有一个集中的循环,在这个循环中,它调用一个放置在舞台上的对象数组。它们都包含updateObject方法。更新对象的状态。addGameChild()是一个封装的addChild(),它不仅将对象添加到后台,还将其放置在数组中,以便可以调用其updateObject()方法。它还用于简化垃圾收集。

首先,我认为使用
addGameChildAt
是不必要的,因为您不需要额外的数组来存储对象来调用更新对象

for(var i:int;i<numChildren;i++) { gameObject(getChildAt(i)).updateObject(); }

我想知道你为什么要使用
setChildIndex
,因为我认为在
eApi
的当前子对象上有一个调用
updateObject
的恒定循环,所以你不断地将
updateObject
方法当前执行的对象移到顶部,这似乎没有意义?

什么是eApi?它在某一点上可以为空吗?当你做eApi.numChildren-1时,你确定里面几乎有一个孩子吗?
for(var i:int;i<numChildren;i++) { gameObject(getChildAt(i)).updateObject(); }
if(eApi && eApi.contains(this)) { eApi.setChildIndex(this, (eApi.numChildren - 1)); }