Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-28-2008, 05:39 PM   #1
Wezlou
Registered User
 
Join Date: Mar 2008
Posts: 3
Default [AS3] Bullet Help

Ok so I have this code

ActionScript 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
Wezlou is offline   Reply With Quote
Old 03-28-2008, 06:34 PM   #2
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

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:
ActionScript Code:
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:
ActionScript Code:
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:
ActionScript Code:
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.
__________________
My Tutorials * My Website
Mazoonist is offline   Reply With Quote
Old 03-28-2008, 06:44 PM   #3
Wezlou
Registered User
 
Join Date: Mar 2008
Posts: 3
Default

Wow that helped me out alot!, Thank you so much for the help I appreciate it alot!!!.

Thank you!
Wezlou is offline   Reply With Quote
Old 03-28-2008, 07:02 PM   #4
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

Haha. No problem, I had some extra time today. Warning: This stuff is addicting. Pretty soon you'll be shooting enemies.
__________________
My Tutorials * My Website
Mazoonist is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[AS2] Arrays and hitTests and bullets CheckersMcGavern Gaming and Game Development 4 02-25-2012 04:34 AM
Array help CheckersMcGavern ActionScript 2.0 0 03-10-2008 01:26 PM
Game help: Bullet yopug Gaming and Game Development 3 08-01-2007 10:08 PM
Collision between bullet & alien richo ActionScript 1.0 (and below) 3 05-19-2007 10:29 AM
Bullet holes using duplicateMovieClip? exfuga ActionScript 1.0 (and below) 1 11-29-2003 11:41 AM


All times are GMT. The time now is 03:57 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.