如何在.as文件之间引用命中测试对象[AS3]

时间:2015-06-01 03:42:08

标签: actionscript-3

我试图做一个自上而下的射击游戏,并且一直在这里关注教程:http://gamedev.michaeljameswilliams.com/2008/09/17/avoider-game-tutorial-1/ 在这里:as3gametuts.com/2013/07/10/top-down-rpg-shooter-4-shooting /

我设法得到射击和移动,但我需要得到一个命中测试对象,以便在子弹(在它自己单独定义为类文件)和敌人(也在单独的文件中定义)来注册联系。代码如下:

敌人代码:

        package 
{
    import flash.display.MovieClip;
    public class Enemy extends MovieClip 
    {
        public function Enemy() 
        {
            x = 100;
            y = -15;            
        }

        public function moveDownABit():void 
        {
            y = y + 3;
        }
    }
}  

子弹代码:

package
{
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.utils.Timer;
    import flash.events.TimerEvent;


    public class Bullet extends MovieClip
    {
        private var stageRef:Stage;
        private var speed:Number = 10;
        private var xVel:Number = 0;
        private var yVel:Number = 0;
        private var rotationInRadians = 0;
        public var enemy:Enemy;



        public function Bullet(stageRef:Stage, X:int, Y:int, rotationInDegrees:Number):void
        {
            this.stageRef = stageRef;
            this.x = X;
            this.y = Y;

            this.rotation = rotationInDegrees;
            this.rotationInRadians = rotationInDegrees * Math.PI / 180;
        }

        public function bullethit():void{
                if (Bullet.hitTestObject(enemy)){
                    gameTimer.stop();
                }

            }

        public function loop():void
        {
            xVel = Math.cos(rotationInRadians) * speed;
            yVel = Math.sin(rotationInRadians) * speed;

            x += xVel;
            y += yVel;

            if(x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0)
            {
                this.parent.removeChild(this);
            }
        }
    }
}

Main.as文档类代码:

 package
{
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.Timer;
    import flash.events.TimerEvent;

    public class Main extends MovieClip
    {
        public var player:Player;
        public var bulletList:Array = []; //new array for the bullets
        public var enemy:Enemy;
        public var gameTimer:Timer;

        public function Main():void
        {
            player = new Player(stage, 320, 240);
            stage.addChild(player);

            enemy = new Enemy();
            addChild( enemy );

            gameTimer = new Timer( 25 );
            gameTimer.addEventListener( TimerEvent.TIMER, moveEnemy );
            gameTimer.start();

            stage.addEventListener(MouseEvent.CLICK, shootBullet, false, 0, true);
            stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true); //add an EventListener for the loop
        }

        public function moveEnemy( timerEvent:TimerEvent ):void 
        {
            enemy.moveDownABit();
        }

        public function loop(e:Event):void //create the loop function
        {
            if(bulletList.length > 0) //if there are any bullets in the bullet list
            {
                for(var i:int = bulletList.length-1; i >= 0; i--) //for each one
                {
                    bulletList[i].loop(); //call its loop() function
                }
            }
        }

        public function shootBullet(e:MouseEvent):void
        {
            var bullet:Bullet = new Bullet(stage, player.x, player.y, player.rotation);
            bullet.addEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved, false, 0, true); //triggers the "bulletRemoved()" function whenever this bullet is removed from the stage
            bulletList.push(bullet); //add this bullet to the bulletList array
            stage.addChild(bullet);
        }

        public function bulletRemoved(e:Event):void
        {
            e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved); //remove the event listener so we don't get any errors
            bulletList.splice(bulletList.indexOf(e.currentTarget),1); //remove this bullet from the bulletList array
        }
    }
}

1 个答案:

答案 0 :(得分:1)

正如Vesper所说,你想要在public function loop(e:Event):void //create the loop function { if(bulletList.length > 0) //if there are any bullets in the bullet list { for(var i:int = bulletList.length-1; i >= 0; i--) //for each one { bulletList[i].loop(); //call its loop() function // check to see if the enemy has been hit if(enemy.hitTestObject(bulletList[i])) { // the enemy has been hit by the bullet at index i } } } } 课程中进行检查。您已经设置了游戏循环,因此您只需在其中添加支票:

enemy

由于您目前只有一个enemy,因此您只需针对该PointF^测试每个项目符号。如果你有更多的敌人,你想要保留一系列对这些敌人的引用并做一个嵌套循环,检查是否有任何敌人被任何子弹击中。