The Action Scripter
12-07-2002, 11:28 PM
Ok, people have been mailing me and messaging me about certain action script questions, so I thought I'd answer some of them, plus a little more:
BASICS(1)
December 7th, 2002. 5:03 PM.
Prefixes:
_root. This is used to target a shape or movie clip. This really is locating the shape/MC, wherever it may be. _level0,1,2,3 may be used also, because this determines the level of where the MC/shape is.
Example: _root.mc1.play();
_parent. This is used to target a shape or a movie clip from INSIDE a movie clip.
Example: _parent._root.mc1.play();
_visible = x; Determines if the object is visible or not. "x" equals "True" or "False."
Example: _root.mc1._visible = false; (The object is invisble.)
_level(x) Like _root., but is more confusing...You must determine what "Level" the object is on. "x" should be replaced by the level, with no () around it.
_alpha = x; Used to determine the transparency of objects.
Example: _root.mc1._alpha = 30;. (This will produce a medium transparency.)
_x In terms of the x axis. (Left and right)"+" would be going right, and "-" would be going left.
Example: _root.mc1._x+=5;. (This moves the MC 5 places to the right every second.)
_y See "x".
Example: _root.mc1._y+=5;
_rotation Rotates an object with no frames required.
Example: _root.mc1._rotation+5;. (This will rotate the object to the right quite slowly.
_xscale Shrinks or enlarges a shape or movie clip, to the "X" axis.
Example: _root.mc1._xscale+=5;.
_yscale See "xscale".
Example: _root.mc1._yscale+=5;
Controllers:
Controllers are action script statements that set up variables, that can be reffered to. In English, you can set up a statement saying:
if(key.isDown(key.LEFT))
{//action goes here
}
That says, if the key is down, Left, the action will take place. Without controllers, there is no way to activate an action.
if(condition) {action} Sets up an "if" statement, which tests if something is happening. If it is, the action will take place.
Example:
if(key.isDown(key.LEFT)) {
_root.mc1._x-=5;
}
}Else{ Used to see if an action is NOT happening. Used in an "if" statement.
Example:
if(key.isDown(key.LEFT)) {
_root.mc1.play();
} else {
doNothing();
}
December 7th, 2002. 8:24 PM.
Dynamic Text with Action Script:
Dynamic text's main use is accomplished with action script. Dynamic text is great for setting up switchs, and making counters, health, loading bars, etc.
Making a simple counter: Making a counter is very, very easy, and is good for learning about dynamic text.
How to do it:
Ok, first, make a dynamic text on whatever layer. Go to it's instance name, and call it "counter." Next, create a small block anywhere on the screen. Convert it to a MOVIE CLIP. It must be a movie clip. Now, put this actionscript on the movie clip you just created's main actions:
Code:
onClipEvent(load) {
this._visible=false;
}
onClipEvent(enterFrame) {
_root.counter+=1;
}
That says: On the EnterFrame();, the dynamic text "counter" will be increased by one.
But we are not done yet.
On the first frame in which "counter" appears, put this action script in the main movie's actions:
Code:
_root.counter=0;
That simply says that the dynamic text "Counter" is set up to be a number dynamic text.
Play your movie. The dynamic text will increase by 1 very rapidly. Ta da.
Making the counter interactive: Let's add some interactivity to the counter. Let's make it, so when you press the Spacebar, the text increases by one, and when you press Control, it decreases by 1.
First, do all of the steps above, in "Making a simple Counter." But now your text keeps going and going, until the end of the world.
Next, go to the actions of the block movie clip.
Change the action script to this:
Code:
onClipEvent(load) {
this._visible=false;
}
onClipEvent(enterFrame) {
if(key.isDown(key.SPACE)) {
_root.counter+=1;
}
if(key.isDown(key.CONTROL)) {
_root.counter-=1;
}
}
Now, test your movie. Click on the screen to ensure it has "Focus." Press control and spacebar, and look what happens!
Normal Scripting Level(2)
Key Press:
There is a function in Flash called "KeyPress" with a white box next to it, signifying which letter you want to be the "Key." This is a much easier way, if you want simple key presses, but if you want 2 or more keys combined to perform an action, you need a little bit more advanced scripting.
Making two keys activate a command.:
This script is insanely easy, all you have to do is copy and paste it from here. Making and understanding it brings us to the Normal Level of scripting.
First, make a block similar to the one in "Making a simple Counter," and convert it to a movie clip. Put this AS in the main actions of it:
Code:
onClipEvent(load) {
this._visible = false;
}
onClipEvent(enterFrame) {
if((key.isDown(key.CONTROL)&&(key.isDown(key.SPACE)))) {Action goes here
}
}
And there you have it. Add an action and change the keys to press to your liking.
BASICS(1)
December 7th, 2002. 5:03 PM.
Prefixes:
_root. This is used to target a shape or movie clip. This really is locating the shape/MC, wherever it may be. _level0,1,2,3 may be used also, because this determines the level of where the MC/shape is.
Example: _root.mc1.play();
_parent. This is used to target a shape or a movie clip from INSIDE a movie clip.
Example: _parent._root.mc1.play();
_visible = x; Determines if the object is visible or not. "x" equals "True" or "False."
Example: _root.mc1._visible = false; (The object is invisble.)
_level(x) Like _root., but is more confusing...You must determine what "Level" the object is on. "x" should be replaced by the level, with no () around it.
_alpha = x; Used to determine the transparency of objects.
Example: _root.mc1._alpha = 30;. (This will produce a medium transparency.)
_x In terms of the x axis. (Left and right)"+" would be going right, and "-" would be going left.
Example: _root.mc1._x+=5;. (This moves the MC 5 places to the right every second.)
_y See "x".
Example: _root.mc1._y+=5;
_rotation Rotates an object with no frames required.
Example: _root.mc1._rotation+5;. (This will rotate the object to the right quite slowly.
_xscale Shrinks or enlarges a shape or movie clip, to the "X" axis.
Example: _root.mc1._xscale+=5;.
_yscale See "xscale".
Example: _root.mc1._yscale+=5;
Controllers:
Controllers are action script statements that set up variables, that can be reffered to. In English, you can set up a statement saying:
if(key.isDown(key.LEFT))
{//action goes here
}
That says, if the key is down, Left, the action will take place. Without controllers, there is no way to activate an action.
if(condition) {action} Sets up an "if" statement, which tests if something is happening. If it is, the action will take place.
Example:
if(key.isDown(key.LEFT)) {
_root.mc1._x-=5;
}
}Else{ Used to see if an action is NOT happening. Used in an "if" statement.
Example:
if(key.isDown(key.LEFT)) {
_root.mc1.play();
} else {
doNothing();
}
December 7th, 2002. 8:24 PM.
Dynamic Text with Action Script:
Dynamic text's main use is accomplished with action script. Dynamic text is great for setting up switchs, and making counters, health, loading bars, etc.
Making a simple counter: Making a counter is very, very easy, and is good for learning about dynamic text.
How to do it:
Ok, first, make a dynamic text on whatever layer. Go to it's instance name, and call it "counter." Next, create a small block anywhere on the screen. Convert it to a MOVIE CLIP. It must be a movie clip. Now, put this actionscript on the movie clip you just created's main actions:
Code:
onClipEvent(load) {
this._visible=false;
}
onClipEvent(enterFrame) {
_root.counter+=1;
}
That says: On the EnterFrame();, the dynamic text "counter" will be increased by one.
But we are not done yet.
On the first frame in which "counter" appears, put this action script in the main movie's actions:
Code:
_root.counter=0;
That simply says that the dynamic text "Counter" is set up to be a number dynamic text.
Play your movie. The dynamic text will increase by 1 very rapidly. Ta da.
Making the counter interactive: Let's add some interactivity to the counter. Let's make it, so when you press the Spacebar, the text increases by one, and when you press Control, it decreases by 1.
First, do all of the steps above, in "Making a simple Counter." But now your text keeps going and going, until the end of the world.
Next, go to the actions of the block movie clip.
Change the action script to this:
Code:
onClipEvent(load) {
this._visible=false;
}
onClipEvent(enterFrame) {
if(key.isDown(key.SPACE)) {
_root.counter+=1;
}
if(key.isDown(key.CONTROL)) {
_root.counter-=1;
}
}
Now, test your movie. Click on the screen to ensure it has "Focus." Press control and spacebar, and look what happens!
Normal Scripting Level(2)
Key Press:
There is a function in Flash called "KeyPress" with a white box next to it, signifying which letter you want to be the "Key." This is a much easier way, if you want simple key presses, but if you want 2 or more keys combined to perform an action, you need a little bit more advanced scripting.
Making two keys activate a command.:
This script is insanely easy, all you have to do is copy and paste it from here. Making and understanding it brings us to the Normal Level of scripting.
First, make a block similar to the one in "Making a simple Counter," and convert it to a movie clip. Put this AS in the main actions of it:
Code:
onClipEvent(load) {
this._visible = false;
}
onClipEvent(enterFrame) {
if((key.isDown(key.CONTROL)&&(key.isDown(key.SPACE)))) {Action goes here
}
}
And there you have it. Add an action and change the keys to press to your liking.