Equipie
Actionscript 3, Flex, and Air. Code snippets and notes...
Monday, 30 January 2012
AS3: Stage event listeners and weak references
Whilst loading and unloading stage event listeners, I had problems with the following listener:
stage.addEventListener(MouseEvent.MOUSE_UP, stopScroll);This was used in a movieclip that was loaded within a parent movieclip, after the loaded movieclip was discarded threw up null pointer error as the listener was still present on the main stage. The key here is to ensure the event listener has a weak reference:
stage.addEventListener(MouseEvent.MOUSE_UP, stopScroll, false, 0, true);This way it is discarded with the movieclip that instantiated it!
Wednesday, 25 January 2012
AS3: Delay code execution
Rather than push loads of code through at the same time, it can be advantageous to delay execution of certain blocks of code:
import flash.utils.*;
var delayStart = setInterval(startCode,3000);
function startCode():void {
// insert delayed code here
// stop setInterval repeating
clearInterval(delayStart);
}
This is useful to remember as a swf may have a lot to do upon initial load (drawing graphics on screen, preparing data structures etc.) So to delay an animation or resource intensive task may be a smart thing to do.
Monday, 10 October 2011
AS3: buttonMode = true and hand cursor still doesn't appear!
I was working on a large project that incorporated the Strobe Media player (SMP), but for some strange reason my buttons lost their hand cursor once the user interacted with the SMP.
I couldn't figure this out for a long time until I noticed that they were setting the hand cursor on the UI like so:
which would override the default behaviour, once I added the same listeners to my own UI elements it was all sorted!
I couldn't figure this out for a long time until I noticed that they were setting the hand cursor on the UI like so:
private function theMouseOut(e:MouseEvent):void {
Mouse.cursor = flash.ui.MouseCursor.ARROW;
}
private function theMouseOver(e:MouseEvent):void {
Mouse.cursor = flash.ui.MouseCursor.BUTTON;
}
which would override the default behaviour, once I added the same listeners to my own UI elements it was all sorted!
Friday, 9 September 2011
AS3: FLVPlayback activeVideoPlayerIndex and multiple videos
Working on an old-school project that had an annoying flicker when switching between video's, here is a sample of the code:
function setVideoSequence(url1in:String, url2in:String):void {
movFLV.flvPlayback.activeVideoPlayerIndex = 0;
movFLV.flvPlayback.source = url2in;
movFLV.flvPlayback.activeVideoPlayerIndex = 1;
movFLV.flvPlayback.source = url1in;
movFLV.flvPlayback.visibleVideoPlayerIndex = 1;
movFLV.flvPlayback.play();
url2 = url2in;
movFLV.flvPlayback.addEventListener(fl.video.VideoEvent.COMPLETE, loadNextVideo);
}
function loadNextVideo(e:fl.video.VideoEvent):void {
movFLV.flvPlayback.activeVideoPlayerIndex = 0;
movFLV.flvPlayback.visibleVideoPlayerIndex = 0;
movFLV.flvPlayback.play();
}
Tuesday, 30 August 2011
AS3: Randomise elements within an array
Lovely function to randomise the order of elements within an array:
This could be implemented as follows:
For further thoughts on randomisation see How to Randomly Shuffle an Array in AS3
private function randomSort(a:*, b:*):Number {
if (Math.random() < 0.5) return -1;
else return 1;
}
This could be implemented as follows:
clipRef.push(movItem1); clipRef.push(movItem2); clipRef.push(movItem3); clipRef.push(movItem4); clipRef.push(movItem5); clipRef.push(movItem6); clipRef.push(movItem7); clipRef.push(movItem8); clipRef.sort(randomSort);
For further thoughts on randomisation see How to Randomly Shuffle an Array in AS3
Tuesday, 19 July 2011
AS3: Brand the right-click menu!
Right click menu's are often overlooked and provide a nice touch to any project.
Here is a code routine that I've used for many past projects, it should hopefully give you an idea on how to set up a right click menu.
for a quick hit use the vars at the top and the last block. The for/switch loops through some items loaded via XML, its code from a live project so perhaps a little verbose.
// context menu
private var nextMenu:ContextMenu;
private var cxLabel:String;
private var cxTarget:String;
private var menuItem:ContextMenuItem;
private function setupContextMenu():void {
nextMenu = new ContextMenu();
nextMenu.hideBuiltInItems();
for(var i:int = 1; i < 10 ; i++){
cxLabel = xmlSource.LoaderMax["Item"+i]. @ menuLabel;
menuItem = new ContextMenuItem(cxLabel);
switch (i){
case 1:
menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, function(e:Event):void { navigateToURL(new URLRequest(links["1-link"]), "_self"); }, false, 0, false);
break;
case 2:
menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, function(e:Event):void { navigateToURL(new URLRequest(links["2-link"]), "_self"); }, false, 0, false);
break;
case 3:
menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, function(e:Event):void { navigateToURL(new URLRequest(links["3-link"]), "_self"); }, false, 0, false);
break;
case 4:
menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, function(e:Event):void { navigateToURL(new URLRequest(links["4-link"]), "_self"); }, false, 0, false);
break;
case 5:
menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, function(e:Event):void { navigateToURL(new URLRequest(links["5-link"]), "_self"); }, false, 0, false);
break;
case 6:
menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, function(e:Event):void { navigateToURL(new URLRequest(links["6-link"]), "_self"); }, false, 0, false);
break;
}
nextMenu.customItems.push(menuItem);
}
var menuItemNext:ContextMenuItem = new ContextMenuItem("© 2012 your brand.");
menuItemNext.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, function(e:Event):void {
navigateToURL(new URLRequest("http://www.noursite.co.uk/"), "_self"); } );
menuItemNext.separatorBefore = true;
nextMenu.customItems.push(menuItemNext);
this.contextMenu = nextMenu;
}
Subscribe to:
Posts (Atom)