1

我正在尝试制作一个符号(为它制作了一个特殊的类)不断下降,直到计时器达到 0。我也希望这块石头在舞台上的随机位置重复并显示。我不知道如何编码。几乎我仍然是动作脚本 3.0 的新手。

这是我到目前为止所拥有的:

主要的

package {
    import flash.display.Sprite;
    import Game_Objects.TinyBird;
    import Game_Objects.Rock;
    import flash.events.Event;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;

    public class Main extends Sprite {

        private var userbird:TinyBird;
        private var obstacle:Rock;

        //containers.
        private var obstacleContainer:Sprite = new Sprite();

        //timers.
        private var enemySpawn:Timer;

        public function Main() {
            // The main code of the game
            // The Bird has to avoid rocks by moving left and right
            // Obstacles = rocks
            // The Birdie will be controlled by the keyboard
            // As long as Birdie alive=1 the loop will continue until alive=0 (where 1=true and 0=false) or timer reaches 0
            // if the bird will hit an object it will die (collision detection)

            this.userbird = new TinyBird(stage.stageWidth/2, stage.stageHeight-20);
            this.obstacle = new Rock;

            obstacleContainer.addChild(obstacle);
            addChild(userbird);

            stage.addEventListener(Event.ENTER_FRAME, startpulse)
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keymove);

            // event to count the passed rocks in order to set score.
            stage.addEventListener(obstacle.KILLED, deadEnemy);
        }

        private function startpulse(evt:Event):void {
            // makes the bird pulsate
            this.userbird.pulse_animation();
        }

        private function keymove(evt:KeyboardEvent):void {
            // the keyboard movements for the bird
            // if leftArrow = pressed -> tiny bird will move left. Else if rightArrow = pressed -> tiny bird will move right
            if (evt.keyCode == Keyboard.LEFT) {
                this.userbird.left();
            } else if (evt.keyCode == Keyboard.RIGHT) {
                this.userbird.right();
            }
        }

        // The obstacle objects are going to fall randomly
        private function spawn(e:TimerEvent):void {
            // calculate a random starting position
            var xPos:Number = Math.floor(Math.random()*stage.stageWidth);
            // calculate a random speed between 2 and 6 
            var speed:Number = Math.floor(Math.random()*4+2) ;
            // create the new rock
            var enemy:Rock = new Rock(xPos, 42, speed, stage.stageWidth, stage.stageHeight);
            // add it to the container
            this.obstacleContainer.addChild(enemy);
            enemy.name = "Rock " + Rock.createdCount;
        }

        private function deadEnemy(e:Event) {
            var obj:Rock = (e.object as Rock);
            this.objectContainer.removeChild(obj)
        }
    }
}

这是摇滚符号:

package Game_Objects {

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

    public class Rock extends Sprite {
        // The Rock is the obstacle that, if colided with the bird, the game is over.

        // public function properties
        static public var _createdCount:int = 0;

        // private function properties
        private var speed:Number;
        private var _score:Number = 4;
        private var scoreCounter:Number = 0;

        // Classes methods
        public static function get createdCount():int {
            return _createdCount;   
        }

        // instance methods
        // Initialization
        public function Rock(x:Number, y:Number, s:Number, maxX:Number = 0, maxY:Number = 0) {
            // set the speed
            this.speed = s;

            // If the rock goes off the stage, then
            if (x < this.width/2) {
                // Put on at left
                this.x = this.width/2;
            // else if x would put the rocks beyond right side of the stage then
            } else if (x > maxX - this.width/2) {
                // Position the rock on the stage.
                this.x = maxX-this.width/2;
            } else {
                // Otherwise position at x
                this.x = x;
            }
            // same for Y
            if (y < this.height/2) {
                this.y = this.height/2;
            } else if (y > maxY - this.height/2) {
                this.y = maxY-this.height/2;
            } else {
                this.y = y;
            }

            // Creating the animation loop in order to repeat the falling motion of the rocks.
            configUI();
            this.addEventListener(Event.ENTER_FRAME, drop);
            // adding a boolean type of loop, taken from 
            // http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001283.html#374074
            this.cacheAsBitmap = true;
            // Add 1 to the public var in order to keep track of the rocks
            _createdCount++;
        }

        // protected function
        protected function configUI():void {

        }

        // private function
        private function drop(e:Event) {
            // in order to show the dropping effect
            // The falling of the rocks
            this.y += this.speed;
            // if at bottom of stage then
            if (this.y-this.height >= stage.stageHeight) {
                // Set score to +1 as reward for not hitting the rock.
                this._score++;
                // Kill the rock that has reached the bottom of the stage
            }
        }
    }
}
4

0 回答 0