Help With Shooting Logic

Hey guys, I’m working on a platform game and I’m currently trying to write a function to shoot bullets.

Basically what I want to happen is when the user holds down the space bar, the bullet attaches to the character, a power bar slowly moves up, and when the space bar is released the bullet shoots an x amount of speed and distance depending on the current frame of the powerbar mc.

Heres my logic:

  • On space bar press play the powerbar mc, attach the bullet mc to the character, contain the x position of the bullet
  • on bullet enter frame stop the powerbar mc, write a bunch of if statements that say if powerbar current frame is so and so then tell the bullet to go this fast and this far
  • if bullet x position is x amount of distance greater than it’s original position, then increase the y position (to create some gravity, falling down effect)
  • Finally, if y position of the bullet is equal to Stage.height then delete the bullet

I’ve almost got it working with the following code:

 
function shootbullet() {
power_mc.play();
var bx = bullet1._x;
var bpower:Number = 0;
attachMovie("bullet", "bullet1", bullet1.getNextHighestDepth(), {_x:hero._x+10, _y:hero._y-25});
 
bullet1.onEnterFrame = function() {
power_mc.stop();
if (power_mc._currentframe<5) {
bpower = 4;
if (this._x>bx+25) {
	this._y *= 1.01;
} 
}
else if (power_mc._currentframe<10) {
bpower = 6;
if (this._x>bx+50) {
	this._y *= 1.01;
	shooting = false;
}
 
}
else if (power_mc._currentframe<15) {
bpower = 8;
if (this._x>bx+75) {
	this._y *= 1.01;
}
}
else if (power_mc._currentframe<20) {
bpower = 10;
if (this._x>bx+100) {
	this._y *= 1.01;
}
}
else if (power_mc._currentframe<25) {
bpower = 11;
if (this._x>bx+75) {
	this._y *= 1.01;
}
}
else if (power_mc._currentframe<30) {
bpower = 12;
if (this._x>bx+125) {
	this._y *= 1.01;
}
}
else if (power_mc._currentframe<35) {
bpower = 13;
if (this._x>bx+150) {
	this._y *= 1.01;
}
}
else if (power_mc._currentframe<40) {
bpower = 14;
if (this._x>bx+175) {
	this._y *= 1.01;
}
}
else if (power_mc._currentframe<45) {
bpower = 15;
if (this._x>bx+200) {
	this._y *= 1.01;
}
}
else if (power_mc._currentframe<50) {
bpower = 15;
if (this._x>bx+250) {
	this._y *= 1.01;
}
}
else if (power_mc._currentframe<=55) {
bpower = 20;
if (this._x>bx+300) {
	this._y *= 1.01;
}
}
this._x += bpower;
if (this._y>Stage.height-20) {
this.removeMovieClip();
}
};
}

and then of course space bar press it just calls shootbullet();

It may seem like a lot of code and I could of just said something like
bpower = power_mc._currentframe / 10;
but I wanted to manually edit each speed and distance.
and also the game is sort of retro style where the power bar moves up in clunky chunks rather than a slow smooth gradual increase.

Anyways, so it works pretty well but there are two problems that I can’t figure out.

  1. I can’t seem to figure out how to slowly decrease the power bar back to the first frame immediately after the space bar is released.
  2. I want to be able to shoot another bullet about 1 or 2 seconds after you shoot, but right now if you shoot the bullet thats currently in the air just comes back to the same place. So how do I make it so each time I shoot it shoots a new instance of the bullet.

Sorry if this is long and confusing, I’m fairly new to actionscripting as you can probably tell.