• Aucun résultat trouvé

Transition to OOP

Dans le document ActionScript 3.0 Design Patterns (Page 26-31)

Changing from sequential or procedural programming to OOP programming is more than picking up a language that gives you little choice in the matter, as is the case with Java. However, certain changes in a language can make it more amenable to OOP, even if it’s not considered a true OOP language by some criterion. In the fol-lowing sections, some new features in Flash CS3 provide a summary of relevant changes to the way ActionScript is used.

MovieClip and Button scripts

For the ActionScript veterans whose introduction to programming was writing little sequential scripts or procedures using theonstatements associated withMovieClipor Button objects, you’re probably aware that the latest version of Flash doesn’t allow script embedded in either.

Built-in State Machines: while most programmers welcomed the demise of movie clip and button embedded scripts, one astute pro-grammer observed that Flash used to have built-in state machines.

Jonathan Kaye, PhD, co-author of Flash MX for Interactive Simula-tion: How to Construct and Use Device Simulations(Delmar Learning, 2002), noted that the button and movie clip scripts often served to cre-ate stcre-ate machines. Each button or movie clip could serve as an encap-sulated, context-sensitive trigger for changing state. (See how design patterns deal with State Machines in Chapter 10.)

In general, the demise of movie clip and button scripts is seen as a boon to better programming, especially OOP programming. Keeping track of the isolated button and movie clip codes could be a headache, even with relatively small applications.

For structuring larger programs, where OOP and Design Patterns are most useful, having movie clips and buttons floating around with their own code moves the prob-lem from the realm of headache to nightmare. So, for learning design patterns, be glad you don’t even have to think about little scripts isolated in movie clips and buttons.

Timeline scripts

Another kind of scripting you’ll be seeing less of in Flash are those embedded in your Timeline. For the most part, placing scripts in the Timeline probably left a lot to be desired in the first place, but worked out to be a convenient location. In Action-Script 2.0, you were able to place a script in a class and call it from a script embed-ded in the Timeline, and so all that the Timeline code was really used for was to call a class that would launch a script in an ActionScript file (.as). That being the case, the Flash CS3.flafile has a little window where you can add the name of the class to call. (See the next section.) So, if all you want to do is to call a program and compile it into an SWF file, you no longer need to use the Timeline for your code at all.

However, Flash CS3 doesn’t do away with code in the Timeline. You can still use it, but in this book, we use it selectively only with movie clips that are called from a class outside the movie clip or button class. (See the section “Movie clip and button classes.”)

Document class

You won’t be placing much, if any, code in the Timeline using ActionScript 3.0.

Rather than using an object with a Timeline script, you can now compile your .as files by entering the name of the class name you want to launch your application.

Figure 1-1 shows how to use the Document class window in the Properties panel to enter the name of the class you want to launch:

You can still use the Timeline, but unless there’s a good reason to do so, there’s no need. Most of the examples in this book use the Sprite object instead of the

MovieClip class. A Sprite object has no Timeline, but a MovieClip class does. So usingSprite objects save a bit of extra weight that the Timeline has.

Movie clip and button classes

In Flash CS3, MovieClip and Button objects you create using the Symbol dialog box and store in the Library can be made available to work with ActionScript 3.0. Unlike ActionScript 2.0 where MovieClip and Button symbols could be associated with a class, with Flash CS3,they can be made into classes themselves. The object’s name entered into the Name window when the symbols are created becomes the class name for the object. (In past versions, references to a movie clip are made through an instance name. You can still make those references, but in a different context.) The advantage of this new procedure is that the symbol objects can be instantiated just like any other class through the code, as long as the symbols are in the Library.

You don’t have to place them on the stage. They can be dynamically instantiated and placed into a display array just like a dynamically generated object. Further, objects contained within the MovieClip or Button can be addressed as a property just like any other class.

While this book is in no way an introduction to Flash CS3, walking through one example of this new way of creating a class with movie clips and buttons may be use-ful to those new to Flash and experienced users alike. The following steps walk you through this new feature:

1. Open a new Flash document and save it asrocket.fla.

2. Select InsertNew Symbol from the menu bar to open the Create New Symbol Dialog box. EnterRocketin the Name window, and Click OK to enter the Sym-bol Edit Mode.

3. In the Symbol Edit Mode, draw a rocket on the stage with the dimensions W=89, H=14, as shown in Figure 1-2. Once finished, position the drawing at X=0, Y=0. Click the Scene 1 icon to exit the Symbol Edit Mode.

Figure 1-1. Document class window

4. Select Insert New Symbol from the menu bar to open the Convert to Symbol Dialog box. EnterFireRocketin the Name window, select Movie clip as Type, and click the Export for ActionScript checkbox. Once you’ve clicked the check-box, Figure 1-3 shows what the dialog box looks like. Notice that the Base class is flash.display.MovieClip. The base class is the reference to the package required for ActionScript to display a MovieClip object. Click OK to enter the Symbol Edit Mode.

Figure 1-2. Rocket drawing

Figure 1-3. Setting a MovieClip class Center point

5. Drag a copy of the Rocket movie clip from the Library to the center of the stage.

Move the center point of the movie clip to the rear of the rocket and position it at X=0, Y=0.

6. Click on Frame 40 of the Timeline and press F5 to create 40 frames. Click Frame 40 again and press F6 to insert a keyframe. Click on the keyframe in Frame 40 and move the rocket to X=400, Y=0.

7. Click on the first keyframe, and, in the tween drop-down menu in the Properties inspector, select Motion. You should now see a blue arrow in the Timeline.

Move the playhead from left to right to make sure that the motion tween is working right. Figure 1-4 shows what you should see.

8. Open the Actions panel. Click on a blank area of the stage to make sure you don’t have any objects selected, and then click on Frame 1. In the Actions panel, type in thestop() statement. Save theRocket.fla file.

9. Open a new ActionScript file and save it asTestRocket.asin the same folder as theRocket.flafile. Enter the script in Example 1-1 in theTestRocket.asfile, and save the file once again:

Figure 1-4. Rocket in motion tween Center point

Current position Ending position

10. Finally, open theRocket.flafile, and in the Document class window in the Prop-erties panel, type inTestRocketand save the file. Then test the movie by press-ing Ctrl + Enter (Command + Return on the Mac). You should see the rocket move from left to right across the screen and then return to its original position.

Using Flash in a more or less traditional manner to create movie clips is still an important part of using ActionScript, but it has changed. You can no longer attach a class to a movie clip as was the case in previous versions. However, in creating appli-cations using design patterns, you can still integrate different aspects created in the Flash IDE. So while ActionScript 3.0 has made the leap to a true ECMAScript lan-guage, it has not abandoned its roots in animated graphics.

Dans le document ActionScript 3.0 Design Patterns (Page 26-31)