vendredi 1 mai 2015

Random enemy position and type spawn from an array not updating properly with each button click

My spawnEnemyClick button should spawn a new enemy in 1 of 3 random positions (which it does) and the spawned enemy should be 1 random enemy from the 5 enemy types in an array (which it isn't doing). The enemy type from the array will change if I close the swf and re-open it, but not on spawnEnemyClick. I've tried moving the variable(s) for the enemy array, random math, etc. into the spawnEnemyClick function in different ways, which is how I fixed the random position spawning, but it will not work due to all of the enemy variables. I need to get the enemy type updating with each button click, and if there is a better way to have a random enemy type array that doesn't use multiple variables, I'd be interested in that too.

package 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Main_EnemySpawnTest_2 extends MovieClip
    {
        // variables
        var enemyType:Array = [Enemy1, Enemy2, Enemy3, Enemy4, Enemy5];
        var enemyTypeRandomNumber:int = Math.random() * (enemyType.length);
        var enemy = new enemyType[enemyTypeRandomNumber];
        var topPosition:uint = 100;
        var centerPosition:uint = 300;
        var bottomPosition:uint = 500;
        var enemySpeed:int = 5;

        public function Main_EnemySpawnTest_2 ():void
        {
            // listeners
            addEventListener(Event.ENTER_FRAME, checkEveryFrame);
            spawnEnemyButton.addEventListener(MouseEvent.CLICK, spawnEnemyClick);
        }

        public function checkEveryFrame(event:Event):void
        {
            // enemy speed
            enemy.x = enemy.x - enemySpeed;
        }

        function spawnEnemyClick (event:MouseEvent):void
        {
            var enemyPositionRandomNumber:int = Math.random() * 3; 
            var enemyPositionX:int = stage.width - (enemy.width * -0.5);
            spawnEnemyButton.removeEventListener(MouseEvent.CLICK, spawnEnemyClick);
            if (enemyPositionRandomNumber == 0)
            {
                enemy.x = enemyPositionX;
                enemy.y = topPosition;
                addChild (enemy);
            }
            else if (enemyPositionRandomNumber == 1)
            {
                enemy.x = enemyPositionX;
                enemy.y = centerPosition;
                addChild (enemy);
            }
            else if (enemyPositionRandomNumber == 2)
            {
                enemy.x = enemyPositionX;
                enemy.y = bottomPosition;
                addChild (enemy);
            }
            trace (enemyTypeRandomNumber);
            spawnEnemyButton.addEventListener(MouseEvent.CLICK, spawnEnemyClick);
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire