this is just a simple guide that i hope will help you understand how to solve your own scripting problems.
i've also found that solving problems using the language is the best way to learn the language.
i was explaining this to somebody else, and i thought i'd share it with an example:
japser;;;;;;;,,,,,,,,,,,, says (9:14 PM):
*scripting is like maths
*you're given a problem
*then you need to figure out what you need to do to solve it
*what method will give you the right answer?
*what methods will not work?
japser;;;;;;;,,,,,,,,,,,, says (9:15 PM):
*and then
*once you know what you need to do
*you can start solving the problem
now recently, i've been helping people out with making games, most particularly a few people have been asking for help with sniper games (the person i was talking to in that chat log right there included).
one of the guys wanted to know how to add a cooldown time between shots in his sniper game. the targets were simple buttons.
so here's an example of what i mean.
1. What is the problem?
To add a cooldown time that makes shooting a target impossible between shots.
2. What do I need to do to solve the problem?
I need to disable all of the buttons that are being used as targets for a certain amount of time after shooting. Then, once that time is up, i need to enable the buttons again. There are 8 buttons that I need to disable and re-enable, so I might need to use a more inventive function to enable/disable them all at once without using 8 lines of code.
3. What method will give me a working solution?
A countdown can be created using a combination of the onEnterFrame repeating function and a numerical variable that goes down by one each time it repeats. The countdown variable can increase when the mouse is clicked. Then I can use an "if" function to check if the countdown is at zero, and if it isn't, shooting should be disabled.
4. What methods will NOT work? (there are a lot more than what i've got here but this is just an example)
I originally thought to use a button that covers the whole screen to check when the mouse is clicked... but you can only click one button at a time. Covering a button with another button means you can only click the button on top. If I were to do that, you wouldn't be able to click the targets. Instead, I'll use an event listener to check when the mouse is pressed down.
The solution:
On the frame there is a movie clip called "asdf" that contains motion tweens of 8 movie clip guys running across the screen. Each one of those guys has the instance name named "runner1", "runner2", "runner3" etc, but they are all instances of the same movie clip. That movie clip contains the body of the runner, plus a head which is a button*. That button has the instance name "headtarget", and when clicked, makes the guy play an animation of being shot in the head.
So this is the script I came up with:
stop();
cooldown = 0;
//set the variable for the cooldown period to zero
cooldowntime = 10;
//set the variable for how long cooldown will take to [cooldowntime]/[framerate] seconds
//ie, 10/20 = 0.5 seconds
numberofbuttons = 8;
this.onEnterFrame = function() {
//as long as the playhead is on this frame, repeat the following:
if (_root.cooldown > 0) {
//if the cooldown period is greater than zero,
_root.cooldown--;
//reduce the remaining cooldown by one
for (i=1; i<=numberofbuttons; i++) {
//as long as variable "i" is less than or equal to the "numberofbuttons",
//do the following, and then increase "i" by one and repeat until "i" is
//equal to "numberofbuttons"
asdf["runner"+i].headtarget.enabled = false;
//disable the button asdf.runner[ALL NUMBERS FROM 1-numberofbuttons].headtarget
//ie, disable all the target buttons
}
} else if (_root.cooldown == 0) {
//otherwise, if the cooldown left is 0,
for (j=1; j<=numberofbuttons; j++) {
//as long as variable "j" is less than or equal to the "numberofbuttons",
//do the following, and then increase "j" by one and repeat until "j" is
//equal to "numberofbuttons"
asdf["runner"+j].headtarget.enabled = true;
//enable the button asdf.runner[ALL NUMBERS FROM 1-numberofbuttons].headtarget
//ie, enable all the target buttons
}
//reenable the buttons (aka the runners' heads)
}
};
var clickListener:Object = new Object();
clickListener.onMouseDown = function() {
//create a listener object that will check when the mouse is clicked and do the following:
if (_root.cooldown == 0) {
//and if the cooldown counter is currently at zero,
_root.sight.play();
//play "sight"
_root.cooldown = _root.cooldowntime;
//set the remaining cooldown to be currently equal to the total cooldown time
}
};
Mouse.addListener(clickListener);
//add the listener object to the mouse
Of course, this is much easier to do if you already know all of the functions and such that you have available to us, which is the advantage of learning the language, but, if you don't know the language, you should still be able to get to step 2 with no problem.
The rest is just searching. Google what you need to learn. For example:
"actionscript 2 disable buttons"
"actionscript 2 create a timer"
"actionscript 2 run a function if a variable is equal to 0"
and so on and so forth. once you know what you need to find, finding it isn't difficult.
that concludes this lecture
good day
* by the way, i am entirely against using buttons in games; i find it much more prudent to use a movie clip as a button, because you can do so much more with a movie clip if you have to. this is just the way the flash file was already laid out when i was given it to make adjustments to, and i didn't want to go changing the whole thing.