在AS3中使落下的物体以随机角度出现

时间:2012-12-06 15:02:29

标签: image actionscript-3 object random angle

我正在进行导弹防御型游戏,并试图让导弹以随机角度落下。我还需要子弹图像以我拍摄的角度转动。我对AS3中的角度非常不熟悉,所以我需要一些帮助。

代码:         

        import spark.components.Image;

        public var missiles:Array;
        public var bullets:Array;
        public var playerLife:Number;

        public var targetX:Number;
        public var targetY:Number;

        public function init():void {
            startGame();
        }

        public function onEnterFrame(e:Event):void { 

            if(Math.random() <.05 ){

                //make a new missle
                var newMissile:Image = new Image();

                //draw to is
                newMissile.source = "assets/missileDown.jpg";

                //position it
                newMissile.x = Math.random() * stage.stageWidth;


                //animate it
                newMissile.addEventListener(Event.ENTER_FRAME, onMissileEnterFrame);

                //add it to missle array
                missiles.push(newMissile);

                //add it to the screen
                gameGroup.addElement(newMissile);
            }
        }

        public function startGame():void {

            //makes new arrays
            //gets rid of old arrays
            missiles = new Array();
            bullets = new Array();

            //set player life
            playerLife = 5;

            //show player life
            playerHealth.text = String(playerLife);

            //add animation and mouse interation
            this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
            stage.addEventListener(MouseEvent.CLICK, fireWeapon);

            //set game over alpha
            gameEnd.alpha = 0;
            reset.alpha = 0; 

            //set game start alpha
            playerHealth.alpha = 1;
            healthLabel.alpha = 1;
        }

        //updates the missle
        public function onMissileEnterFrame(e:Event):void {

            //reference to target
            var targetMissile:Image = Image(e.currentTarget);

            //move missle down
            targetMissile.y += 10;

            //if missle has gone too far, remove it and player loses life
            if(targetMissile.y > stage.stageHeight) {
                playerLife --;
                removeMissile(targetMissile);

                //show player life
                playerHealth.text = String(playerLife);
            }

            //if player is dead, game over
            if(playerLife <= 0) {
                gameOver();
            }

        }

        //update bullet
        public function onBulletEnterFrame(e:Event):void {

            //get reference to bullet
            var thisBullet:Bullet = Bullet(e.currentTarget);

            //animate towards point..

            //calculate difference between current position and desired position
            var diffX:Number =  thisBullet.targX - thisBullet.x;
            var diffY:Number =  thisBullet.targY - thisBullet.y;

            //move 10% of difference closer
            thisBullet.x += diffX * .1;
            thisBullet.y += diffY * .1;

            //chekc for overlap between bullet and missles
            for(var i:Number = 0; i < missiles.length; i++) {

                //if they do overlap, remove missle
                if( thisBullet.hitTestObject(missiles[i]) ) {
                    removeMissile(missiles[i]);
                    removeBullet(thisBullet);
                    break;
                }
            }

            //if we're 'close enough' to the target position, remove bullet
            if(Math.abs(diffX) <  10 && Math.abs(diffY) < 10) {
                removeBullet(thisBullet);
            }
        }

        //gets rid of a missle
        public function removeMissile(targetMissile:Image):void {

            //removes the missle from the missiles array
            for(var i:Number = missiles.length - 1; i >= 0; i--) {
                if(missiles[i] == targetMissile) {
                    missiles.splice(i,1);
                    break;
                }
            }

            //don't animate anymore
            targetMissile.removeEventListener(Event.ENTER_FRAME, onMissileEnterFrame);

            //remove from stage
            gameGroup.removeElement(targetMissile);
        }

        //removes bullet from stage
        public function removeBullet(targetBullet:Bullet):void {

            //stop animation
            targetBullet.removeEventListener(Event.ENTER_FRAME, onBulletEnterFrame);

            //remove from stage
            gameGroup.removeElement(targetBullet);
        }

        //shoot a bullet at the mouse position
        public function fireWeapon(e:MouseEvent):void {

            //make a new bullet
            var newBullet:Bullet = new Bullet();
            newBullet.addEventListener(Event.ENTER_FRAME, onBulletEnterFrame);

            //position near the earth in the center
            var halfStage:Number = stage.stageWidth / 2;
            newBullet.x = halfStage;
            newBullet.y = 500;

            //set target 
            newBullet.targX = stage.mouseX;
            newBullet.targY = stage.mouseY;

            //add it to the stage
            gameGroup.addElement(newBullet);

        }

        //you lose
        public function gameOver():void {

            //remove missles
            for(var i:Number = 0; i < missiles.length; i++) {
                removeMissile(missiles[i]);
            }

            //stop interaction
            stage.removeEventListener(MouseEvent.CLICK, fireWeapon);

            //stop animation
            this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);

            //set game start alpha
            playerHealth.alpha = 0;
            healthLabel.alpha = 0;

            //set game end alpha
            gameEnd.alpha = 1;
            reset.alpha = 1; 
        }

    ]]>
</fx:Script>

1 个答案:

答案 0 :(得分:0)

的onEnterFrame

...
//position it
newMissile.x = Math.random() * stage.stageWidth;
//rotate it
newMissile.rotation = - (Math.random() * 60 - 30);

onMissileEnterFrame

...
//move missle down
//targetMissile.y += 10;
targetMissile.x -= 10 * Math.sin(targetMissile.rotation * Math.PI/180);
targetMissile.y += 10 * Math.cos(targetMissile.rotation * Math.PI/180);

fireWeapon

...
//set target 
newBullet.targX = stage.mouseX;
newBullet.targY = stage.mouseY;
newBullet.rotation = - Math.atan((newBullet.x - newBullet.targX) / (newBullet.y - newBullet.targY)) * 180/Math.PI;