Today I came across a perplexing issue when working on applying a BlurFilter to a Movie Clip in a Flash Application. When working with a large container object for a scrolling gallery I found that the BlurFilter I was trying to apply was not being rendered. After some testing and research it appears that Bitmaps have a max height and width of 2880 pixels. This limit was causing the BlurFilter to not render.
If you find that filters aren’t working on your Display Objects it may be that your object exceeds the max dimensions of 2880×2880.
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} );
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, ... );
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.
What is the Flash Context Menu?
The Flash Context Menu is the menu that appears when you right-click on a flash swf. This menu normally contains settings, about, zoom and quality if not modified by the user. This menu is an excellent place to put the copyright or credits of the application with links to author’s own website. If you built it why not put your name on it?
Modify the Context Menu
To modify the Context Menu you must first import a couple classes into your application ContextMenu, ContextMenuItem, and ContextMenuEvent:
import flash.ui.ContextMenu; import flash.ui.ContextMenuItem; import flash.events.ContextMenuEvent;
Now lets go ahead and create a new Context Menu
var myMenu:ContextMenu = new ContextMenu();
After creating the context menu i prefer to hide the default options for zoom and quality utilizing the hideBuiltInItems method
myMenu.hideBuiltInItems();
Now to create our custom options. This is done by instantiating new ContextMenuItems and adding them to the custom items list of the ContextMenu. So for example lets create a new menu item with the text “copyright 2009 MDBitz”.
var webMenuItem:ContextMenuItem = new ContextMenuItem("copyright 2009 MDBitz");
To add the item to our custom menu we push it to the customItems array of the menu
myMenu.customItems.push( webMenuItem );
At this point we have created our new menu item but we have not added any event handlers for if a user selects the option. To listen for the event we add a handler for the MENU_ITEM_SELECT event.
webMenuItem.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, onWebClick );
In the previous step we have set the onWebClick function to be called when the menu item is selected. In this case we are going to open my website in a new window. to do this we are going to use the URLRequest and navigateToURL classes. Again to use these we will first need to import them.
import flash.net.navigateToURL; import flash.net.URLRequest;
Then we simply create the new request and use the navigateToURL class to go to that website in a new window.
public function onWebClick( event:ContextMenuEvent ) { var url:String = "http://www.mdbitz.com"; var request:URLRequest = new URLRequest(url); navigateToURL(request, '_blank'); }
Finally we are ready to set this menu as the context menu for the flash application. This is done by setting the contextMenu to our menu.
this.contextMenu = myMenu;
You know have your own customized context menu. An important item to remember though is that you can only have a limit of 15 items in the custom menu.