Actionscript

string replace

var str:String = "AS3 rocks!";
var search:String = "AS3";
var replace:String = "Actionscript 3";

function strReplace(str:String, search:String, replace:String):String {
return str.split(search).join(replace);
}

trace(strReplace(str, search, replace)); //Outputs Actionscript 3 rocks!

tweenlite

//import the GreenSock classes
import com.greensock.*;
import com.greensock.easing.*;

//tween the MovieClip named "mc" to an alpha of 0.5 over the course of 3 seconds
TweenLite.to(mc, 3, {alpha:0.5});
//scale myButton to 150% (scaleX/scaleY of 1.5) using the Elastic.easeOut ease for 2 seconds
TweenLite.to(myButton, 2, {scaleX:1.5, scaleY:1.5, ease:Elastic.easeOut});
//tween mc3 in FROM 100 pixels above wherever it is now, and an alpha of 0. (notice the vars object defines the starting values instead of the ending values)
TweenLite.from(mc3, 1, {y:"-100", alpha:0});
//after a delay of 3 seconds, tween mc for 5 seconds, sliding it across the screen by changing its "x" property to 300, using the Back.easeOut ease to make it shoot past it and come back, and then call the onFinishTween() function, passing two parameters: 5 and mc
TweenLite.to(mc, 5, {delay:3, x:300, ease:Back.easeOut, onComplete:onFinishTween, onCompleteParams:[5, mc]});
function onFinishTween(param1:Number, param2:MovieClip):void {
trace("The tween has finished! param1 = "   param1   ", and param2 = "   param2);
}
//call myFunction() after 2 seconds, passing 1 parameter: "myParam"
TweenLite.delayedCall(2, myFunction, ["myParam"]);
//use the object-oriented syntax to create a TweenLite instance and store it so we can reverse, restart, or pause it later.
var myTween:TweenLite = new TweenLite(mc2, 3, {y:200, alpha:0.5, onComplete:myFunction});
//some time later (maybe in by a ROLL_OUT event handler for a button), reverse the tween, causing it to go backwards to its beginning from wherever it is now.
myTween.reverse();
//pause the tween
myTween.pause();
//restart the tween
myTween.restart();
//make the tween jump to exactly its 2-second point
myTween.currentTime = 2;

wit scherm na loading

na loader

[Embed(source="../../../../../../bin/assets/menuscreen/bg.png")]
                                public var BM:Class;
                                public var backgroundBMP:Bitmap;

                                public function MenuScreen()
                                {
                                                this.background = null;//forget this for the first screen// 'assets/menuscreen/bg.png';
                                                backgroundBMP = new BM();
                                                backgroundBMP.smoothing = true;
                                                addChild(backgroundBMP);
}

prevent android device from dimming screen

in main.as
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;

in application.xml
<uses-permission android:name="android.permission.WAKE_LOCK"/>

Log Line Numbers

trace(">",new Error().getStackTrace().match(/(?<=:)[0-9]*(?=])/g)[0]);

remove from parent

if (obj.parent)
{
obj.parent.removeChild(obj);
trace('remove chld');
}

saveSharedObject

var saveDataObject:SharedObject;
saveDataObject = SharedObject.getLocal("test");
of
in 1en
saveDataObject:SharedObject = SharedObject.getLocal("test");

saveDataObject.data.savedScore = currentScore;

When you want the SharedObject to actually, immediately write its save data to its file on the player’s local hard drive you flush it:

saveDataObject.flush();

This is a very processor intensive action to call, so use it sparingly (never include it an a loop function).

To load “currentScore” from the “savedScore”, we would write:

currentScore = saveDataObject.data.savedScore;

remove a specific object

this.collectionOtherObjects.splice(this.collectionOtherObjects.indexOf(bombC), 1);

masking cutting out shapes

1) Inflexible hole cutting:

this.graphics.beginFill(0x666666);
this.graphics.drawRect(0,0,256, 256);
this.graphics.drawCircle(128,128,32);
this.graphics.endFill();
this will create a rectangle of 256 by 256 with a 64px hole in it.

2) Flexible hole cutting:

Obviously this will not work when you're not using the graphics class. In That case I would go with BlendMode.ERASE.

var spr:Sprite = new Sprite();
var msk:Sprite = new Sprite();

addChild(spr);
spr.addChild(msk)

spr.graphics.beginFill(0x666666);
spr.graphics.drawRect(0,0,256, 256);
spr.graphics.endFill();

msk.graphics.beginFill(0x000000);
msk.graphics.drawEllipse(0,0,64,64);
msk.graphics.endFill();
msk.x = 128;
msk.y = 128;

spr.blendMode = BlendMode.LAYER;
msk.blendMode = BlendMode.ERASE;

Random min max && random color

Random between min max:
var x:* = min + (max - min) * Math.random();

Random Color:
var randomColor:Number = Math.random()*0xFFFFFF;

min max array

var myArray:Array = [2,3,3,4,2,2,5,6,7,2];
var maxValue:Number = Math.max.apply(null, myArray);
var minValue:Number = Math.min.apply(null, myArray);

Subscribe to RSS - Actionscript