samedi 17 septembre 2016

Addchild - object not appearing on stage from external class

I'm having problem with "DiamondEnemy" objects created in external class "Level" not appearing on the stage. I'm trying to retrieve a random enemy from "EnemyNotReleasedArray" at intervals and add them to screen through "enemyOnScreen" sprite.

Please note I have not 100% finished with all the functionality; so it may seem a little weird. I don't want to go further until I can actually get it working.

package  {

    import DiamondEnemy;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.display.Sprite;
    import flash.display.MovieClip;

    public class Level extends MovieClip {

        private const START_DELAY_SECONDS:uint = 1000;
        private var EnemyNotReleasedArray:Array = new Array();
        private var startDelay:Timer;
        private var enemyOnScreen: Sprite;

        public function Level(NumberDiamonds:uint) 
        {
            // load the required enemies into the array
            loadEnemyArray(NumberDiamonds);

            //setup up sprite, for enemies that will appear on the screen
            enemyOnScreen = new Sprite();
            addChildAt(enemyOnScreen, numChildren);

            // create delay timer before enemies can start appearing on screen
            startDelay = new Timer(START_DELAY_SECONDS,1);

            // set eventlistener that once delay finishes 
            startDelay.addEventListener(TimerEvent.TIMER_COMPLETE, releaseRandomEnemy);
            startDelay.start();

            //setup up sprite, for enemies that will appear on the screen
            enemyOnScreen = new Sprite();
            addChild(enemyOnScreen);

        }

        // creates the requested number of enemies type into EnemyNotReleasedArray so they can be released later
        private function loadEnemyArray(numDiamonds:uint)
        {
            // use requested number diamonds enemies - to create diamond enemy objects
            for (var i:uint = 0; i < numDiamonds; i++)
            {
                var diamond:DiamondEnemy = new DiamondEnemy();
                EnemyNotReleasedArray.push(diamond);
            }
        }

        // selects a random enemy from EnemyNotReleasedArray and resizes the array so enemy is no longer in it
        private function releaseRandomEnemy(evt:TimerEvent)
        {
            var arrayLength:uint = EnemyNotReleasedArray.length;

            // check make sure array is not empty, if empy level is over
            if (arrayLength > 0)
            {
                var randomArrayIndex = Math.ceil(Math.random() * arrayLength) -1;

                /// adding random enemy to sprite object
                enemyOnScreen.addChild(EnemyNotReleasedArray[randomArrayIndex]);
                trace(EnemyNotReleasedArray[randomArrayIndex]);

                //remove the enemy from array and make element null
                EnemyNotReleasedArray.removeAt(randomArrayIndex)

                //tempory array to store non-null values
                var tempArray:Array = new Array();

                // cycle through EnemyNotReleasedArray and store all values that are not null into temp array
                for each(var enemy in  EnemyNotReleasedArray)
                {
                    if (enemy != null)
                    {
                        tempArray.push(enemy)
                    }
                }

                // save temp array value into EnemyNotReleasedArray
                EnemyNotReleasedArray = tempArray;
            }
            else
            {
                trace("no more enemies left in array");
            }
        }
    }

}

Aucun commentaire:

Enregistrer un commentaire