Groups | Blog | Home
all groups > flash actionscript > january 2005 >

flash actionscript : can some one tell me how this code works


chinese_democracy
1/22/2005 7:35:07 PM
i asked the other day for a code to fire shots in an asteroids style game, and
Nsurveyor gave me this code

MovieClip.prototype.fireShot = function(idName, tDist, jumpSize) {
if(this.maxShots==undefined)this.maxShots = 10;
if(this.cShots==undefined)this.cShots = 0;
if (this.fnum == undefined)this.fnum = -1;
if (this.cShots>=this.maxShots)return;
this.cShots++;
this.fnum++;
var cf = this._parent.attachMovie(idName, this._name+'_f'+this.fnum,
this._parent.getNextHighestDepth());
cf._x = cf.i_x = this._x;
cf._y = cf.i_y = this._y;
cf.piece = this;
var moveIt = function (mc, dir, step, dist) {
degs = dir;
rads = degs/180*Math.PI;
sin = Math.sin(rads);
cos = Math.cos(rads);
stepY = sin*step;
stepX = cos*step;
mc._x += stepX;
mc._y += stepY;
updateAfterEvent();
distH = mc._x-mc.i_x;
distV = mc._y-mc.i_y;
distT = Math.sqrt(Math.pow(distH, 2)+Math.pow(distV, 2));
if (distT>=dist) {
clearInterval(mc.intID);
mc.piece.cShots--;
mc.unloadMovie();
}
};
trace(this)
cf.intID = setInterval(moveIt, 1, cf, this._rotation, jumpSize, tDist);
};

//To use:

someMC.maxShots = 3;
someMC.fireShot('fire', 600, 5);

how ever i dont' know how it works can someone help.
NSurveyor
1/23/2005 2:45:49 AM
Here is the fireShot function explained, line by line:


--------------------------------------------------------------------------------
------------

[quoted text, click to view]

This makes a method for the movieclip, so that you can specify any movieclip
and it will fire another movieclip.

[quoted text, click to view]

This sets up the default settings for the movieclip.

[quoted text, click to view]

Stops the fireball from being shot if there already are the max amount of
fireballs

[quoted text, click to view]

-cShots represents the number of shots that have been fired, and that are
still on the screen. (later in the code, cShots is decreased when the fire is
gone).
-fnum is a number that is used to label each fireball so that it is unique.
-Then a movieclip is created, which is a movieclip located in the libarary,
and is given a unique name.

[quoted text, click to view]

This moves the fireball to the position of the movieclip that is firing it.
And it also sets an i_x and i_y variable which contains the starting point of
where the movieclip was fired from.

[quoted text, click to view]

I made a variable for the fireball so that it can easily refernce to the piece
it fired from.

[quoted text, click to view]

Creates a function to move a movieclip specified by mc, towards the direction
(degress) specified by dir, by the amount of pixels specified by step. Dist
represents how far the movieclip should travel before it dissappears.

[quoted text, click to view]

Uses trigonometry to move the movieclip in the specified direction by a
certain amount of pixels.

[quoted text, click to view]

Uses Pythagorean Theorem to calculate the distance the movieclip has traveled.

[quoted text, click to view]

Checks to see if the distance traveled is farther than (or equal to) the
distance that it should disappear. If it is true, it stops the moveIt function,
decreases cShots because the fireball is finished, and finally removes the
fireball.

[quoted text, click to view]

Ends the moveIt function.

[quoted text, click to view]

-The trace is unnecessary (I forgot to remove it ;)).
-The moveIt function is called in a setInterval with the fireball set as the
current fireball, the direction set as the movieclip's rotation, the size of
the jump to the one specified when calling the fireShot function, and the
distance to travel also specified when calling the fireShot function.

[quoted text, click to view]

Ends the fireShot function

[quoted text, click to view]

Sets the max number of shots allowed to be on the screen fired by that
movieclip.

[quoted text, click to view]

Fires a movieclip (with linkage id: 'fire') from someMC, and travels 600
pixels, jumping 5 pixels every millisecond.


--------------------------------------------------------------------------------
------------

Hope that helps you understand my code better!
chinese_democracy
1/23/2005 12:44:15 PM
NSurveyor
1/23/2005 7:10:54 PM
chinese_democracy
1/24/2005 4:51:58 PM
NSurveyor
1/30/2005 2:02:42 PM
Place the fireShot method onto frame 1 of a new layer. Then, create a movieclip
to be the actual 'fireball'. Then, in the library, right click on that
movieclip. Select linkage. Check off, "Export for ActionScript". Type in fire1
as the instance name. Then, let's say you have a movieclip, _root.myCharacter.
And you want to make him fire on key press, space bar. You could put this code
on the frame that holds myCharacter:

_root.myCharacter.maxShots = 20;
myListener = new Object();
myListener.onKeyDown = function(){
if(Key.isDown(Key.SPACE)){
_root.myCharacter.fireShot("fire1",500,5);
}
}
Key.addListener(myListener);

20 fireballs can be shot. Once there is less than 20 on the screen, then
another shot can be fired. The fireballs will disappear after they travel 500
pixels.
chinese_democracy
1/30/2005 4:43:39 PM
i have done exactly what you said to to do and when i test the movie and pess
space bar the panel where errors are reported pops up and each time i press
space bar it adds _level0.car to the box until it has this many on.

_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
_level0.car
NSurveyor
1/30/2005 7:40:32 PM
chinese_democracy
1/30/2005 8:08:35 PM
if by did the code work you mean firing the shots no it didn't and when i
removed the trace nothing happened when i hit spacebar could you send me the
test file you made using the code so i can see what i have done wrong. thanks
NSurveyor
1/30/2005 8:21:12 PM
chinese_democracy
1/30/2005 8:22:19 PM
NSurveyor
1/30/2005 8:28:04 PM
Let me put the file together....


BTW, made a mistake on my instructions, should be:

NSurveyor
1/30/2005 9:44:19 PM
kglad
1/30/2005 10:38:37 PM
NSurveyor
1/30/2005 11:07:44 PM
kglad
1/31/2005 12:33:41 AM
LOL. grass and trees??! that's great and really makes me feel pretty darn
good about myself: i thought i was totally devoid of artistic ability, but it
appears i'm not the only artistically-challenged person to use flash.
Rothrock
1/31/2005 12:39:29 AM
Actually I totally got NSurveyor's representation of flora. (Okay not totally,
I kind thought it was to be those camo-nets that you see in the action movies.)
So I was reading it as a representation of a representation of flora. And due
to the fact that most of us can continue to use ActionScript despite the
disappearance of Normal Mode, it is hardly surprising to learn that most of us
are probably artistically challenged. :)
NSurveyor
1/31/2005 1:05:19 AM
chinese_democracy
1/31/2005 4:48:51 PM
AddThis Social Bookmark Button