Forums

ForumsProgramming Forum

AS3 - Rotating and Shooting

Thread Locked

Posted May 24, '09 at 3:32am

EvilNinja

EvilNinja

6 posts

The problem I'm having is shooting a bullet while rotating like a top down shooter, Also I'm using Flash ActionScript files.

 

Posted May 25, '09 at 2:31am

EvilNinja

EvilNinja

6 posts

Can anyone help me?

 

Posted May 25, '09 at 5:24am

PixelSmash

PixelSmash

567 posts

What exactly is the problem? It doesn't seem that big a problem... if you like, you can make a special 'bullet' class, which manages the movement (and rotation) along the x- and y-axis, and remove itself if it's off the stage.
Once you fire, you add it to the stage's display list, and by using localToGlobal you can get the correct x and y location to start the bullet from.

...which would looks something like this:

public function fireBullet():void{
// fire the gun
var bSpeed:Number = 10;
var bType:String = "pistol";
var bLocation:Point = new Point(x,y);

bLocation = this.localToGlobal(bLocation);

var bullet:Bullet = new Bullet(bSpeed, bType, bLocation, rotation);
stage.addChild(bullet);
}

package{

include flash.display.MovieClip;

class Bullet extends MovieClip{

  var bType:String;
  var bSpeed:Number;
  var bullet_mc:MovieClip;

  public function Bullet(inSpeed,inType,startLocation,inRot):void{
   bType = inType;
   bSpeed = inSpeed;
   rotation = inRot;
   switch(bType){
    case "pistol":
     bullet_mc = new PistolBullet();
    break;
    default: trace("wrong bullet MC switch");
   }
   addChild(bullet_mc);
   bullet_mc.addEventListener(Event.ENTER_FRAME, bulletFunctions);
  }

  private function bulletFunctions(e:Event):void{
   x = Math.sin(rotation*Math.PI)*bSpeed;
   y = Math.cos(rotation*Math.PI)*bSpeed;
   if(x < 0 || x > stage.width || y < 0 || y > stage.height){
    remove();
   }
  }
}
}

Now, this isn't everything, and it'll probably give a whole lot of errors... but it should make the general idea pretty clear. Let me know if you still run into problems after some tweaking!

 

Posted May 26, '09 at 3:26am

EvilNinja

EvilNinja

6 posts

I did still run into some problems.
One - The bullets didn't start at the end of the gun.
Two - The bullets still didn't move

 

Posted May 26, '09 at 3:34am

Sparkky

Sparkky

15 posts

make sure your adding the event listener so it can call the bulletFunctions every time the frame is redrawn.

 

Posted May 26, '09 at 3:53am

EvilNinja

EvilNinja

6 posts

Here is my rotation code:

package {
   
    //List of imports
    import flash.display.MovieClip;
    import flash.events.Event;
   
    //Shooting Class
    public class Shooting extends MovieClip {
        //List of variables
        var PistolGun:MovieClip;
        var Angle:Number;
       
        //Constructor function
        public function Shooting() {
            //Create PistolGun and place it on the stage
            PistolGun = new Pistol;
            PistolGun.x = stage.stageWidth / 2;
            PistolGun.y = stage.stageHeight / 2;
            stage.addChild(PistolGun);
            //GunRotate EventListener
            stage.addEventListener(Event.ENTER_FRAME, GunRotate);
        }
       
        public function GunRotate(event:Event){
            //Find the right angle
            Angle = Math.atan2(mouseY-PistolGun.y, mouseX-PistolGun.x);
            Angle *= 180/Math.PI;
            //Rotate the PistolGun to the mouses angle
            PistolGun.rotation = Angle + 90;
        }
    } //End of Shooting class
} //End of Package

 

Posted May 28, '09 at 7:17am

PixelSmash

PixelSmash

567 posts

To adress your problem of the bullets not starting at the end of the gun: that's right... they wouldn't. If I'm correct, they should start at the registration point of the gun, right? Simple to fix... add a 'bulletAppear' movieclip, and instead of using new Point(x,y) use new Point(bulletAppear.x,bulletAppear.y).

The bullets not moving is probably because of a small error on my side: instead of using x = ... and y = ... you should use x += ... and y += ... - though perhaps they should be minus, I can't remember.

 

Posted May 28, '09 at 8:10am

EvilNinja

EvilNinja

6 posts

The bullets just get faster and faster AND faster every time you shoot. When I add x '-''+'= or y '-''+'= can you help?

 

Posted May 29, '09 at 5:57am

PixelSmash

PixelSmash

567 posts

Could it be that you remove the movieclip off the display list, but still retain the event listener? This way every time you fire, you add an extra ENTER_FRAME to the bullet_mc... which would make it speed up every time :)

 

Posted May 29, '09 at 9:54pm

EvilNinja

EvilNinja

6 posts

I found what I did wrong I did Bullet.addEventListener(Event.ENTER_FRAME, BulletMove); when it should of been stage.addEventListener(Event.ENTER_FRAME, BulletMove); thanks for all your help.