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 = "istol"; 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 "istol": 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!
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
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.
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
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.
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