Question

I have been having this problem for ages and haven't found a solution for this.

Well in flash I have a character who's gun position is similar to this

http://us.123rf.com/400wm/400/400/chudtsankov/chudtsankov1004/chudtsankov100400927/6906625-cartoon-character-mobster-carries-weapon.jpg

I have been having difficulties in try to shoot the bullet in the same angle as the gun is pointing. I've tried working with angles but it just doesn't seem to be working.

Right now, the bullet is shooting straight up but i want it to shoot in an angle.

So far I have this

function shootBullet():void {

    var fire:myBullet2 = new myBullet2();
    fire.x = GirlHero.x;
    fire.y = GirlHero.y;
    addChild(fire);
    fire.addEventListener(Event.ENTER_FRAME, moveBullet);
}

Any help would be much appreciated! Thank you! :) By the way, my character is controlled by the left,right and up key, The up key is to shoot and the left is to walk left and right to walk right.

Was it helpful?

Solution

There's a good example of this at:-

http://rhuno.com/flashblog/2011/11/18/calculating-angles-and-moving-objects-accordingly/

You want to do something like

var angle:int = 270;
var rads:Number = angle*Math.PI/180;
var speed:int = 2;

function moveBullet(e:Event)
{
  e.currentTarget.x += Math.cos(rads) * speed;  
  e.currentTarget.y += Math.sin(rads) * speed;  
}

You'll need to set angle accordingly depending on which way the character is facing (if it flips)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top