Welcome to my actionscript snippet library!

Actionscript Snips is a small repository of actionscript code that aims to save developers and designers time when developing a website.  

Right-Click Context Menu


var menu:ContextMenu = new ContextMenu();
menu.hideBuiltInItems();
var menuItem:ContextMenuItem = new ContextMenuItem("Go to google");
menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, goToContextMenu);
menu.customItems.push(menuItem);
this.contextMenu = menu;

function goToContextMenu(event:ContextMenuEvent):void {
var link:URLRequest = new URLRequest("http://www.google.com");
navigateToURL(link,"_blank");
}

Center A Display Object


centerObject(obj); //Centers a display object

function centerObject(obj:DisplayObject):void {
obj.x = stage.stageWidth / 2 - obj.width / 2;
obj.y = stage.stageHeight / 2 - obj.height / 2;
}

Draw A Triangle


var triangle:Shape;

drawTriangle(0, 10, 100, 0xFF0000);

function drawTriangle(x:Number, y:Number, height:Number, color:uint){
triangle = new Shape;
triangle.graphics.beginFill(color);
triangle.graphics.moveTo(height/2, y);
triangle.graphics.lineTo(height, height+y);
triangle.graphics.lineTo(x, height+y);
triangle.graphics.lineTo(height/2, y);
addChild(triangle);
}