Kenny Martin
Kenny Martin

Reputation: 1

Bullet won't fire?

I have been working on a space invaders game, I currently have no compiling errors finally but my bullets wont fire.

I kind of know where I should be looking to fix the problem but everything I try fails. Am I right in thinking it is to do with the bullet x-y argument?

I have a Movieclip symbol in the library named Bullet the same as my .as file but the script dose not seem to add the bullets to the scene.

Here is my ship and bullet code.

ship.as

    package
    {
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;

    /**
     * ...
     * @author Kenny Martin
     */
    public class Ship extends MovieClip 
    {
        private var fireRate:int = 6;
        private var framesSinceFire:int = 0;
        private var bulletHolder:Sprite
        private var leftDown:Boolean = false
        private var rightDown:Boolean = false
        private var firing:Boolean = false
        private var speed:int = 10;
        private var bulletPool:Vector.<Bullet>

        public function Ship(bHolder:Sprite)    
        {
            x = 275;
            y = 350;

            bulletHolder = bHolder;
            addEventListener(Event.ADDED_TO_STAGE, addedToStage, false, 0, true);   
        }

        private function fillBulletPool():void 
        {
            bulletPool = Vector.<Bullet>();
            // while counter
            var i:int = 0;

            // while i is less than 25, keep adding bullets
            while (i < 25)
            {
                //if we dont increase i,we risk getting stuck in an infinite loop
                i++;

                //push a new bullet straight into the vector
                bulletPool.push(new Bullet());
            }
        } 

        public function update():void
        {
            // check if left is down and the ship is at least half its width from the edge
            if (leftDown && x > (width / 2))
                x -= speed;

            // check if right is down and the ship is more than half its width from the    right edge
            if (rightDown && x < stage.stageWidth - (width / 2))
                x += speed;

            if (firing && framesSinceFire >= fireRate)
                Fire();
        }
        public function updateBullets():void
        {
            framesSinceFire++;

            updateBullets();

            for (var i:int = 0; i < bulletPool.length; i++)
            {
                bulletPool[i].update();
            }
        }


        public function Fire():void 
        {
            var bullet:Bullet = findBullet();

            if ( bullet != null)
            {
                bullet.activate(x,y);

                bulletHolder.addChild(bullet);

                framesSinceFire = 0;
            }
        }

        private function addedToStage(e:Event):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStage);

            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown, false, 0, true);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUp, false, 0, true);
        }

        private function keyUp(e:KeyboardEvent):void 
        {
            // check left arrow
            if (e.keyCode == 37)
                leftDown = false;
            else if (e.keyCode == 39 ) // check right arrow]
                rightDown = false;
            else if ( e.keyCode == 32)// check space bar
                firing = false;
        }

        private function keyDown(e:KeyboardEvent):void 
        {
            // check left arrow
            if (e.keyCode == 37)
                leftDown = true;
            else if (e.keyCode == 39 ) // check right arrow]
                rightDown = true;
            else if ( e.keyCode == 32)// check space bar
                firing = true;
        }

        private function findBullet():Bullet
        {
            // loop through the bulletpool,from 0 to 24(we added 25 bullets to the pool)
            for (var i:int = 0; i < bulletPool.length; i++)
            {
                if (!bulletPool[i].isActive)
                {
                    // if its not active return the bullet for use
                    return bulletPool[i];
                }

            }

            return null;
        }
    }

}

Bullet.as

    package
    {
    import flash.display.MovieClip;

    /**
     * ...
     * @author Kenny Martin
     */
    public class Bullet extends MovieClip 
    {
        public var isActive:Boolean = false
        private var speed:int = 0.2;
        private var _x:int;
        private var _y:int;

        public function Bullet()
        {
            super()
        }

        public function activate(_x:Number,_y:Number)
        {
            isActive = true;
            x = _x;
            y = _y;

        }
        public function deactivate():void
        {
            isActive = false;

            // check we've got a parent before trying to remove ourselves from one
            if (parent)
                parent.removeChild(this);
        }
        public function update():void
        {
            if (isActive)

            // move up the screen
            y -= speed;

            //until we go well past the top of the screen
            if (y < -height)
            {
                deactivate();
            }
        }   
    }
}

Upvotes: 0

Views: 93

Answers (1)

DFreeman
DFreeman

Reputation: 542

You're not calling the updateBullets function.
The only call to updateBullets is... inside updateBullets... which is never called (and would cause a stack overflow if you did call it with a call to itself in there).

You need to move the updateBullets function call to update().

Upvotes: 1

Related Questions