RSS

MovieClip transparency to mouse click

Actionscript 3 | Posted on Jun 30 2009

ActionScript 3, by default you cannot click through a display object to items underneath, it will block the mouse events from happening. In order to click through the MovieClip to the button behind we need to set two properties: mouseEnabled and mouseChildren. The mouseEnabled property sets whether or not the mouse registers on the given clip while the mouseChildren sets whether children within the given clip register the mouse.

my_mc.mouseEnabled = false;
my_mc.mouseChildren = false;

Full Screen in AS3

Actionscript 3 | Posted on Jun 30 2009

I been play around with the Fullscreen functionality of Actionscript 3 and used for one of my personal project “Last Day 2009″. This code is pretty simple. What it does is make the Flash movie go full screen when you click on button. Keep in mind that this will only work when you view this in the browser. It’s doesn’t work when compiled inside of Flash CS3.

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

btn_full.addEventListener(MouseEvent.CLICK, fullScreen);
btn_full.buttonMode=true;

function fullScreen(e:MouseEvent):void {
    if (stage.displayState==StageDisplayState.NORMAL) {
        stage.displayState=StageDisplayState.FULL_SCREEN;
        e.currentTarget.gotoAndStop(1);
    } else {
        stage.displayState=StageDisplayState.NORMAL;
        e.currentTarget.gotoAndStop(2);
    }
}

The html source code that embeds the flash movie within your page needs to have the parameters allowFullScreen set to True and menu needs to be set to True as well.

<script type="text/javascript">
    var so = new SWFObject("main.swf", "blackhole", "100%", "100%", "8", "#000000");
    so.addParam("allowFullScreen", "true");
    so.addParam("scale", "noscale");
    so.write("flashcontent");
</script>

Download the source code

Swap Depth using AS3 via setChildIndex

Actionscript 3 | Posted on Jun 26 2009

Use the setChildIndex method to changes the position of an existing child in the display object container with the instances name “holder_mc” to the highest depth..

function setDepth(target) {
    var maxIndex:Number = maxIndex = numChildren - 1;
    setChildIndex(target, maxIndex);
}

setDepth("holder_mc")

root or parent in AS3

Actionscript 2, Actionscript 3 | Posted on Jun 16 2009

The root used to be the main timeLine right but in AS3 that is gone. Here post some kind of direction how would you have a movieClip commutate with the main timLine.

In AS2 you could just do _parent._parent as many times as needed to reach up the tree to get to the parent’s top level.

_root.onOff = true;
_parent._parent.subMenuOnOff();

In AS3 you need to use this code below instead.

MovieClip(this.root).onOff = true;
MovieClip(this.parent.parent).subMenuOnOff();

Custom right-click menu with AS3

Actionscript 3 | Posted on Jun 15 2009

This tutorial will teach you how to customize the context menu by removing the default items and adding your own items instead. AS3 already provides you with a class that controls the menu: ContextMenu, and classes that handle the properties and events: ContextMenuItem and ContextMenuEvent.

//Create an instance of ContextMenu in order to use it
var myMenu:ContextMenu = new ContextMenu();

myMenu.hideBuiltInItems();

//Create your custom item as a ContextMenuItem
var menuItem1:ContextMenuItem = new ContextMenuItem("Click and do nothing");

//If you want to add action to your custom item,
//add the item and then add a listener to listen for the event
var menuItem2:ContextMenuItem = new ContextMenuItem("Go to My Home Page");
menuItem2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, doSomething);

//Add your custom item to "myMenu":
myMenu.customItems.push(menuItem1);
myMenu.customItems.push(menuItem2);

//Tell Flash where your custom menu appears when you right-click.
this.contextMenu = myMenu;

//Define your listener function for the menu item that contains the action
function doSomething(e:ContextMenuEvent):void {
    var url:String = "http://www.mouseup.me";
    var request:URLRequest = new URLRequest(url);
    navigateToURL(request, '_blank');
}

Sorting Arrays in AS3

Actionscript 2, Actionscript 3 | Posted on Jun 04 2009

One of my previous projects at work involve displaying a list of data to the user, it need to sort according to the planner list and display the calculation to the user. Here I set up a quick little test actionscript which provides a couple of interesting findings:

var food:Array = new Array({food:"Jelly",list:12},
                           {food:"Coke",list:1},
                           {food:"Cake",list:5},
                           {food:"Burger",list:2})

food.sortOn("food")
trace(food[0].food) //Burger
trace(food[1].food) //Cake
trace(food[2].food) //Coke
var numbers:Array = new Array(3,5,100,34,10);
trace(numbers); // 3,5,100,34,10

numbers.sort();
trace(numbers); // 10,100,3,34,5

numbers.sort(Array.NUMERIC);
trace(numbers); // 3,5,10,34,100

How can you randomize the results of an array in a for loop? Sort method can randomize array if you put function that return -1, 0 or 1 randomly, which basiclly tells sort randomly are elements less then, equal or greater then eash other.

function randomSort(elementA:Object, elementB:Object):Number {
    var sortNum : int = Math.round(Math.random() * 2) - 1;
    return sortNum;
}

var numbers:Array = new Array(1,2,3,4,5 );
trace(numbers.sort(randomSort)); //3,2,1,4,5