Raider00321
Raider00321

Reputation: 57

An issue with Arrays

I'm trying to keep a static array list so i can call it globally in my flash application when needed, however it doesn't seem to be adding any values into the array list.

package com.globals
{
    import com.player.PlayerBullets;
    import com.ships.enemy.SpaceDrone;
    import com.globals.Globals;
    import flash.display.MovieClip;
    public class MCActiveLibrary 
    {
        private static var enemyShips:Array = new Array();
        private static var enemyBullets:Array = new Array();
        private static var playBullets:Array = new Array();

        public static function addPlayerBullets(bullets:PlayerBullets):void
        {
            playBullets.push(bullets);
            trace(playBullets[1]);
            bullets.id = playBullets.length -=1;
            trace("array Length:"+ bullets.id + "\nbullet ID:"+playBullets.length+"\n");
            Globals._stage.addChild(bullets);
        }
        public static function getPlayerBullets(i:int):PlayerBullets
        {
            return playBullets[i];
        }
        public static function removePlayerBullets(i:int):void
        {   
        trace(i);
            Globals._stage.removeChild(playBullets[i]);
            //updatePositions(playerBullets, i+1);
            //playerBullets.splice(i, 1);
        }
}

The run time error i get is..

TypeError: Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/removeChild() at com.globals::MCActiveLibrary$/removePlayerBullets()[H:\HobbyProject\SpaceDevils\com\globals\MCActiveLibrary.as:28] at com.player::PlayerBullets/eFrame()[H:\HobbyProject\SpaceDevils\com\player\PlayerBullets.as:30]

and as for the trace commands being ran

[object PlayerBullets] array Length:0 bullet ID:0 0

This one has got me a little stumped, especially considering the object PlayerBullets is being found in position 0 of the array, even though the array length is 0.

Much thanks for any help anyone can give!

Upvotes: 1

Views: 56

Answers (1)

Marijn
Marijn

Reputation: 810

change the line

bullets.id = playBullets.length -=1;

to

bullets.id = playBullets.length - 1;

your line changes the length of the array, instead of setting the bullets.id.

also, array's are zero-indexed, so trace(playBullets[1]); will fail since there is just 1 object in the array, at index 0, instead of index 1. replace it with trace(playBullets[0]);

Upvotes: 1

Related Questions