ActionScript: Throw bullets to mouse direction

After calculating the angle of 2 graphic objects, you can also use this angle to throw a bullet to the object direction with the trigonometry.

How ?

  1. Calculating the angle (in radian) between 2 graphics objects with the Math.atan2 method (cf. Orient object to the mouse cursor).
  2. On click, display a bullet to the “tank” position and add an “enter frame” event on this bullet.
  3. And save the angle between the tank and the mouse cursor on the new  bullet.
  4. Use this angle on the “enter frame” event to calculate the x and y speed with the cosinus and the sinus.

[swf:/wp-content/uploads/2010/12/www.benoitfreslon.com_Throw_bullets_to_mouse_direction.swf 640 400]

import flash.display.MovieClip;
import flash.geom.Point;
import flash.events.MouseEvent;

// Code by Benoit Freslon.
// Tutorials, Flash games:
// https://www.benoitfreslon.com

// This object will always look at the mouse cursor
myTank.addEventListener(Event.ENTER_FRAME, tankEnterFrame);
// This function will be launched every frame (25 times by seconds);
function tankEnterFrame(pEvt) {
	// pEvt.currentTarget: myTank
	var mc = pEvt.currentTarget;
	// Get the radian angle between the tank and the cursor
	// You can also replace mouseX and mouseY by another coordinates
	var angleRadian = Math.atan2(mouseY - mc.y,mouseX - mc.x);
	// Convert the radian angle in dedree angle
	var angleDegree = angleRadian * 180 / Math.PI;
	// Set the orientation
	mc.rotation = angleDegree;
	// Display angle of rotation in degree
	txtAngle.text = Math.round(angleDegree) + "°";
}

// Add a mouse down event on stage
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);

function mouseDown(pEvent) {
	// Create a new bullet
	var b = new Bullet();
	// Set his position to the tank position
	b.x = myTank.x;
	b.y = myTank.y;
	// Save the randian angle between the mouse and the tank
	// This angle will set the direction of the bullet
	b.angleRadian = Math.atan2(mouseY - myTank.y,mouseX -myTank.x);
	// Add an enter frame event on each bullet
	b.addEventListener(Event.ENTER_FRAME, bulletEnterFrame);
	// Add this display object on the display list
	addChild(b);
}

// Velocity of each bullet
var speed = 8;

function bulletEnterFrame(pEvent) {
	// Get the current object (Bullet)
	var b = pEvent.currentTarget;
	// Move this bullet on each frames
	// On X axis use the cosinus angle
	b.x +=  Math.cos(b.angleRadian) * speed;
	// On Y axis use the sinus angle
	b.y +=  Math.sin(b.angleRadian) * speed;
	// Orient the bullet to the direction
	b.rotation = b.angleRadian * 180 / Math.PI;
	// You have to remove each created bullet
	// So after every moves you must check bullet position
	// If the bullet is out of the screen
	if (b.x < 0 || b.x > 400 || b.y < 0 || b.y > 400) {
		// Remove it from the display list
		removeChild(b);
		// /!\ AND REOMOVE HIS EVENT LISTER
		b.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
	}
}

Download source: www.benoitfreslon.com_Throw_bullets_to_mouse_direction.zip