Embed
Email

More ActionScript

Document Sample
More ActionScript
Shared by: Doni Ibrahim
Categories
Stats
views:
12
posted:
11/16/2011
language:
pages:
23
More ActionScript





10

ActionScript for Interactive

Content



Making Decisions – if and switch



Repeating Actions - For and while



Functions



Arrays



Reading External Data



Playing Sound via ActionScript



Debugging ActionScript

MORE ACTIONSCRIPT









ACTIONSCRIPT FOR INTERACTIVE CONTENT



As you begin to design interactive content, you’ll need to have a solid knowledge of ActionScript.

There are a number of basic concepts that you’ll need to understand that will make your

development experience enjoyable.



In the process of creating different types of activities in ActionScript, there are some common things

you’ll use o en in your ActionScripts. Some of the common things you’ll need to do include:



● Keeping track of a score using variables



● Make decisions on what to do next based on user input, scores, or other actions



● Loading and saving data



● Using movie clips to place objects on the screen and have them respond to the user and other

objects









188

MORE ACTIONSCRIPT









MAKING DECISIONS  IF AND SWITCH



You will o en need your ActionScript code to make decisions, such as whether to execute a

particular action or group of actions. To make decisions, you’ll need to compare values, which is

done with the if and the switch statements.





If, else and else if

The if statement is used to compare a variable against a value. If this condition is met, the code

inside the { } brackets is executed, otherwise it’s skipped. For example, on an exercise with 5

questions, we’ll display a congratulations message in a text field if the learner ends up with a score of

5 using the following:



if (score == 5) {

txt_Score.text = “Congratulations!”;

}





It’s important to note that in ActionScript, the == symbol is used to compare two values to see if they

are equal.



You can extend the if statement to perform actions when the comparison in the if statement is not

met by using the else statement. Using the else statement and an additional set of curly braces, you

can tell Flash what to do if the original condition is not met:



if (score ==5) {

txt_Score.text = “Congratulations!”;

} else {

txt_Score.text = “Nice try.”;

}









189

MORE ACTIONSCRIPT









You can also have a longer if statement by adding else if statements.



if (score ==5) {

txt_Score.text = “Congratulations!”;

} else if (score > 3) {

txt_Score.text = “Nice try. You did well.”;

} else {

txt_Score.text = “Please try again.”;

}





To test conditions, you can use any of the logical comparison operators such as:



== equals

>= greater than or equal to

greater than







ferry.jpg

A Washington State Ferry headed to downtown Seattle





portofseattle.jpg

The Port of Seattle is one of the nation’s busiest

commercial container ports.









Each item in our slideshow has two a ributes: a picture a ribute that defines the file name of the

picture, and a description a ribute that has a text description for the picture.









201

MORE ACTIONSCRIPT









Creating an XML Object



You must create an XML object before you can do anything with the class. The way to create an object

is simply to call the constructor function in a new statement. The function requires no parameters.

Once you’ve created the object, you’ll want to set the ignoreWhite property of the XML object to

true, this tells Flash to ignore any white space in the XML file.



var my_xml:XML = new XML();

my_xml.ignoreWhite = true;





Loading Data



A er you have created the XML object, you’ll call the load() method from the object, passing it a

parameter specifying the URL where the XML file can be found.



The loading process is asynchronous, so you can define an onLoad() method that’s called when the

data has been loaded (or failed to load).



my_xml.load(“slideshow.xml”);

my_xml.onLoad = function(success) {

//function to parse data goes here

}





Reading Child Nodes



You have seen that there exists a hierarchy in an XML document, in which elements are nested

within other elements. This hierarchy is o en called the document tree. Nested elements are called

the children or child elements of a parent element.



When you are stepping through the hierarchy of your XML object or traversing the document tree,

it is most useful to be able to retrieve the child nodes of a parent element. For instance, in our XML

slideshow.xml file, if the current element you are working with is the item element, you would likely

want to read the names and values of its child nodes. One way to do this is to use the firstChild

or lastChild properties, and then step through all the child nodes by accessing the nextSibling

or previousSibling properties of the child elements. (Sibling elements are simply other elements

within the same hierarchy in the tree and that are nested within the same parent element.)









202

MORE ACTIONSCRIPT









In our example, the firstChild property of the item element is a reference to the picture node.

Likewise, the lastChild property of the item element is a reference to the description node.

Remember, both tags and text elements are nodes in the ActionScript XML parser.



The firstChild property of an XML object that has been parsed from a string or document without

any additional spaces or a Document Type Definition (DTD) is the root node. When there are spaces

in the string or a document before the root node, or if the string or document contains a DTD, the

firstChild of the object might not always be the root node.





A er loading the XML file into the XML object, the following code will display the entire XML file:



trace(my_xml.firstChild);





To access the first node of the XML file (the first item), you would use the childNodes property

while specifying the position like this:



my_xml.firstChild.childNodes[0]





The value of this property is:



ferry.jpgA Washington State Ferry

headed to downtown Seattle.





To access the picture value for the first item, you’d use the following



my_xml.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue





To access the description value for the first item, you’d use the following



my_xml.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue





To get the number of nodes in the slideshow, you would use the length property:



my_xml.firstChild.childNodes.length





To get the number of nodes in an item, you would use the length property:



my_xml.firstChild.childNodes[0].childNodes.length









203

MORE ACTIONSCRIPT









Sharing Data Across Domains



Flash movies have built-in security that restricts access to send or load content from any source that

is on a different domain from where the Flash movie is playing. With Flash Player 7, the domains

must match exactly, including protocol and port. In other words, a movie playing at http://www.

myflashserver.com can load any data from the same domain, but it cannot load data from http://

www.edserver.com, and it cannot even load data from http://data.myflashserver.com. This can

be problematic. You might want an SWF file running on one domain to be able to send and load data

on another domain. You have three options available:



● Create a crossdomain.xml policy file. This requires that you have access to the domain from

which you want to load data or to which you want to send data. If you do have access to that

domain, this is the suggested technique.



● Set up DNS aliasing. This is not a likely candidate for most. It involves a certain level of

expertise and access that many folks do not have. The idea is that on the DNS servers that

your server uses, you can set up an alias to a remote server so that it appears to be in the same

domain.



● Use a proxy script. This is probably the option that will be available to most users. You can

use a proxy script that will reside on the same domain as your Flash movie and simply relay

the data between the Flash movie and the remote domain.



For more information on sharing data across domains, the Flash MX 2004 ActionScript Bible has

sample crossdomain.xml policy files and sample proxy scripts that can be used.









204

MORE ACTIONSCRIPT









PLAYING SOUND VIA ACTIONSCRIPT



When utilizing sound objects via ActionScript, the sound is never dragged from the library onto

stage or placed in a frame. This is true whether you use the attachSound (for embedded sounds) or

the loadSound method (for external sound files). The sound is defined in a frame or movie clip script

and an identifying name is a ached to the sound while it resides in the library (except in the case of

loading an external MP3).



To play a sound with the attachSound function, you first import the sound to the Library. Next,

right-click (Windows) or control-click (Mac) on the sound file within the Library and select

“Linkage...”. Click the “Export for Actionscript” checkbox and then type in an identifier for the

linkage ID, which you’ll use to reference the sound file.









The Identifier, which, in this example, is coindrop, must be unique. It should not be the same as your

sound object instance name or any other identifier in your movie.



To play the sound file, you’ll use first create a new sound object, then use the a achSound method to

specify which sound file is a ached to the sound object.



coinSound=new Sound();

coinSound.attachSound(“coindrop”);





To play the sound, we’ll use the start() method:



coinSound.start()









205

MORE ACTIONSCRIPT









The start method has up to 2 parameters you can use. The first is to specify a secondOffset, for how

many seconds to skip before playing the file, and the second is the number of times the sound file

is to loop. For example, if we wanted to start playing our sound file at the beginning and loop it 100

times, the script would be:



coinSound.start(0, 100)





To stop playing a sound file, use the stop() method:



coinSound.stop()





You can play multiple sound files at the same time, for example if you want to have a background

soundtrack and then play sound effects as needed by creating multiple sound objects. However, if

you use the stop() method on one sound object, it will stop the playback of all sound objects on the

same timeline or below.









206

MORE ACTIONSCRIPT









DEBUGGING ACTIONSCRIPT



Now that you’re ready to create the next great e-Learning project, one of the new challenges you

have is to learn how to fix the problems in your ActionScript. Flash provides a number of different

methods to test and fix problems in your scripts.





Syntax checking

In the Actions panel, you can perform a syntax check by clicking the syntax check bu on, which will

inform you if there are any problems with your script.









For example, let’s take a look at the following bu on script:



on (release) {

gotoAndPlay();

}





When the syntax check bu on is clicked, the following dialog box is displayed:









207

MORE ACTIONSCRIPT









The Output panel also displays the following message:



**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: Wrong number of

parameters; gotoAndPlay requires between 1 and 2.



gotoAndPlay();



Total ActionScript Errors: 1 Reported Errors: 1



The error is that we’ve used the wrong number of parameters in the gotoAndPlay action. We’ve used

0 parameters and it requires 1 or 2 parameters.





Using the Trace action to Debug

Another common method of finding problems is using the trace() action to display the contents of

a variable to the Output panel to check. In our previous exercise, if we wanted to see the value of the

variable vOne, we could use the trace action this way:



trace(vOne);



This would display the contents of vOne in the Output panel.









208

MORE ACTIONSCRIPT









Using the Flash Debugger



Sometimes it makes the most sense to look for problems by using the Flash debugger. This allows

you to set breakpoints on certain actions in your scripts, and to be able to examine the values of

variables in your movies.



To set a breakpoint in a script, you can click on the action where want to set the breakpoint, then

using the Debug Options drop-down menu, select Set Breakpoint. You can also set a breakpoint by

clicking in the gray column next to the script line where you want to set the breakpoint. Once you’ve

set a breakpoint, a red dot will appear in the gray column next to the script line.



Launching the Debugger



Under the Control menu, select Debug Movie. Your movie will run in a special test mode with the

Debugger Window appearing on top of your movie.



The debugger will allow you to watch variables and step through the execution of scripts so you can

watch how your program is running.









209


Related docs
Other docs by Doni Ibrahim
Multimedia in FlashMX 2004
Views: 10  |  Downloads: 0
Getting Started in FlashMX 2004
Views: 12  |  Downloads: 1
Creating Text in FlashMX 2004
Views: 3  |  Downloads: 0
Drawing in FlashMX 2004
Views: 3  |  Downloads: 0
Working With Symbols
Views: 10  |  Downloads: 1
Flash Learning Interactions
Views: 7  |  Downloads: 1
Introduction to ActionScript
Views: 11  |  Downloads: 0
More ActionScript
Views: 12  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!