Bits & Bytes

Posts Tagged ‘adobe’

Type Conversions of Primitive Types in Actionscript

We can perform explicit type conversions by using constructor notation like this:

var dFloat:Number = 3.14159;
var iConv:int = int(dFloat);

Here, we have the conversion of a floating-point (Number) type to an integer (int) type. A conversion from a Number to an int truncates the decimal so that the converted value “qConv” holds the value 3 after these lines are executed. In general, this is how the Number type gets converted to an int or a uint type: the value is simply truncated at the decimal point.

The Number type holds a much wider range than either the uint or int type can hold. When a Number type variable’s value lies outside the range of an int or a uint and a conversion takes place, a truncation occurs and the value is wrapped until it lies in the range of the target type.

var dFloat:Number = -3.14159;
var uiConv:uint = uint(dFloat);

In the example above, the negative value of “dFloat” lies outside the range of the uint type. So, the result of this conversion is 4294967293, which is the same value you would get if you converted an int with the value of -3 to the unsigned (uint) type.

var bBool:Boolean = true;
var uiConv:uint = uint(bBool);

We can convert numbers to booleans and vice versa. A Boolean variable with the value true gets converted to 1 and the value false gets converted to 0. This is the case regardless of whether the number type we are converting to is a uint, int or Number. So, the code segment above results in “uiConv” taking on the value 1 after the conversion.

var iNeg:int = -1;
var bConv:Boolean = Boolean(iNeg);

When converting to a Boolean type from a numerical type, the value 0 converts to false, as does the value NaN for Number type variables. All other numerical values convert to true. So, for example, the code above converts the value -1 to true.

Converting numerical types to a String is pretty straight-forward. The value is converted to a string representation of the number. Likewise, Boolean values are converted to the strings “true” or “false,” as expected.

var sString:String = "a3.14";
var dConv:Number = Number(sString);

However, converting values from a String type is somewhat strange. A String converts to a Boolean value of false if the String is null or empty (“”). Otherwise, it converts to true. A String converts to a number if it can be converted. If a String contains non-numeric characters it converts to 0 for int and uint types, but converts to NaN for a Number type. The code above converts the string “a3.14” to NaN.

How to Import an Image to the Library and Display it in Actionscript 3.0

In this post, I demonstrate how to import an image to the library and display it in Actionscript 3.0. Displaying an image that imported to the stage is automatic, but displaying an image that is imported to the library requires creating a class for the image. This is an important step toward using images in programs.

1. Open Flash and create an Actionscript 3.0 project by left-clicking “Flash File (Actionscript 3)” under “Create New” at the start up screen.

2. Next, we want to import an image to the library. To do this, Left-click “File” in the menubar, mouse over “Import” and left-click “Import To Library” in the submenu.

3. Select an image and left-click the “Open” button to import the image.

4. Right-click the image in the “Library” pane and left-click “Properties…” to open the “Bitmap Properties” dialog.

5. Click the box “Export for Actionscript” and we will change the “Class:” to “XoaXLogo” in this case, but you can use whatever name is appropriate. Finally, left-click the “OK” button.

6. Now you will see this warning dialog. Left-click the “OK” button in the dialog to create the new class.

7. Make sure that the “Actions” pane open by left-clicking “Window” in the menubar and left-clicking “Actions” in the submenu if it is not checked.

addChild(new Bitmap(new XoaXLogo(480, 360)));

8. Add the line of code above to the “Actions” Pane. Here, the “Actions” pane is shown docked in the left of the window.

9. Then left-click “Control” and “Test Movie” in the submenu to run the program and you should see you image displayed, just as we showed at the top of this post.

10. The line of code that we added creates an instance of our image class ”XoaXLogo’ with the same size as our original image and stores that in a Bitmap class instance, which we add to the stage via a call to addChild(). Displayable objects that are added to the stage are automatically displayed.

11. At this point, the project is not saved. To save it, select “File” and “Save As…” in the submenu. Then find a location, enter the a filename into the “File name:” box, like “DrawBitmap.fla” and left-click the “Save” button to save the .fla file.

Creating a Simple Motion Tween in Adobe Flash

In this post, I demostrate how to create a simple motion tween, which is an interpolated animation in Flash. This example creates a rectangle that moves across the screen, just like the one above.


1. Left-click the Adobe Flash icon under the Start menu to Open Adobe Flash.


2. Under “Create New,” left-click “Flash File” with the latest version of Actionscript.


3. Left-click the “Rectangle Tool” from the tools toolbar.


4. Left-click and drag to create a rectangle in the stage area.
5. Left-click “Selection Tool” in the tools toolbar.


6. Right-click the first frame in the Timeline an left-click “Create Motion Tween” in the pop-up menu.


7. This will pop up a dialog that says “The selected item cannot be tweened. …” Left-click the “OK” button to create the tween.
8. Now you can left-click and drag the right end of the tween region to stretch it over as many frames as you wish. We will leave it at the default length of 24 frames, which is 1 second.


9. Right-click the last frame, the 24th in our case, and mouse over “Insert Keyframe” and left-click “Position” in the submenu. You should now see a dot in that frame to indicate a keyframe.


10. Grab and drag the rectangle to a desired destination and you should a path of dots from the original position to the new position.
11. These dots represent when the rectangle will be at the intermediate frames.
12. To see the animation, left-click “Control” in the menubar and “Test Movie” in the submenu. You should now see a new window with the animation running in it.

 

© 2007–2026 XoaX.net LLC. All rights reserved.