In the last tutorial I showed you how to make a turret fire a bullet that doesn’t move. This is little help when creating a turret game, but it leads us on to the next part.
If you think about what exactly we want the bullet to do it is quite simple. All we want it to do is move in a constant speed in the direction that the mouse is pointing. If you look back at the previous tutorial at the createBullet function you can see that the x coordinate is controlled by cos and the y by sin.
createBullet(turret.x+(40*Math.cos((turret.rotation+90)*degreesToRadians)),turret.y+(40*Math.sin((turret.rotation+90)*degreesToRadians)));
We are going to use the same concepts when making the bullet move.
Can you remember how we used 40*Math.cos to make the bullet appear at the end of the turret, well we are going to increase the x coordinate of the bullet by that, whilst replacing the 40 with the speed of the bullet.
To do this we need to make a function that is triggered every frame that increases the bullets position when triggered. We are going to put this new code into the create bullet function.
var bulletGo = bulletHolder.addChild(bullet);
bulletGo.currentTurretRotation = turret.rotation+90;
bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
function moveBullet(event:Event):void {
bulletGo.x+=Math.cos((bulletGo.currentTurretRotation)*degreesToRadians);
bulletGo.y+=Math.sin((bulletGo.currentTurretRotation)*degreesToRadians);
}
line 1: Creating a variable to handle adding the child of the bullet to the stage. This is to make the variables associated with it independent each time the variables are set. This stops the bullet from changing direction due to the turrets rotation.
line 2: Setting the current rotation variable to the turrets rotation.
line 3: Adding the Enter frame listener to move the bullet.
line 4: Setting up the moveBullet function that is triggered by the event listener in the line above.
line 5: Using the bullet child handler that i set up earlier to move the bullets x coordinate by Math.cos( the rotation of the turret, set by the currentRotation variable in line 2, in radians. As i said before i move the bullet though this handler so that each bullet moves independently of each other.
line 6: Just like above i move the bullets y coordinate by the same method, but use Math.sin.
Full code
-
stage.frameRate = 30;>
-
var angle:Number = 0;
-
var radiansToDegrees:Number = 180/Math.PI;
-
var degreesToRadians:Number = Math.PI/180;
-
var currentTurretRotation:Number;
-
var bulletHolder:MovieClip = new MovieClip();
-
addChild(bulletHolder);
-
var turret:MovieClip = new MovieClip();
-
turret.graphics.beginFill(0xFFFFFF);
-
turret.graphics.lineStyle(2, 0xFFFFFF);
-
turret.graphics.drawRect(-10, 0, 20, 40);
-
turret.graphics.endFill();
-
turret.graphics.beginFill(0xFFFFFF);
-
turret.graphics.drawCircle(0,0,20);
-
turret.graphics.endFill();
-
turret.x = 275;
-
turret.y = 200;
-
addChild(turret);
-
function createBullet(startx:Number, starty:Number) {
-
var bullet:MovieClip = new MovieClip();
-
bullet.graphics.lineStyle(2, 0xFFFFFF);
-
bullet.graphics.drawCircle(0, 0, 5);
-
bullet.x = startx;
-
bullet.y = starty;
-
var bulletGo = bulletHolder.addChild(bullet);
-
bulletGo.currentTurretRotation = turret.rotation+90;
-
bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
-
function moveBullet(event:Event):void {
-
bulletGo.x+=Math.cos((bulletGo.currentTurretRotation)*degreesToRadians);
-
bulletGo.y+=Math.sin((bulletGo.currentTurretRotation)*degreesToRadians);
-
}
-
}
-
turret.addEventListener(Event.ENTER_FRAME, rotateTurret);
-
function rotateTurret(event:Event):void {
-
angle = Math.atan2(mouseY-turret.y, mouseX-turret.x);
-
angle *= radiansToDegrees;
-
turret.rotation = angle - 90;
-
}
-
stage.addEventListener(MouseEvent.CLICK, shoot);
-
function shoot(event:Event) {
-
createBullet(275+(40*Math.cos((turret.rotation+90)*degreesToRadians)),200+(40*Math.sin((turret.rotation+90)*degreesToRadians)));
-
}
changes to previous code:
line 4: I am creating a variable called currentTurretRotation, which you know is where i hold the rotation of the turret when the mouse is clicked.
Example.
In the next tutorial i will show you how to delete the bullets when they go offstage, and maybe add some things to shoot.


