When working with tweens in Flash we often create custom time-line based animations for various elements of the project. These time-line animations can sometimes need to be played in reverse by the frame count and not by a time duration. If one utilizes the TweenLite or TweenMax Tweening Framework this task can easily be completed by use of the frameLabel and useFrames parameters.
The frameLabel property defines the name of the frame that the movie clip should be tweened to enabling us to tween a movie clip both forward and backwards.
TweenLite.to( movieclip, 1.5, { frameLabel:"intro" } );
The above example will tween to the labeled frame over 1.5 seconds. If instead we want our animation to play over 60 frames we would use the useFrames property which treats the second parameter as the number of frames and not number of seconds.
TweenLite.to( movieclip, 60, {frameLabel:"intro", useFrames:true} );
Lastly we may want to have the animation play back to a previous label using the exact animation duration that it was created with. This task although more complicated can be accomplished the first step is to set the ease type to Linear.easeNone which will make the animation play uniformly. The second step is to know the exact number of frames between the current frame of the movie clip and the desired frameLabel. Flash does not provide an interface for obtaining the frame number of a labeled frame and instead you will have to create your own utility and formulas for determining the duration. A simple method would be to have an associated array or dictionary of the frame labels with their corresponding frame number that you can then get the difference of the current frame and that value.
TweenLite.to( movieclip, frameDiff, {frameLabel:"intro", useFrames:true, ease:Linear.easeNone} );
ActionScript 3, AS3, frameLabel, TweenLite, TweenMax, useFrames
There are often odd cases when in a Flash Application you find that by design you want to call a function but only have the name of the function as a String. This could be caused by various reasons one being that the name is stored in a database as part of some settings. To accomplish this conversion we make use of the Function object.
How to Convert a String to a Function
var functionName:String = "toString";
// obtain the function handler
var namedFunction:Function = this[fName];
// execute the function and pass any desired arguments
nameFunction( arg1, arg2, ... );
ActionScript 3, AS3, Function
Generating a random number for use in Flash is a common problem that many developers come across. Luckily the Math object provides static methods to help us perform this basic task. Found below are two snippets of how to generate a random integer from 0 to a max number or for a range of values.
How to Generate a random number from 0 to a max value:
Math.floor( Math.random() * (maxNum + 1) );
Examples:
Problem – return a number between 0 and 10
Math.floor( Math.random() * 11 );
Problem – return a number between 0 and the length of an array
Math.floor( Math.random() * myArray.length );
How to Generate a random number between the range min and max inclusively:
Math.floor( minNum +
(Math.random() * (maxNum - minNum + 1) ) );
Examples:
Problem – return a number between 10 and 45
var minNum = 10;
var maxNum = 35;
Math.floor( minNum + ( Math.random() * (maxNum - minNum + 1 ) ) );
//-- Math.floor( 10 + (Math.random() * 26) );
Code Break Down:
In the two methods we used the Math.random() and Math.floor() functions to generate our random number for us. The Math.random() function returns a value between 0 and 1 with 1 excluded. While the Math.floor() function returns the whole number or integer below the current value ( e.g. Math.floor( 3.82 ) = 3 ). Utilizing these methods we generate a number in the desired range by multiplying the random number generated by our max value + 1 as the floor function will only return a value of max value.
ActionScript 3, AS3, Random
Array duplication or copying is actually a very simple task with multiple methods of the Array class returning a new Array. The only drawback is that their is no clone, copy, or duplicate method that would make it obvious to first time users. Instead the Array class has the concat and slice function that can be used to return a new Array with all the same entries as the first.
Concat method
The concat method concatenates the parameters passed to the array and returns the new array with all the entries. Therefore if you pass nothing into the function you will be returned with a duplicate of the original.
var array:Array = new Array(1, 2, 3);
var copyArray:Array = array.concat();
Slice method
The slice method is used to return the values in an array from the start index to the end index without modifying the original array. Much like concat if you pass no arguments you will be returned with an array containing all the values of the first.
var array:Array = new Array(1,2,3);
var copyArray:Array = array.slice();
Resources
For those creating dynamic text fields in Flash it often comes as a surprise that dynamic text fields do not behave as expected when modifying the alpha parameter of it or it’s parent. Often even I forget that telling a movie clip to transition out based on a tween of its alpha property doesn’t modify the text field. Without getting into the details of why this occurs lets jump straight to one solution for quickly making the alpha property work, setting it’s Blend Mode!
The Quick Fix
- Import the BlendMode class
import flash.display.BlendMode;
- Set the blendMode of your TextFields to Layer
this._panel.currentTimeTxt.blendMode = BlendMode.LAYER;
That is it!! Now you will be able to modify the alpha parameter of the field or it’s parents and the dynamic text fields will behave as expected.
Alpha, Dynamic Text Fields, TextField