Why Loops Are Useful
Loops enable Flash to perform an action (or set of actions) repeatedly, which means that with just a few lines of ActionScript, you can force an action to be executed several thousand times. In ActionScript, you use loops for tasks that would be difficult or impossible without loops. For example:Creating dynamically generated drop-down listsValidating dataSearching textDynamically duplicating movie clipsCopying the contents of one array to a new arrayDetecting collisions in games between projectiles and objects
You can use loops to automate any number of tasks, such as dynamically creating movie clip instances. Suppose your project called for 100 evenly spaced instances of the same movie clip. You could drag one instance from the library and then create a four- or five-line looping statement to duplicate the instance 100 times and position each duplicate on the stage automaticallya great improvement over dragging 100 instances from the library and aligning each one manually.
Loops are also dynamic. Suppose you scripted a loop to create a menu of 15 choices (buttons) dynamically. By altering the loop slightly, you could add and remove choices dynamically. In contrast, adding or removing a button from a manually created menu involves adding or removing the choice and then moving all of the other choices up or down one position, and perhaps even rescripting here and there.As you progress through this lesson (and the rest of the book), you'll see the value of using loops in your scripts.
• Table of ContentsMacromedia® Flash MX 2004 ActionScript: Training from the SourceBy
Derek Franklin, Jobe Makar Publisher: Peachpit PressPub Date: November 19, 2003ISBN: 0-321-21343-2Pages: 636
Sure, you can use Flash MX 2004 without being a master programmer, but as any Flash developer worth his or her salt will tell you, you''''re not tapping all of its power unless you''''re taking advantage of its scripting language "ActionScript 2.0" which offers a more robust programming model and better object-oriented programming support than ever before. Here to take the fear factor out of learning it are Flash veterans and best-selling authors Derek Franklin and Jobe Makar, who demonstrate that scripting is an instinctual process you already know by translating real-life activities into ActionScript scripts. In these pages, you''''ll find methodologies and techniques for building over 40 real-life Flash ActionScript projects, including sample games, applications, Web sites, and more. New in this edition are coverage of ActionScript 2.0, Web services, Components, Printing, Video, and more. On the companion CD, you''''ll find all the project files and images you need to complete each project.