| Code: The code for buttons is "on(condition){code!}" The condition is what you want to press or happen for the button to be activated. If you have code hints enabled then it'll list all the conditions you can use. The ones you're interested in are press and release. Press makes the button work as soon as it is clicked. Release makes the button work only after it has been clicked and the button has been released. Generally you should use release, as it's more user friendly, and widely accepted. If you want to activate it on a button press, you'll have to go through the list of codes to have it activate on buttonpress.
Note that you should never use buttons to code games. Seriously, don't even think of making games until you have a much more complete knowledge of how actionscript works.
As for the code you put inside the button (these work on the main timeline too). "play();" This makes the movie play. By default it plays. "stop();" This makes the movie stop. Note that if you use movieclips that they will keep playing independent of the main timeline.
To skip around the movie you use, "gotoAndPlay(13);" This code skips to a frame and then plays. I used frame 13 for demonstration purposes, you can change 13 to whatever frame you want. If you want to skip to a different scene, you'll have to name the frame in the scene you want to skip to (you don't have to use this code to go to the next scene, flash does that on its own). To give a frame a name you have to make a keyframe, select it, open the property bar, and it has a little textbox by the left side where you can give it a name (movieclips work the same way, except with them it's called an instance name). Once you've named a frame and you want to skip to it, you enter in the name of the frame exactly as it appears in the little text box (all of actionscript is case sensitive, you MUST CAPITALIZE EVERYTHING EXACTLY AS IT IS WRITTEN) inside quotations "". For example, if you named the frame BuDDy, you must enter "BuDDy" inside the parenthesis.
"gotoAndStop();" Does the same exact thing as gotoAndPlay(); except it stops the movie at the frame you tell it to. One thing you have to remember with both gotoAndPlay along with gotoAndStop is that the frame that they are placed on does not play. The viewer will not see the frame that has the goto script. It will be skipped over completely by flash. This is because when flash renders a frame it first does whatever the code says, then once it's done that it does the graphics, and then it shows the result to the viewer. If you tell it to goto another frame during the actionscript part, it'll skip the last two steps and go to the frame you told it to. I left out the 13 in this example to show what the code looks like by default. It still works exactly the same way as gotoAndPlay();
"_root._quality = "HIGH";" This sets the quality of the movie. You can use HIGH, MEDIUM, and LOW. These must be placed inside quotes, and I remind you once again that actionscript is case sensitive. The little _root. part of it makes sure that it targets the main timeline, meaning that if you placed it in a nested timeline (like inside a movieclip), it'll still work. In truth _quality is a universal property, you don't need the _root part to target the main timeline, just it's good form to get in the habit of targeting the main timeline.
"this.play();" This code can be used to target the movieclip itself. You probably won't ever have a use for this unless you go into games or fancy menus. The this. universally targets whatever movieclip is running the code. this and _root are both special codes that target certain things.
The way flash works is that there are various levels. _root is the top level containing all the others. It is the main timeline of your movie, and anything affecting _root will affect the top level of your movie. this targets whatever the hell is running the code. Every object onstage in flash carries an instance name.
These are automatically assigned by flash unless you tell flash what the instance name is yourself. Remember that two copies (called, instances) of the same symbol have different instance names. To target something with an instance name, you type that name after the _root. DO NOT USE SPACES IN YOUR INSTANCE NAMES, it screws everything up. Also try not to name them the same thing as a code in the library, this causes errors (note that flash is still case sensitive, if you name a movieclip Play, it will not generate an error due to being the same as the play(); code, if you name it play, then it will cause an error).
To target a movieclip named "bob" on the main timeline you would type, _root.bob The _root means that you are going directly to the main timeline of the movie, the very top level. Once you're there you type . to tell it that there's something you want to do with _root and then bob to tell it to target bob. Lets say you put a movieclip inside bob named "steve". To target that, you'd type _root.bob.steve Now, let's say you want steve to start playing. To do this you type, _root.bob.steve.play();
In case you haven't noticed, this gets complicated fast, because you have to go all the way back to _root to do anything. Lets say you're inside a movieclip, and there's another movieclip sitting inside it, right there on the movieclip's timeline. To target a movieclip right on the timeline, you can just type that movieclip's name, no name for _root. For example, lets say you have three movieclips, the first named A, the second named B, and the third named C. B is inside of A, and C is inside B. You are currently inside A and you are writing code to make C start playing. Now, you could say, _root.A.B.C.play(); or, because you're inside A already, you can just say B.C.Play();, skipping the need to go back to _root. One thing you'll notice is that moving up and down levels (moving up a level from where you are is slightly more complicated than moving down a level, and I'm not going to cover it here) is that it gets a bit strange because you are working entirely based upon where one clip is in the levels relative to the one you're coding from. _root is an absolute reference point, if you use _root, you will be at the very top of the stack, not x levels up/down from where you currently are. _root is also called level0, and typing _level0 does the same thing as _root.
This last bit talks about the sort of stuff used in interactive things, like games, preloaders, and others. Variables are things that store information for you. Variables are objects similar to movieclips, and you access them the same way as movieclips. All the same code you use on movieclips can be used on variables too (except for the timeline functions because variables do not have timelines). To create a variable all you have to do is type a name and choose a value for it, like "myVariable = 10;" Later on you can change the value by doing the same thing but choosing a different value like, "myVariable = 23;" This example, while it works, is considered bad programming practice. This is because flash doesn't know what type of variable it is. The variable was never clearly defined as being a number, so flash can't tell whether it's a number, or a string (a string is a sequence of characters like "j234j23sfg42jo" or "Hello, my name is Jim"). If you don't clarify which type it is then you end up adding myVariable (equal to 10) and hisVariable (equal to 50) and getting "1050". On the frame when you make the variable you should define the initial value (what the variable starts out as) and the type of variable it is (there are a lot of different types including string and number, which are probably the only ones you have to know). When naming variables, generally follow the same rules as naming movieclips, don't use function names, and don't use spaces.
The correct way to make a variable is "var myVariable:Number = 10;" This clearly tells flash that you want to make a variable (var) named myVariable, which is a Number (:Number), and is equal to 10. Variables that are numbers can be added together, subtracted from each other, multiplied, and divided by each other. In addition you can use math functions on them, such as Math.random(), generates a random number from 1 to 0. This is a decimal number with infinite digits by the way, you multiply it by the range of numbers you want to get a random number from 0 to whatever number (Math.random() * 50 returns a random number between 0 and 50). When you want to change a variable later, just do the same thing we did in the first example, "myVariable = 23;". With Numbers there are several ways of changing their value. You can say "myNumber = myNumber + 10;" to add 10 to whatever myNumber currently is, you can do the same thing by typing "myNumber += 10;" and subtract with "myNumber -= 10;", you can add or subtract 1 to or from a variable with "myNumber ++;" and "myNumber --;". It's worth noting that you really shouldn't use += or -= unless your variable are all positive, because it causes a lot of problems.
A String is a sequence of characters, that can be used to convey messages. Strings are assigned like "myString = "Jesus christ, you sprite editors suck.";" When assigning a string it needs to be contained in quotations. Adding strings works the same way as numbers except you can't use -=, +=, ++, or --. adding "myOtherString = "You really should learn how to make real sprites";" to myString works like this, "myNewString = myString + myNewString;", and it returns "Jesus christ, you sprite editors suck.You really should learn how to make real sprites" Notice how there's no space between the two string's sentences. The space is a character, and you need to add it in, because flash doesn't do this for you. The correct code here would be "myNewString = myString + " " + myOtherString;", manually adding in a space. You can add numbers to strings as well (but you can't add together numbers inside a string, it'll treat it like it's another string, not like a number). For example, let's say we have a variable that has the frame number inside it, and you want to tell the viewer what frame the movie is currently at. To do this you'd make a string like this, "yourFrame = "You are currently on frame " + currentFrame;"
The last type of variables you'll use regularly is called a Boolean. A Boolean just says true or false. "areYouStupid = true;" "areYouAwesome = false;" You can't add these, just set them to true or false.
To address variables you do it the same way as movieclips. Variables made on the main timeline can be accessed like "_root.myVariable" I'm sure you can figure out the rest yourself. Movieclips also have variables in them called properties. Among these are the transparency(_alpha), the position(_x and _y), the instance name (_name), the height(_height) and width(_width) (these are in pixels), and the scale for the height (_yscale) and width(_xscale) (these are a perentage out of 100). Out of the properties I listed (there are more), all of them are numbers except instance name, which is a string. Beware of _name, as changing the instance name means you have to use the new instance name for the rest of your movie. Using these you can change the properties of any variable with an instance name. Remember that stuff I mentioned about changing the quality earlier? _quality is one of the properties of the movie itself. It happens to be a string, which is why you need the quotes around HIGH, MEDIUM, and LOW. _quality is one of many universal properties, including the dimensions of the movie, and other essential things. Universal properties are special properties that do not need to address a specific level to work, and affect the entire movie. _root itself has properties too. You can use actionscript to adjust the location of the camera on _root by changing its _x and _y properties, and you can distort the movie (or zoom in by keeping the two proportional to the original value) by changing the _xscale, _yscale, _height, and _width.
FINALLY there are conditions. Basically these check if something is true, and then do something if it is. The basic if condition works like this "if(suchAndSuch == true){code!}" Notice the ==. You have to use ==, not = when checking if something is true. To check for something not being true, you use !=. With numbers you can use >, <, >=, and <= to check whether the variable is greater than, less than, greater than or equal to, and less than or equal to, respectively. There are two other commonly used types, these being while, and for. While will perform the same action continuously until the condition stops being true. Remember what I said about gotoAndPlay();? The code is rendered before the frame is shown. If you have a while loop where the condition never becomes false, the frame will never show, the movie will lag tremendously, and flash will probably crash (you might get lucky and it'll bail you out by telling you that the movie has a script causing it to run slowly and ask if you want to disable it). The for loop works the same thing as the while loop except it requires you to set a variable, perform a function upon it, and create a condition where the function cancels. The for loop is good for doing the same thing several times (you can also reference the variable you use for the for loop so that it will do the same thing to several different objects, without having to copy and paste the code and make it reference each object). A for loop works like, "for(i = 0,i ++,i<10){code!}" In this case we make a variable named "i", which is equal to 0 (i = 0), and with each loop add one to it (i ++), and tell it to stop looping when it's no longer lower than 10 (i < 10). This means that it will perform the coded action 10 times before showing the viewer the frame. If you code this incorrectly, it can cause the same problems as the while loop, crashing flash, and making lag. You can use the variable i in the code within the for loop so that instead of doing the same thing to the same object 10 times, it'll do the same thing to 10 different objects. You can reference an object's name by adding together variables like you would with strings by putting the name in brackets like this, "_root["myObject" + i]._x = _root.["myObject" + i]._x + 10;" Note that you do not put a period between _root and the brackets. You can use this same method to address variables too. In the case of, "for(i = 0,i ++,i==10){_root["myObject" + i]._x = _root.["myObject" + i]._x + 10;}", it would add 10 to the x position of the movieclips on the main timeline called, myObject1, myObject2, myObject3 . . . myObject10.
And to top it off, here's a bit of terminology. A movieclip placed on the timeline is called an "instance", because it's just an instance of something from the library. A code like play(); stop(); or gotoAndPlay(); is called a "function". The objects inside a function's parenthesis telling it what to do are its "parameters". _root, this, and _level0 reference a movie's "levels". on(){} and onClipEvent(){} are called "handlers" (worth noting is that handlers never end with a semicolon ;). if(){} is a condition, while(){} and for(){} are loops. | |