Archive for July, 2009
A more heavy-duty tool is the debugger. This is a Flash window that shows you all sorts of data about your Flash movie and ActionScript code as your movie plays.
To use the Debugger window, choose Control, Debug Movie rather than Control, Test Movie. Your movie will compile and launch, but this time with the Debugger window shown.
One way to use the Debugger window is to set breakpoints. A breakpoint is an indicator placed next to a line of code in your scripts. When you test the movie in debug mode, the movie automatically stops at a breakpoint and allows you to examine the current state of the movie, including variable values. You can then proceed to step, line-by-line, through the code.
Because you are just getting started with ActionScript, the Debugger window is a needlessly complex tool. However, as you advanced from novice to expert, you will find yourself needing it more.
Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours
No Comments »
A simple but effective tool in debugging is the Output window. You can place trace commands in your program that send information to the Output window. This information can tell you what your code is doing.
For instance, suppose that you have a simple loop that doesn’t seem to work right. By adding a few trace commands, you can get a list of actions in the Output window that should point you in the right direction.
for(var i=0;i<9;i++) {
trace(”starting loop”);
trace(i);
a += i;
trace(a);
trace(”ending loop”);
}
In this example, you will get a series of “starting loop” and “ending loop” messages, with two numbers representing the variables i and a between them. You can then watch the progression as these two variables change.
Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours
No Comments »
No matter how careful you are writing your program, every programmer has to eventually deal with bugs. To stamp out these pesky things, you should be as skilled in debugging techniques as you are in programming. Three methods are useful for debugging: logical deduction, sending messages to the Output window, and the ActionScript debugger.
Logical Deduction
Most bugs that occur are simple and easy to track down and fix. You won’t need any special tools. When the bug occurs, you will have a pretty good idea of what it is and where it is.
Even if you don’t know where the problem is at first, if you act like a detective and look through your code, you can usually find it. Think of all the information that you have as clues: When did the problem happen? What happened? What should have happened instead? What else unusual went on that might be another symptom of the bug?
No one knows your program better than you do. If you use logical deduction, you should be able to find your bug and fix it. However, some bugs are a little trickier, so you will need to use some helpful tools.
Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours
No Comments »
Some of the most important lines of code in your programs don’t do anything at all. They are comments, which Flash just ignores.
Comments, however, help you organize your code and remind you what a particular piece of code does. Here is a simple example:
// add 2 to the number
num += 2;
The previous example shows a line that is completely a comment. It begins with two forward slashes. The rest of the line is whatever you want it to be.
You can also place a comment at the end of a line. Here is an example:
num += 2; // add 2 to the number
Whenever you use the double slashes, the rest of the line is ignored by Flash.
If you have ever studied computer programming in college, you probably already know how important comments are. Most teachers will severely penalize a programming assignment if it is turned it with inadequate comments.
There are many different ways to use comments in your code. Programmers usually place one or more lines of comments before each function that explain what the function does. You can also place comments before individual lines if you feel they need more explanation.
Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours
No Comments »
We’ll look at all the different movie clip properties as we need to use them in future hours, particularly in Hour 7, “Moving and Changing Movie Clips.”
You can also use dot syntax to reference global variables inside other movie clips. So if you have code in a movie clip and are using a global variable inside that movie clip, you can access it from the root level like this:
var a = myClip.myVariable
Don’t worry if you aren’t sure about objects, movie clip properties, or movie clip levels. We’ll examine each of these more throughout the book as we use them, particularly in Hour 6, “Controlling Movie Clips,” and Hour 7. Right now, it is just important that you understand what dot syntax looks like.
Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours
No Comments »
Something else that you will be seeing a lot of as you learn ActionScript is dot syntax. Dot syntax is a way of grouping objects and functions that is used in many object-oriented programming languages.
Here is an example of dot syntax. Suppose that you want to take the square root of a number. Flash has a built-in square root function. It is a part of a group of math functions called the math object. To use the square root function, you first have to use the name of the math object, which is simply Math. The name of the function is sqrt. So this is how you would use the math object’s square root function:
var a = Math.sqrt(4);
We’ll look at the math object and its functions, as well as other objects like it, throughout the book.
Another common way to use dot syntax is to address a property of a movie clip. Suppose that you have a move clip named myClip and you wanted to determine its horizontal position on the screen. That would be the _x property of the movie clip. So the code would look like this:
var a = myClip._x;
Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours
No Comments »
When the function starts, it creates a local variable called num and places 7 inside it. It then runs the code inside, which ends with the return command sending the value 10 back to the thing that originally called the function. In this case, a gets set to 10.
A great thing about functions is that you can reuse them. Here are three lines of code that reuse the function to produce three different results:
trace(myFunction(7));
trace(myFunction(13));
trace(myFunction(2));
When you run this code, along with the function included before it, you will get the results 10, 16, and 5. Another advantage to using functions is that you can make one change in the function, and it will affect all the commands that use that function. For instance, if you change the + 3 in the function to + 4, the results of the preceding three lines become 11, 17, and 6.
Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours
No Comments »
You can have one, many, or no parameters. Either way, you close off the parameters section with a right parenthesis and then use an open bracket to start the function.
All the lines between the open and close brackets are the instructions inside the function. In this case, a new local variable is created, called newNum. The value of newNum is set to whatever num is, plus 3. So if you pass a 7 in to the function as num, newNum is now 10.
The return command is a special command used only inside functions. It completes the function and sets a value as the result of the function. In this case, newNum is the result of the function.
To use this function, call it like it was a standard ActionScript function or command, such as trace. Here is an example:
var a = myFunction(7);
This line of code creates a new local variable called a. It places in it the results of myFunction(7). To determine this value, myFunction is called with the number 7 as its only parameter.
Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours
No Comments »
Until now, we’ve placed all our code in short scripts attached to the first frame of the movie. This works well for short, simple programs, but these scripts can get very long if you try to make the program complex.
Functions allow you to organize and reuse your code. You place functions in the timeline just as we have been doing. Here is a simple function:
function myFunction(num) {
var newNum = num + 3;
return newNum;
}
A function starts with the keyword function followed by the function name. Function names can be anything you want, just like variable names. But they should usually be something that relates to what the function does.
After the function name comes a left parenthesis. Then follows a list of parameters. A parameter is a variable that is defined when the function is called. Think of it as the input to a function. In this case, you are going to give the function a number to do something with.
Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours
No Comments »
All three kinds of loops can use two optional commands to change the flow of the loop. The first command, break, stops the loop and jumps right to the instruction following the loop.
The other command, continue, terminates the current pass through the loop but starts the next pass through the loop right away.
For instance, if instructions A, B, and C are inside the loop, and instruction B performs a continue command if a certain condition is met, instruction C will be skipped, and the loop will start again at A. If it was a break command instead, C would be skipped and the loop would end.
It is difficult to show valid examples of break and continue without a complex example, so we will save that for later in the book when we need it.
Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours
No Comments »
|
Partner links