dimanche 31 mai 2015

how to reference a hit test object between .as files [AS3]

i'm trying to make a top down shooter game, and have been following tutorials here: http://ift.tt/1MBdRFj and here: http://ift.tt/1GeLkVF

i've managed to get shooting and movement, but i need to get a hit test object to register when the bullet (defined in its own seperate as class file) and the enemy (also defined in seperate file) come into contact. code below:

Enemy code:

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

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

Bullet code:

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 document class code:

 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
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire