130 likes | 243 Views
Variables, Scope, Images, Sound. IM 215 - ActionScript. Legal Variable Names. may only contain letters, numbers, dollar signs and under-scores must start with a letter or underscore or a dollar sign are case-insensitive
E N D
Variables, Scope, Images, Sound IM 215 - ActionScript
Legal Variable Names • may only contain letters, numbers, dollar signs and under-scores • must start with a letter or underscore or a dollar sign • are case-insensitive • suffixes on your variable names allow for easy type identification when reading • var firstName_str; • var products_array;
Code hints • Typical variable suffixes by type: _mc MovieClip _array Array _str String _btn Button _txt Text Field _sound Sound _color Color _video Video
= vs. == operator if (speed = 100) { //changes the value of speed if (speed == 100) { //does a comparison = sets a value == compares a value This is a common error. If your if/while statements aren’t working, check this first.
Variable TypingStrict vs. Automatic Most programming languages require that variables have explicit data types. Actionscript allows for automatic typing: var firstName; firstName = “Jim”; firstName = “Daisy”; trace (firstName); Convenient, but use explicit typing instead in AS3, e.g.: var firstName:String;
Global vs Local • Variables that can be accessed throughout the program are called global variables. • Variables that are accessible only to limited sections of the program are called local variables. • In Flash, all variables must be scoped to one of the following; • A function (local timeline) • The main timeline or movie clip (root variable)
Variables on different timelines • Create two different shapes (square, circle) on two different layers. Create an Actions Layer, too. • On frame 1 of the square layer set someX to 3 var someX:Number; someX = 3; • On frame 1 of the circle layer, set someY to 4 var someY:Number; someY = 4; • Place instances of the clips on frame 1 layer 1 of the main timeline and name the instances square and circle • On frame 1 of the Actions layer enter the following frame script trace (someX); trace (someY); Test the movie, what happens? (NaN => Not A Number)
Scope Crash! Variables attached to a movie clip timeline have scope limited to that timeline. They are not directly accessible to scripts on other timelines, such as our main movie timeline. What if we place the trace statements on frame 1 of the square layer? Now what happens when you put the trace statements in the circle layer?
Scope in Functions • Try this in Actions: var num1:int = 30; var num2:int = 40; var num3:int = 50; addIt(); function addIt():void { var sum:int = num1 + num2 + num3; trace(“Sum from inside the function”); } • What happens when you trace the sum after the addIt() function call?
Images • Create an empty MovieClip: • Insert -> New Symbol • Give it the name “spotForPicture” • The symbol location on your stage is where the upper left of your image will appear • Name the movieclip on the stage currentPicture • Add the following AS code: • var pic:Loader = new Loader(); • pic.load(new URLRequest(“filename.jpg”)); • currentPicture.addChild(pic); • Use the same code to load a new image—just don’t reinitialize the pic variable.
Sound Objects • To create the sound and start it var my_introsound:Sound = new Sound(); my_introsound.load(new URLRequest("sounds/filename.mp3")); var channel:SoundChannel; channel = my_introsound.play(); • To stop the sound channel.stop(); • See pp. 296-312 in your book
Exercise Create a program that has one frame and four different buttons, Show Image 1, Show Image 2, Play Sound 1, Play sound 2. Do not play a sound or show an image initially. When the user selects a play sound button, a unique sound will play for each button. When the user selects a show image button, a unique image will be displayed for each button.
Sources: “ActionScript 3.0 with Flash CS3 and Flex” (Chapters 3 and 8) by Webster, Yard, and McSharry “Essential ActionScript 3.0” by Colin Moock (O’Reilly)