PDA

View Full Version : [AS3] Bullet Help


Wezlou
03-28-2008, 05:39 PM
Ok so I have this code

stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);

function shoot(e:Event):void{
var Bullet:bullet = new bullet();
Bullet.x = mouseX;
Bullet.y = mouseY;
Bullet.y = Bullet.y - 5;
addChild(Bullet);
}

The problem is that the bullet doesn't move it just stays idle.

Thanks for any help :)

Mazoonist
03-28-2008, 06:34 PM
Hey, Wezlou,

Welcome to actionscript programming! The bullet doesn't move because the command that you think is going to make the bullet move only happens once:
Bullet.y = Bullet.y - 5;
In order for this command to happen more than once, on a continuous basis, you have to put it into an event that fires continually. ENTER_FRAME is one choice, a Timer event is another. Learn the ENTER_FRAME technique first, though, because it's easiest:
stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);
function shoot(e:Event):void {
var Bullet:bullet = new bullet();
Bullet.x = mouseX;
Bullet.y = mouseY;
addChild(Bullet);
Bullet.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
function enterFrameHandler(event:Event) {
event.target.y = event.target.y - 5;
}
Notice that inside the enter frame event handler function, the name "Bullet" is not used, instead "event.target" is used. Why? Well, you can't use the name "Bullet" anymore, because that variable was created inside another function, and outside of that function the name "Bullet" is not known anymore. The variable name has gone "out of scope." However, "event.target" can be extremely handy, because it lets you re-use the same listener function. Inside the function, "event.target" is like saying "whatever object was given this handler as a listener." So it makes it more anonymous. In fact, if you keep clicking, you keep getting another bullet, and each succeeding one gets this same handler assigned to it.

There is now another problem though. Each bullet simply keeps going, up and up and up, right off the screen. And, for all you know, it's still going up into negative y locations as long as your program keeps running. This consumes memory unnecessarily, and to avoid it you might do something like this:
stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);
function shoot(e:Event):void {
var Bullet:bullet = new bullet();
Bullet.x = mouseX;
Bullet.y = mouseY;
addChild(Bullet);
Bullet.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
function enterFrameHandler(event:Event) {
event.target.y = event.target.y - 5;
if(event.target.y < 20) {
event.target.removeEventListener(Event.ENTER_FRAME , enterFrameHandler);
removeChild(MovieClip(event.target));
}
}

In the above, I set the determinant y location at "< 20" so that you can watch it disappear. First the enter frame event listener is removed from it, then it is removed from the display list.

Wezlou
03-28-2008, 06:44 PM
Wow that helped me out alot!, Thank you so much for the help I appreciate it alot!!!.

Thank you!

Mazoonist
03-28-2008, 07:02 PM
Haha. No problem, I had some extra time today. Warning: This stuff is addicting. Pretty soon you'll be shooting enemies.