i am trying to make a “double-barrel” firing cannon in AS3…
below is a crappy drawing of what im trying to acheive…
im having trouble making the two bullets come out where they should (1 on the left barrel, 1 on the right barrel).
prior to this, i already successfully created a standard “one-barrel” turret that rotates depending on the mouse location and fires 1 bullet per shot… what i’m trying to do now is (like i said above) to create a double-barrel turret that would fire 2 bullets per shot (1 on the gun1 and 1 on gun2)…
if someone has already done this, maybe you can help me out in modifying my code…
below is the code i used for the single-barrel turret:
on the timeline:
stage.addEventListener(Event.ENTER_FRAME, followMouse);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fireBullet);
var radians:Number;
function followMouse(event:Event):void
{
radians = Math.atan2(mouseY - turret.y, mouseX - turret.x);
var degrees:Number = radians / Math.PI * 180;
turret.rotation = degrees;
}
function fireBullet(event:KeyboardEvent):void
{
var newBullet:MovieClip = new Bullet();
newBullet.x = Math.cos(radians) * 93.5 + turret.x;
newBullet.y = Math.sin(radians) * 93.5 + turret.y;
newBullet.rotation = turret.rotation;
addChild(newBullet);
}
on the Bullet class
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
public class Bullet extends MovieClip
{
var xSpeed:Number;
var ySpeed:Number;
var angle:Number;
public var speed:Number;
public function Bullet()
{
speed = 30;
this.addEventListener(Event.ADDED, initializeBullet);
}
function initializeBullet(event:Event):void
{
angle = this.rotation / 180 * Math.PI;
xSpeed = Math.cos(angle) * speed;
ySpeed = Math.sin(angle) * speed;
this.addEventListener(Event.ENTER_FRAME, moveBullet);
}
function moveBullet(event:Event):void
{
if (this.x > 550)
{
this.removeEventListener(Event.ENTER_FRAME, moveBullet);
this.parent.removeChild(this);
}
else
{
this.x += xSpeed;
this.y += ySpeed;
}
}
}
}