- Home
- Tutorials
- Flash
- Intermediate
- Shooting Bullets
Shooting Bullets

Turret Controls, and unbroken code
Andy Crockett
Loved writing code since I started a couple months ago, and I get pissed to see the lack of useful tutorials for widely used subjects on the web.
View all articles by Andy CrockettI did a little something like this:
turret.onEnterFrame=function(){
if(Key.isDown(Key.LEFT)){
this._rotation-=5;
};
if(Key.isDown(Key.RIGHT)){
this._rotation+=5;
};
if(Key.isDown(Key.SPACE)){
fire();
};
};
This controls the turret alone, but is done on the frame actions so it can interact with the global variables without using _root.
As you can see when the respective keys are pressed, the rotation can either be added to or subtracted.
And fire(); can be executed only when space is pressed.
I hope this really helped you to understand this effective way of "Shooting Bullets".
Here's the code unbroken:
var bulletSpeed:Number=5;
var xSpeed:Number=0
var ySpeed:Number=0
var reloadSpeed:Number=100;//milliseconds
var bulletOffset:Number=10;//pixels
var randomNum:Number=0;
var life:Number=0;
var totalLife:Number=50;//frames
var reloaded:Boolean=true;
var x:Number=0;
turret.onEnterFrame=function(){
if(Key.isDown(Key.LEFT)){
this._rotation-=5;
};
if(Key.isDown(Key.RIGHT)){
this._rotation+=5;
};
if(Key.isDown(Key.SPACE)){
fire();
};
};
function fire(){
if(reloaded){
x=_root.getNextHighestDepth();
var bullet:MovieClip=_root.attachMovie("bullet", "bullet"+x, x);
bullet._x=turret._x;
bullet._y=turret._y;
randomNum=random(bulletOffset)-bulletOffset/2;
bullet._rotation=turret._rotation+randomNum;
bullet.xSpeed=Math.cos(Math.PI/180 * bullet._rotation)*bulletSpeed;
bullet.ySpeed=Math.sin(Math.PI/180 * bullet._rotation)*bulletSpeed;
bullet.life=0;
bullet.onEnterFrame=function(){
this._x+=this.xSpeed;
this._y+=this.ySpeed;
this.life++;
if(this.life>totalLife){
this.removeMovieClip();
this.unloadMovie();
};
};
reload();
};
};
function reload(){
reloaded=false;
timer=setInterval(this, "Reloaded", reloadSpeed);
}
function Reloaded(){
clearInterval(timer);
reloaded=true
}
Have fun with this!
Leave a comment and I'll try to help you out.
Spread The Word
Attachments
17 Responses to "Shooting Bullets" 
|
said this on 19 Apr 2009 7:30:40 PM CST
when I try to shoot the b
|
|
said this on 20 Apr 2009 10:38:40 PM CST
Hi David, I'm taking a gu
|
|
said this on 21 Apr 2009 7:12:58 AM CST
Cheak the code. Should be
this._x+=thi this._y+=this. enjoy this fan |
|
said this on 02 May 2009 10:31:01 PM CST
Mine doesn't work at all.
|
|
said this on 01 Oct 2010 2:45:56 AM CST
mine doesn't work at all
|
|
said this on 18 May 2009 6:04:02 AM CST
You most likely didn't ad
How do |
|
said this on 13 Jun 2009 1:46:23 PM CST
To change where the bulle
|
|
said this on 06 Jun 2009 3:14:08 PM CST
Hey there,
The swf sam How come? T |
|
said this on 23 Jun 2009 6:41:04 AM CST
How do I get the bullets
va va var var b function var b bulle bulletMc._x = bullet } function moveBullets(){ if (bulletReady && Key. bu cur } els if (currentTime+bul } for (var i = 0; i bulletArray[i]._y -= } } thi moveBullets(); }; this code doesnt work |
|
said this on 12 Jul 2009 7:30:18 PM CST
my turret is moving aroun
|
|
said this on 16 Jul 2009 3:47:09 AM CST
how about some enemies fo
what is |
|
said this on 16 Jul 2009 2:47:19 PM CST
How do i make the bullets
|
|
said this on 06 Apr 2010 12:52:20 PM CST
mine is not working, i ha
|
|
said this on 20 Jul 2009 2:05:46 PM CST
Ok everyone, i updated th
|
|
said this on 11 Dec 2009 8:11:37 PM CST
My bullet only fires once
|
|
said this on 31 Aug 2010 1:01:19 AM CST
Does anyone have this fil
|
|
said this on 04 Feb 2011 12:39:19 PM CST
I am trying..... v hard t
I can get it cld u |


Author/Admin)
