Groups | Blog | Home
all groups > flash actionscript > june 2004 >

flash actionscript : Firing a Bullet


isthatlegal
6/8/2004 8:17:52 PM
I am making a platformy style game, and i am wanting to fire a "bullet" from my
charactor, only i want the bullet to fire towards the mouse cursor...

could sumone please give me the script for this, only with comments, as i wish
to understnad the script, not just use it..


thanks a lot for any help

Dan
Will Brubaker
6/8/2004 9:18:55 PM
Well I don't have any code for something like that, but I do have a suggestion
on how you could implement it.

Try this:

Create a movie clip in your library, named "bullet" or whatever. Then have it
contain an animated bullet which will start at the origin and move along the
x-axis until it reaches however far you want your bullets to go.

Using ActionScript, you can then place a bullet at the location of your
character when he shoots or whatever, and then immediately adjust the rotation
of the bullet movie to an angle corresponding to the line between the cursor
and the player's character.

Now here's the cool part: if in the final frame of your bullet movie clip you
put

this.removeMovieClip();

it will destroy the movie clip on its final frame, so you don't have to deal
with deleting it yourself.
be forewarned, though, that the removeMovieClip function will ONLY work on
movie clips that have been created using actionscript, and will NOT work on
clips that have been placed using the Flash editing tools

Drawbacks of this method are that if you want to use hittests to detect if the
bullet hits anything, you'll need a pretty high frame rate to avoid the bullet
"jumping" over solid objects if it's moving at any reasonable speed, or I
suppose you can have the actual bullet graphic surrounded by a fill with the
alpha channel = 0 so that it would register on a hit test but not be visible in
the movie.

Also, you'll have to keep track of what level of the parent movie you've
spawned bullets into, lest you spawn one into an occupied level and erase
something else in the process.

-Will-
isthatlegal
6/8/2004 10:00:35 PM
Timothée_Groleau
6/9/2004 10:09:14 AM
Here is a very simple code sample. You will want to adjust it to do
whatever you want.

================================================
// assuming symbol with linked name "bullet" exists in library
// assuming movieclip named "gun" is on stage and its center point
// in the middle of the gun

// length of vector representing the strength at the exit of the gun
gun.strength=10;

// functions that alows the bullet to move
// must be assigned to the bullet as onEnterFrame
// on bullet movieclip when fired
gun.$bulletPath = function() {
this._x += this.dx;
this._y += this.dy;
}

gun.fire = function() {
var d = this._parent.depth++;
var b = this._parent.attachMovie(
"bullet",
"b" + d,
d,
{_x: this._x, _y: this._y}
);

// find direction bullet must take, relative to gun
// by the way you'll need to modify gun's rotation too
var dx = this._parent._xmouse - this._x;
var dy = this._parent._ymouse - this._y;

// calculate ratio of mouse distance relative to strength
var ratio = this.strength / Math.sqrt(dx*dx + dy*dy);

// adjust movement steps for bullet
b.dx = dx * ratio;
b.dy = dy * ratio;

// bullet to start moving
b.onEnterFrame = this.$bulletPath;
}

// on Mouse Down, fire!
// you can call fire from whatever event you like
gun.onMouseDown = gun.fire;

================================================

Tim.




[quoted text, click to view]
AddThis Social Bookmark Button