Posts tagged quick snippet

#quick snippet: UNIX Timestamp to as3 Date

0

The problem:
Our flashmovie fetches a Timestamp (UNIX) from some kind of backend code and you need to change this to a workable Date format. It’s actually pretty easy and almost totally standard in flash!


Note:
Flash handles time in milliseconds (think about the timerclass that expects 3000 if you want to time 3 seconds) and the UNIX timestamp is a format based on seconds from the “first day” computers started to use the Gregorian Calendar. In short: the timestamp is defined as the number of seconds elapsed since midnight Universal Time (UTC) of January 1, 1970. Your computer keeps track of how many seconds have elapsed since then and the flash Date class expects this in milliseconds. Simply multiply your seconds with 1000 and you’re done. Easy peasy!


The solution:
A quick code snippet that’s even easy to read for – let’s say – mobile developers!

// replace the capitalized variable with your unix timestamp
// multiply it with 1000 (remember my note above!)
var currDate:Date = new Date(Number(YOUR_TIMESTAMP_HERE)*1000); 
 
// trace out the humanly readable values			
trace(currDate.getDate());
trace(currDate.getMonth());
trace(currDate.getFullYear());



Done!


A real life flash example (DateDisplayer):

Download this wonderful widget here!

Go now, my young actionscript children! Feed on my example and make me proud! Soon the world will be filled with actionscript 3.0 vampires!

#quick snippet: right-click menu in as3

0

The intro:
I noticed a friend of mine always builds in a custom right-click menu in his flash websites and decided that this is an excellent code snippet to share with you boys and girls.


The code:
It’s actually pretty simple so here’s the full block. Additional tips are given in actionscript comment style.

// create a new instance of the ContextMenu class
var my_instance:ContextMenu = new ContextMenu();
 
// hide the standard items
my_instance.hideBuiltInItems();
 
// create a new ContextMenuItem by feeding the constructor a piece of text
var menuItem_instance:ContextMenuItem = new ContextMenuItem("made by crazy beaver");
 
// maybe you even want one that has a link behind it
var menuItem2_instance:ContextMenuItem = new ContextMenuItem("crazy beaver");
// add a listener to this item
menuItem2_instance.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, clickHandler);
 
// push all your menuItems in the ContextMenu instance
my_instance.customItems.push(menuItem_instance, menuItem2_instance);
 
// set the default ContextMenu for your movie to your own contextMenu
this.contextMenu = my_instance;

Don’t forget to create a handler for the items that have listeners to it (in this case “menuItem2_instance”) like so:

private function clickHandler(e:Event):void {
trace("you clicked on the crazy beaver link!");
}

It’s a small snippet, but it’s a nice feature to have and definately worth to implement in your future projects!

Go to Top