February 2010
M T W T F S S
« Jan   Mar »
1234567
891011121314
15161718192021
22232425262728

Flip movieClips horizontally

Problem
How to flip movieClips horizontally

Solution
Use scaleX=-1 and scaleX=1

Detailed explanation

stage.addEventListener(KeyboardEvent.KEY_DOWN, starMoving);
var speed:Number=20;
function starMoving(event:KeyboardEvent):void {
trace(event.keyCode);

switch (event.keyCode) {
case Keyboard.LEFT :
face_mc.x-=speed;
[...]

Append new content to the current loaded swf

Problem
Content has been loaded from a text file. How do we append text through AS3?

Solution
Using appendText method, or just + your content.

Detailed explanation

var myTextObject:URLRequest=new URLRequest(”my_content.txt”);
var myLoaderObject:URLLoader = new URLLoader();

myLoaderObject.addEventListener(Event.COMPLETE,
myContentWasLoaded);
myLoaderObject.load(myTextObject);

function myContentWasLoaded(e:Event):void {
myContent_txt.text=myLoaderObject.data + ” Added some new
text here!!!”;//first way
myContent_txt.appendText(” Hello [...]

Loading content from txt file

Problem
Need load text from *.txt file.

Solution
Use URLRequest and URLLoader. Also using text and data methods.

Detailed explanation

var myTextObject:URLRequest=new URLRequest(”my_content.txt”);
var myLoaderObject:URLLoader = new URLLoader();

myLoaderObject.addEventListener(Event.COMPLETE,
myContentWasLoaded);
myLoaderObject.load(myTextObject);

function myContentWasLoaded(e:Event):void {
myContent_txt.text=myLoaderObject.data;
}

An example is in the source in the attached files.

Source:
loading_content

Thank you for reading.

Using the htmlText method

Problem
Load a text file with html code into Flash

Solution
Use the htmlText method.

Detailed explanation

var myTextObject:URLRequest=new URLRequest(”my_content.txt”);

var myLoaderObject:URLLoader = new URLLoader();

myLoaderObject.addEventListener(Event.COMPLETE,
myContentWasLoaded);

myLoaderObject.load(myTextObject);

function myContentWasLoaded(e:Event):void {

myContent_txt.htmlText=myLoaderObject.data;

}

An example is in the source of the attached file.

Source:
reading_html

Thank you for reading.

Show fps of your game on .swf file

Problem
Need show fps for users

Solution
Using stage.frameRate, also don’t forgot to change types.

Detailed explanation

my_fps.text=String(stage.frameRate);

Source:
fps

Drag and Drop movie clip

Problem
Need drag and drop movie clip + separate code from .fla file to .as file.

Solution
Using startDrag/stopDrag, and addEventListener/removeEventListener.

Detailed explanation

package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class DragDrop extends MovieClip {
function DragDrop() {
[...]

Getting array elements

Problem
Need get array elements on output window and on the stage.

Solution
Using indexOf.

Detailed explanation

globe_mc.buttonMode=true;
bag_mc.buttonMode=true;
dice_mc.buttonMode=true;

var myItems:Array=[globe_mc,bag_mc,dice_mc];

globe_mc.addEventListener(MouseEvent.CLICK, checkMyItems);
bag_mc.addEventListener(MouseEvent.CLICK, checkMyItems);
dice_mc.addEventListener(MouseEvent.CLICK, checkMyItems);

function checkMyItems(event:MouseEvent):void {
trace(event.target.name);
trace(myItems.indexOf(event.target));
arrayElement_txt.text=String(myItems.indexOf(event.target));
}

Source:
gettingArrayElement

Sorting array elements

Problem
Need sort array elements.

Solution
Using loop and indexOf.

Detailed explanation

var
foodArray:Array=[fruit_mc1,fruit_mc2,fruit_mc3,fruit_mc4,fruit_mc5,fruit_mc6,fruit_mc7,fruit_mc8,fruit_mc9,fruit_mc10,fruit_mc11,fruit_mc12,fruit_mc13,drink_mc1,drink_mc2,
drink_mc3, drink_mc4, drink_mc5, drink_mc6, drink_mc7, drink_mc8,
drink_mc9, drink_mc10, drink_mc11, drink_mc12, drink_mc13];

sortingAll_btn.label=”Wanna All”;
sortingAll_btn.useHandCursor=true;
sortingAll_btn.addEventListener(MouseEvent.CLICK, moveFoodToBags);
function moveFoodToBags(event:MouseEvent):void {
var instanceName:String;
fruitBasket_mc.alpha=.5;
drinkBasket_mc.alpha=.5;
for (var i:uint=0; i

Enabling MovieClips as button

Problem
Need to display movieclips as buttons/show users that it’s enabled or active, also assign a Click MouseEvent.

Solution
Using property buttonMode.

Detailed explanation

my_mc.buttonMode=true;
my_mc.addEventListener(MouseEvent.CLICK, gogogo);
function gogogo(event:MouseEvent):void{
trace(”go”);
}

Specifies the button mode of this sprite. If true, this sprite behaves as a button, which means that it triggers the display of the hand cursor when the mouse passes [...]

Percentage Calculator

Problem
Need create percentage calculator.

Solution
Recreating types. Using function. Also added simple error checker. If user not enter numbers in fields he will see error.

Detailed explanation

calculate_btn.useHandCursor=true;

calculate_btn.addEventListener(MouseEvent.CLICK,
persentageCalculate);
function persentageCalculate(event:MouseEvent):void {

var a:Number=Number(sum_txt.text);
var b:Number=Number(percentage_txt.text);
var c:Number;

c=a*b/100;
enteredPercentege_txt.text=String(b);
enteredSum_txt.text=String(a);
[...]