Animation and Effects with Macromedia Flash MX 1002004 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Animation and Effects with Macromedia Flash MX 1002004 [Electronic resources] - نسخه متنی

Jen deHaan

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید




Coding Concepts


There are many different parts of the ActionScript language, some of which you have explored and even written so far. There are particularly important parts of the language to learn the fundamentals of, regardless of whether you continue on to create business applications for the boardroom or fantastic graphic effects for a bleeding edge website. However, there are particular parts of the language that are well-suited to animation. You cover the basics of ActionScript syntax here, and in the following chapters you'll continue to piece together more pieces of the puzzle. Syntax forms the rules and guidelines for a particular way of writing your code.

Pay special attention to the terminology used in these sections. You encounter them a lot in this and following chapters, so it'll make your life much easier to note down or highlight some of the names and terms used in this chapter.

Using data and declaring variables


Variables are also useful when you have long and complex names or paths in your ActionScript. Perhaps you have a long path to a movie clip you need to target. You can save that target as a variable, so you only have to type that instead.

A data type is a type of information, such as a Number (such as 601), a String (such as "pelican"), or a Boolean (either true or false). A variable is a container that holds a particular value of any data type. You create variables in Flash, which is called declaring a variable. When you declare a variable, you give it a name (for example, myPelican or redCar). When you need to reuse a particular value in your code, such as the score of the Pong game you're creating, variables can be useful. You assign a value to the variable, so you can use it over and over, even if it changes. For example, your Pong score will constantly change when users score points, so the variable for that score can update (change) with it.


Naming Variables


There are several considerations to make when you name a variable. Variables can contain only letters, numbers and underscores. The name can be anything you want, except that it cannot share a name with another variable. So, you cannot have two variables called myVar. Variables are case sensitive, so you could use myvar and myVar as two variable names, except that this is confusing and should be avoided. You should also try to avoid special characters.

Variables cannot be a keyword; or share the name of an object, method, or property in ActionScript. You want to avoid any part of the ActionScript language, in fact. For example, if you use a keyword as a variable, the variable is treated like the keyword instead, which then breaks your code. For a list of words to avoid, see "Avoiding reserved words" in the Using ActionScript book of Flash documentation (Help > Help). Search "reserved words" in the Help panel to find this section.When you declare a new variable, you use the var keyword. You use the var keyword to declare (create) the variable.

Keywords have only a single purpose in a Flash file. Keywords exist in Flash to behave a particular way or to do certain things. For example, you saw that the var keyword was used to declare a variable.

You follow the keyword with the name of the variable (what you use to identify it), which can almost be anything (see the sidebar titled "Naming Variables"). You then use the assignment operator (=) to assign a value to the variable and follow this with the value itself.

A Boolean is a TRue or false value. You might use a Boolean to turn something on or off in a SWF filesuch as visibility or whether the user can click a button. String values are denoted by quotation marks around the value. Number and Boolean values do not have quotation marks around the value to differentiate them, as seen in the previous code.

Therefore:


var myLuck = false;

The variable myLuck contains the Boolean value of false.

Later on in the application, the value can be set to TRue if luck changes. Or, if you use the variable myLuck several times in your code, and later on you want all of those values to be true, all you have to do is change the line above and all the values are updated everywhere you might use it.

For example, perhaps you want to have a series of movie clips that are quite transparent. You might set the transparency in your code, which is set by using a Number value:


var myAlpha = 50;

Understanding Objects" (later in this chapter).

You have set 30 movie clips to myAlpha. Later on, you might change the design of your site and decide that much transparency detracts from the design. All you need to do is change the single myAlpha value from 50 (its initial value) to 20, and every single clip updates. Imagine if you set each clip to the number 50 instead of myAlpha. You would have a lot of updating to do in your code.

Using dot notation


ActionScript uses dot notation, sometimes called dot syntax, to address (or target) objects in your SWF files. For example, you need to target a movie clip instance before you can apply an action to it. Dot notation helps you create a path to the instance you need to target.

The path to the instance that you target is sometimes called the target path.

If you want something to work in your SWF file, you need to target that instance and then tell it to do something, such as by giving it an action or changing its properties. You usually need to define where that instance is in the SWF file (for example, what Timeline it's on or what instance it is nested within) by creating the target path.

Remember that you have given many of your instances an instance name, and then you added code to the FLA file using that instance name. By doing this, you target that particular instance and then tell it to do something (such as move the playhead or open a web page.) The instance name is the name that you type into the Property inspector after you select the instance on the Stage.

You sometimes assign an instance name using code, such as when you use ActionScript only to create a new movie clip.

An FLA document has a particular hierarchy. You might have instances on the main Stage, or you can have instances that nest inside a movie clip. Or, you might even have instances that nest within a couple of movie clips. If each of these movie clips has an instance name, you can code a target path to one of the nested clips. For example, you might have a target path that's separated by a series of dots, as follows:


myClip_mc.myClip2_mc.myClip3_mc.gotoAndPlay(5);

How ActionScript manipulates instances" later in this chapter.

This code targets the myClip_mc instance, which sits on the main Stage. Then the ActionScript targets the myClip2_mc instance, which is inside the myClip_mc instance. Finally, you add the action that you want to occur: The gotoAndPlay() method plays the myClip3_mc instance, which sits inside of myClip2_mc.

Remember that each movie clip instance has its own Timeline, which you can not only target with code, but also place graphics, code, or other instances on. So you would use this code if you want to target myClip3_mc and have it go to frame 5 in the movie clip and play that frame onward. Notice that the clip that's manipulated comes right before the action. You'll notice this trend in upcoming sections.

It might seem pretty confusing, and it certainly can be. You should try to keep your FLA documents as simple as you can, but there will be times that you encounter complex nested instances that you need to target by using code similar to the snippet just outlined.

You might think that because the myClip3_mc has a Timeline, why not just place the code on that Timeline instead of typing out this long target? You can! However, you might remember that it is a lot easier to keep all your code in one place. Therefore, you can put all your code on the main Timeline together, which is much easier to edit than trying to open up a few clips and dig around for ActionScript.


Adding Scripts Using the Add Button and Toolbox


You can use the Actions toolbox to help you add code to your FLA file if you're new to writing ActionScript or are unsure of ActionScript syntax. The Actions toolbox is located to the left of the Actions panel. The Actions toolbox contains all the actions you can use that are built into Flash. The toolbox contains many "books" that you can click and move throughout to find actions, methods, and properties. When you double-click a method in the Actions toolbox, for example, it adds to the Script pane wherever the cursor is located. You can also use the Add (+) button to add new scripts to your code (Figure 6.7). This useful feature lets you add pieces of code to the Script pane when you select an action from the menu. In some cases, after you add the action, the cursor in the Actions panel is specifically placed where you need to type, and a tooltip appears. The tooltip provides a guide on how you should go about finishing off the script.

Figure 6.7. Use the Add button in the Actions panel to help you add code to the FLA document.

[View full size image]


Commenting Code


When you add code to your Flash documents, the most important thing to remember is to add enough comments to your code. Comments can be any text that you want to write, such as what the code is doing, or any helpful hints you want to add. Comments help you remember what exactly the code is doing if you have to modify the code in the future. Comments also make it easier for other people to understand the code when they modify your FLA files.

Place // in front of a line of code to add a comment, and then type anything you want (the comments) onto that line. Or, if you want to add a large comment, you can type /* at the beginning of the first comment line, and then add another */ at the end of the comment. The comment will be a different color than the rest of your code in the Actions panel (comments are gray by default).

Also try to give your variables descriptive names, which can help with the readability of your ActionScript. If your variables describe what they contain, then other people can better understand what they're used for. Don't make your variable names too long (this helps your performance), but make sure they still describe that they're used for in the SWF file.

/ 123