»
Creating 3D Classes with ActionScript 2.0
Published 10/1/2006
Creating 3D Classes with ActionScript 2.0
This article describes how to create 3D effects in Macromedia Flash using a class-based programming model. I begin with an overview of the new syntax describing many of the new features in ActionScript 2.0, followed by a presentation of a 3D calculation method that uses quaternion math for fast, efficient 3D transformations. Rather than demonstrating cool 3D effects, I emphasize creating classes that you can use in 3D engines. I examine these classes and demonstrate how to implement them to create a 3D scene. Hopefully, after reading this article you'll be able to use or modify the code presented here to explore 3D and class-based programming on your own. To complete this tutorial you will need to install the following software and files: Macromedia Flash MX Professional 2004 Download sample files for this tutorial
|
»
Building Preloaders and Progress Bars in Macromedia Flash
Published 09/30/2006
Building Preloaders and Progress Bars in Macromedia Flash
One of the unique features of web content built with Macromedia Flash is the ability to control when and how the content loads. When loading a heavy HTML page, the user is usually stuck looking at a blank window until the content starts appearing. Flash allows for the creation of animated preloaders, which give the user precise information about the progress of the loading process. A simple rectangular progress bar or percentage indicator will do the job, but why stop there? A preloader should be given just as much love and consideration as the rest of the site content, especially on a site that is trying to evoke a mood, or create an immersive experience. If a preloader is engaging enough, the user won't mind waiting for content, and the time it takes to load will seem shorter. The preloader is the first element someone will see when visiting your site. You can make a good first impression by welcoming your visitors with a snappy preloader.
Tutorials and sample files: Prerequisite knowledge Basic knowledge of ActionScript 2.0
|
»
Dynamically creating instances of a class
Published 09/30/2006
Dynamically creating instances of a class You needn't always drag an instance of a symbol onto the Stage during authoring time; you can also create new instances of a class in the Library using ActionScript by using the new operator. Using the new operator Previous versions of ActionScript forced you to create new MovieClip or TextField instances by using methods such as MovieClip.attachMovie(), MovieClip.createEmptyMovieClip(), or MovieClip.createTextField(). In ActionScript 3.0, you can create new MovieClip, TextField, Sprite, and Video instances—or your own custom classes—by calling new Ball(), as seen in the following example: -
Delete the ball symbol from the Stage in the flancyBall.fla document and add the following ActionScript to the main Timeline: var b1:Ball = new Ball(); - Press Control+Enter (Command+Return) to test the SWF file. Notice that nothing appears on the Stage, but the following text appears in the Output panel: "ball created: instance1". Although Flash creates a new instance of the Ball class as requested, it isn't visible on the Stage because you didn't add the symbol to the display list using the
addChild() method. -
Modify the code from Step 1 to add the following line of code, which adds the b1 instance to the display list: Next you need to use a document class, which allows you to move code off of frame 1 of the timeline and place it in an external file instead. This is similar to associating a class with a symbol, as shown in previous examples. Using the Document Class text box The next example demonstrates how you can move code off the Timeline and place it in an AS file: - Delete the code in frame 1 of the fancyCircle.fla document.
- Create a new ActionScript document and save it as BallDocumentClass.as in the same folder as the fancyCircle.fla document.
-
Add the following ActionScript code to BallDocumentClass.as: package { import flash.display.MovieClip; public class BallDocumentClass extends MovieClip { private var tempBall:Ball; private var MAX_BALLS:uint = 10; public function BallDocumentClass() { var i:uint; for (i = 0; i < MAX_BALLS; i++) { tempBall = new Ball (); tempBall.scaleX = Math.random(); tempBall.scaleY = tempBall.scaleX; tempBall.x = Math.round(Math.random() * (this.stage.stageWidth - tempBall.width)); tempBall.y = Math.round(Math.random() * (this.stage.stageHeight - tempBall.height)); addChild(tempBall); } } } } - Save and close the AS file and open the fancyBall.fla document.
- Type BallDocumentClass in the Document Class text box in the Property inspector and then save the FLA file.
- Select Control > Test Movie to preview the SWF file. You should see 10 instances of the ball symbol at various shapes and sizes on the Stage.
Using the Document Class text field allows you to place ActionScript code in external files instead of within a frame on the main Timeline, reuse the code between numerous FLA files, and check the ActionScript code into a Concurrent Versioning System (CVS)—making it much easier to share code within a large team.
|
»
Converting code into a class
Published 09/30/2006
Converting code into a class The following example creates an ActionScript class which encapsulates all the logic that makes a movie clip draggable. Now whenever a new instance of this class is created, the shape is automatically draggable without having to write additional code for each instance on the Stage: - Create a new FLA file and save it to your hard drive as fancyBall.fla.
- Select File > New and choose ActionScript File to create a new AS file.
- Save the new ActionScript document as Ball.as in the same folder as fancyBall.fla (created in Step 1).
-
Add the following code to the ActionScript document: package { import flash.display.MovieClip; import flash.events.MouseEvent; public class Ball extends MovieClip { public function Ball() { trace("ball created: " + this.name); this.buttonMode = true; this.addEventListener( MouseEvent.CLICK, clickHandler); this.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownListener); this.addEventListener( MouseEvent.MOUSE_UP, mouseUpListener); } private function clickHandler(event:MouseEvent):void { trace("You clicked the ball"); } function mouseDownListener(event:MouseEvent):void { this.startDrag(); } function mouseUpListener(event:MouseEvent):void { this.stopDrag(); } } } This code defines a new class called Ball, which extends the MovieClip class (now located in the flash.display package in ActionScript 3.0). Notice that you must explicitly import classes when you work with external ActionScript documents, unlike when you work with FLA files. - Save and close the Ball.as document and then open the fancyBall.fla document.
- Use the Oval tool to create a circle on the Stage and convert it to a MovieClip symbol, following the steps from the previous examples.
- Right-click the symbol in the Library panel and select Linkage from the context menu.
- Select Export for ActionScript and type Ball in the Class text box.
- Click OK to apply the changes and close the dialog box.
- After making sure that an instance of the ball symbol is on the Stage, select Control > Test Movie to create a SWF file. When you click the mouse on the ball instance, notice that the mouse cursor changes to a hand cursor and the symbol is still draggable.
It gets better! The next example shows you how to create class instances dynamically.
|
»
Creating clickable and draggable shapes
Published 09/30/2006
Creating clickable and draggable shapes In this example, you'll explore some of the new features in the Flash Professional 9 ActionScript 3.0 Preview. You will create a simple shape and make it clickable using the improved event model. In later examples, you will make the shape draggable, turn your code into a class, and dynamically create a class instance. Clickable shapes Let's start with making the shape clickable: - Select File > New and then choose Flash Document from the New Document dialog box, or select Flash Document from the Start page. Save the document as simpleBall.fla.
-
Select the Oval tool from the Tools panel and draw a circle on the Stage. To create a perfect circle, press and hold the Shift key while drawing the shape. Note: Make sure that Object Drawing mode is turned off when you create the shape. - Select the Selection tool and double-click the shape on the Stage to highlight it.
- With the shape selected, choose Modify > Convert to Symbol (or press F8) to open the Convert to Symbol dialog box.
- Change the Name text field to circle and click OK to convert the shape into a movie clip.
- With the symbol still selected on the Stage, type ball_mc in the Instance Name text box in the Property inspector.
- Deselect the symbol on the Stage and open the Actions panel.
-
Add the following ActionScript code in the Script pane: ball_mc.addEventListener(MouseEvent.CLICK, clickHandler); function clickHandler(event:MouseEvent):void { trace("You clicked the ball"); } In this code, the ball_mc instance becomes clickable because you add an event listener that detects when the user clicks the shape. Whenever the user clicks the ball_mc movie clip, the clickHandler() function is dispatched. This is similar to adding event listeners to components in earlier versions of ActionScript. For example, ActionScript 2.0 used the onPress() event handler to detect when a user clicks a movie clip or button instance. - Select Control > Test Movie to publish your file. When you click the circle, the Output panel opens and displays the text, "You clicked the ball".
-
Close the SWF file and return to the Flash authoring environment to modify your ActionScript code. Add the following line of ActionScript above the existing code: ball_mc.buttonMode = true; - Republish the Flash document. Now when your mouse cursor appears over the circle, the cursor changes into a hand, giving users a visual clue that they can click the shape.
Draggable shapes If you want to let the user drag the shape around the Stage, you need to add a couple more event listeners for the mouseDown (MouseEvent.MOUSE_DOWN) and mouseUp (MouseEvent.MOUSE_UP) events, as shown in the next example. The following example demonstrates how add listeners for the mouseDown and mouseUp events by adding event listeners to the ball movie clip: -
Modify the ActionScript from the previous sample so it matches the following code: ball_mc.buttonMode = true; ball_mc.addEventListener(MouseEvent.CLICK, clickHandler); ball_mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener); ball_mc.addEventListener(MouseEvent.MOUSE_UP, mouseUpListener); function clickHandler(event:MouseEvent):void { trace("You clicked the ball"); } function mouseDownListener(event:MouseEvent):void { ball_mc.startDrag(); } function mouseUpListener(event:MouseEvent):void { ball_mc.stopDrag(); } - Select Control > Test Movie to publish the Flash document. When you click and hold the left mouse button over the circle, you can move the shape around the Stage. When you release the mouse button, you can no longer drag the shape around the Stage.
While dragging shapes on the Stage can provide hours of entertainment, you would not want to rewrite that code every time you wanted to create a draggable shape. Imagine if you could convert that code into an external class file that lets you associate a symbol with a class that automatically makes a symbol draggable. Read on to find out how.
|
PSDTop offers Royalty Free files like PSDs and JPGs. PSD files are prepared for instant use in collages, banners, animations and other computer graphic related tasks - PSD Tuts!!! Files are multi layered and isolated. Just download a file, imort it to a program able to read .psd format (Adobe Photoshop, GIMP, other) and that's it - a new layer is added. PSD files are zipped to make transfer faster.
|