子弹赢了?

时间:2014-08-15 18:42:44

标签: actionscript-3 movieclip

我一直在从事太空侵略者游戏,我目前没有编译错误,但我的子弹不会开火。

我知道应该在哪里解决问题,但我尝试的一切都失败了。我认为这与子弹x-y参数有关吗?

我在名为Bullet的库中有一个Movieclip符号与我的.as文件相同,但脚本似乎没有将子弹添加到场景中。

这是我的船舶和子弹代码。

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();
            }
        }   
    }
}

1 个答案:

答案 0 :(得分:1)

您没有调用updateBullets功能 对updateBullets的唯一调用是...... updateBullets内...从不被调用(如果你在那里调用它自己调用它会导致堆栈溢出)。

您需要将updateBullets函数调用移至update()