Basically what happens is when I shoot a bullet, it creates the bullet. However, if I click again while the bullet it still on screen, it removes the bullet and makes a completely new one. So now only one bullet can be on the screen at a time. How do I fix this?
The bullet in the library has the instance name of ‘bullet’
package
{
public class GameEnter extends MovieClip
{
public function GameEnter()
{
addEventListener(MouseEvent.MOUSE_DOWN, shootBullet);
}
public var _bullet1:bullet = new bullet;
public var angleRadian = Math.atan2(mouseY - 300,mouseX - 300);
public var angleDegree = angleRadian * 180 / Math.PI;
public var speed1:int = 10;
public function shootBullet(evt:MouseEvent)
{
_bullet1.x = 300;
_bullet1.y = 300;
_bullet1.angleRadian = Math.atan2(mouseY - 300,mouseX -300);
_bullet1.addEventListener(Event.ENTER_FRAME, bulletEnterFrame);
addChild(_bullet1);
}
public function bulletEnterFrame(evt:Event)
{
var _bullet1 = evt.currentTarget;
_bullet1.x += Math.cos(_bullet1.angleRadian) * speed1;
_bullet1.y += Math.sin(_bullet1.angleRadian) * speed1;
_bullet1.rotation = _bullet1.angleRadian*180/Math.PI;
if (_bullet1.x < 0 || _bullet1.x > 600 || _bullet1.y < 0 || _bullet1.y > 600)
{
removeChild(_bullet1);
_bullet1.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
}
}
}
}
Well think about it, you store your _bullet1 variable in the class, which is fine, but every time you shoot you use the same variable and just change the x and y position again.
A better way to do this is to handle the animation for the bullet in the bullet class itself.
When you shoot, you just create a new bullet and add it to the stage.
In case you still want to use this way, it should be more like this:
But the best way would be to go with option 1