开发者

does the display index effect the scope of an object?

开发者 https://www.devze.com 2022-12-18 06:58 出处:网络
Just curious to know if the index effect the scope of an object because I am creating a game and for some reason, I get an error like below

Just curious to know if the index effect the scope of an object because I am creating a game and for some reason, I get an error like below

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at com.objects::Torret/updateObject()
    at com.objects::EngineApi/loop()

depending on where I put my object on the stage. I can prevent this error from happening. all i have to do is alter or remove the following code.

eApi.setChildIndex(this, (eApi.numChildren - 1));

There is a constant flow of objects coming and going on my stage so this code keeps my objects from falling underneath a new object. There is only one object that is throwing this error and that is my Turret class. The other class is my ship class. The turret class holds reference to the ship class so it can shoot at it.

Turret class is the only class that is throwing this error. below is my code for both class. And YES i know I spelled turret wrong. THANKS

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;开发者_Go百科
        }

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

Below is my ship class

package com.objects{

    import flash.display.MovieClip

    public class Avatar extends gameObject {

        public var targets:Array;
        public var delay:Number = 3000;
        public var weapon:Number = 1;

        public function Avatar():void
        {
            rotation = -90;
            lastTime = getTime();
            targets = new Array();
        }

        override public function Attack(dir:Number = -40):void
        {
            switch(weapon){

                case 1:
                    var bullet1:Bullet = new Bullet();
                    bullet1.wielder = this;
                    bullet1.x = x + 35;
                    bullet1.y = y + 30;
                    bullet1.bulletDir = rotation;
                    eApi.addGameChild(bullet1);

                    var bullet2:Bullet = new Bullet();
                    bullet2.bulletDir = rotation;
                    bullet2.wielder = this;
                    bullet2.x = x - 35;
                    bullet2.y = y + 30;
                    eApi.addGameChild(bullet2);
                break;
                case 2:
                    if((getTime() - lastTime) > delay)
                    {
                        var missle = new Missile();
                        missle.x = x;
                        missle.y = y;
                        missle.wielder = this;
                        eApi.addGameChildAt((eApi.numChildren - 2),missle);
                        lastTime = getTime();
                    }
                break;
                default:
            }
        }

        public function Hit():void
        {
            trace("ouch");
        }

        override public function updateObject():void
        {
            eApi.setChildIndex(this, (eApi.numChildren - 1));
        }
    }
}

Just to let you guys know, updateObject is the loop for all my objects. I have one centralized loop and in that loop it calls on an array of objects that have been placed on the stage. all of them containing the method updateObject. to update the status of the object. addGameChild() is a encapsulated addChild() , that not only adds the object to the stage, but places it in a array so it's updateObject() method can be called on. it is also used to make it easier for garbage collection.


firstly i think using addGameChildAt is unneccessary as you dont need an extra array to store your objects to invoke updateObject on them.

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

then i suggest to start your class names with uppercase letters i.e. GameObject and method names should start with lowercase letters i.e. attack()

and i recommend you to check whether your objects reference to eApi exists (not null) and your object is part of the eApi before you set it's index

if(eApi && eApi.contains(this)) { eApi.setChildIndex(this, (eApi.numChildren - 1)); }

i wonder why you use setChildIndex anyway, as i take it there is a constant loop which invokes updateObject on the objects which are currently childs of eApi so you constantly move the object whose updateObject method is currently executed to the top, which seems pointless?

0

精彩评论

暂无评论...
验证码 换一张
取 消