ForumsProgramming ForumAS3 - Rotating and Shooting

10 8745
EvilNinja
offline
EvilNinja
6 posts
Nomad

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

  • 10 Replies
EvilNinja
offline
EvilNinja
6 posts
Nomad

Can anyone help me?

PixelSmash
offline
PixelSmash
566 posts
Nomad

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 = &quotistol";
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 &quotistol":
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!

EvilNinja
offline
EvilNinja
6 posts
Nomad

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

Sparkky
offline
Sparkky
14 posts
Nomad

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

EvilNinja
offline
EvilNinja
6 posts
Nomad

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

PixelSmash
offline
PixelSmash
566 posts
Nomad

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.

EvilNinja
offline
EvilNinja
6 posts
Nomad

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

PixelSmash
offline
PixelSmash
566 posts
Nomad

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

EvilNinja
offline
EvilNinja
6 posts
Nomad

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.

PixelSmash
offline
PixelSmash
566 posts
Nomad

Hmm, hadn't even thought of that myself. Glad it worked out though! Good luck with the rest of your project, and show us some results when it's possible

Showing 1-10 of 10