ActionScript 3 Cheat Sheet

Alt Text

Listed below is the code that i use most in ActionScript 3. I will be adding to it on an ongoing basis. If you have a question about how to use any of the code please feel free to add a comment at the bottom of the page.

OBJECT UPLOAD
// UPLOAD PICTURE

var pictLdr:Loader = new Loader();
var pictURL:String = “img/logo.png”
var pictURLReq:URLRequest = new URLRequest(pictURL);
pictLdr.load(pictURLReq);
this.addChild(pictLdr);

GET URL
// GET ULR

myButton.addEventListener(MouseEvent.MOUSE_DOWN, linkGo);
function linkGo(event:MouseEvent):void{
var getLink:URLRequest = new
URLRequest(“http://www.kahndesign.com/automotive/projectkahn/”); navigateToURL(getLink, ‘_self’);
}

SPRITE
// SPRITE CONTAINER

var container:Sprite = new Sprite
this.addChild(container);

SWFFIT
// SWFFIT

ExternalInterface.call(“swffit.fit”,’webdesign’,’990′, pageHeightNum);

STAGE NO SCALE ALIGN
// STAGE

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

CSS
// CSS STYLE SHEET

var css:StyleSheet = new StyleSheet;
var body:Object = new Object();
body.color = “#333333″;
body.fontSize = “12”;
body.fontFamily = “Calibri”;
body.letterSpacing = “1”;
body.leading = “-1″;

var titleColor:Object = new Object();
titleColor.color = “#a92400″;
titleColor.fontSize = “12”;
titleColor.fontWeight = “bold”;

var commentColor:Object = new Object();
commentColor.color = “#a95700″;
commentColor.fontSize = “12”;

css.setStyle(“body”, body);
css.setStyle(“.titleColor”, titleColor);
css.setStyle(“.commentColor”, commentColor);

TEXT FORMAT SET TEXT FORMAT
// TEXT FORMAT

var headerText:TextFormat = new TextFormat();
headerText.color = 0x006699;
headerText.font = “Calibri”;
headerText.size = “24”;
headerText.letterSpacing = 0;
headerText.leading = -1;
headerText.bold = true;
yourText.setTextFormat(headerText);

NEW TEXT FIELD
// TEXT FIELD

var yourText:TextField = new TextField();
container.addChild(yourText);
yourText.x = 40;
yourText.y = storyOne.y – 40;
yourText.width = 80;
yourText.height = 20;
yourText.htmlText = “Latest news”;
yourText.setTextFormat(menuText);}

TEXT FUNCTION
// TEXT FUNCTION

var myText:TextField = new TextField();
this.addChild(myText);
myText.x = 450;
myText.y = 570 + homeFit;
myText.width = 633;
myText.height = 200;
myText.multiline = true;
myText.wordWrap = true;
myText.styleSheet = css;
myText.htmlText = ” Go”;
myText.addEventListener(TextEvent.LINK, goLink);

function goLink(evt:TextEvent):void {
if(evt.text == “goWebsites”){
trace(“goWebsites”)
}
}

COLOUR ARRAY RECTANGLE GRADIENT PLAIN
// CREATE COLOUR ARRAY

var gradType:String = GradientType.LINEAR;
var matrix:Matrix = new Matrix();
// matrix.createGradientBox(boxWidth, boxHeight, boxRotation, x, y);
matrix.createGradientBox(624, 1400, 0, 8, 147)
var colors:Array = [0xDEDEDE, 0xEFEFEF];
var alphas:Array = [1, 1];
var ratios:Array = [0, 120];

// CREATE GRADIENT RECTANGLE
var rectangleGrad = new Sprite();
rectangleGrad.graphics.beginGradientFill(gradType, colors, alphas, ratios, matrix)
rectangleGrad.graphics.drawRect(8, 147, 624, 1400);
container.addChild(rectangleGrad);
rectangleGrad.filters = myfilters;
// CREATE RECTANGLE
var rectangle:Sprite = new Sprite();
rectangle.graphics.lineStyle(1, 0xCCCCCC);
rectangle.graphics.beginFill(0xCCCCCC, 0);
rectangle.graphics.drawRect(8, 147, 624, 1400);
rectangle.graphics.endFill();
container.addChild(rectangle);

EMPTY CONTAINER
// WHILE NUMCHILDREN

while (container.numChildren) {
container.removeChildAt(0);}

CREATE LINE
// CREATE LINE

var myLinesp:Sprite = new Sprite();
container.addChild(myLinesp);
var myLineg:Graphics = myLinesp.graphics
myLineg.lineStyle(1, 0xCCCCCC);
myLineg.moveTo(12, 100);
myLineg.lineTo(112, 100);

SWF ADDRESS SET VALUE + TITLE
// SWF ADDRESS

SWFAddress.setValue(“/” + storyOneAddressText + “/”);
SWFAddress.setTitle(storyOneAddressText);

MOUSE EVENT
// MOUSE EVENT

storyOne.addEventListener(MouseEvent.MOUSE_DOWN, storyOneBig);
function storyOneBig(event:MouseEvent):void{
}

DROP SHADOW FILTER
// DROP SHADOW FILTER

import flash.filters.DropShadowFilter;
var myfilters:Array = [];
var dropShadow:DropShadowFilter = new DropShadowFilter();
dropShadow.distance = 4;
dropShadow.alpha = 0.4;
dropShadow.blurX = 4;
dropShadow.blurY = 4;
dropShadow.quality = 2;
myfilters.push(dropShadow);

SCROLLING TEXT
// TEXT AREA

import fl.controls.ScrollBarDirection;
import fl.controls.TextArea;
import fl.events.ScrollEvent;
var myScrollText:TextArea = new TextArea();
myScrollText.setSize(318, 60);
myScrollText.move(8, 125);
myScrollText.addEventListener(ScrollEvent.SCROLL, scrollScrollTextOneHandler);
myScrollText.htmlText += “kgk h kjhlj “;
addChild(myScrollText);
function scrollScrollTextOneHandler(event:ScrollEvent):void {
var myScrollTextOneTA:TextArea = event.currentTarget as TextArea;
var posScrollTextOne:Number = event.position;
var maxposScrollTextOne:Number;
if (event.direction == ScrollBarDirection.VERTICAL) {
maxposScrollTextOne = myScrollTextOneTA.maxVerticalScrollPosition;
} else {
maxposScrollTextOne = myScrollTextOneTA.maxHorizontalScrollPosition;
}
}
You Need:
TextArea and UIScrollBar
The problem is that the fl.events.ScrollEvent is tied to the UIScrollBar component. You need to add one of these components to your library, and all will be well.

DRAG AND DROP
// DRAG OBJECT

yourObject.dragyourObject.buttonMode = true;

yourObject.dragyourObject.addEventListener(MouseEvent.MOUSE_DOWN,DragStartyourObject);
yourObject.dragyourObject.addEventListener(MouseEvent.MOUSE_UP,DragStopyourObject);
function DragStartyourObject(event:Event) {
yourObject.startDrag(false);
}

function DragStopyourObject(event:Event) {
yourObject.stopDrag();
}

SEND VARIABLES (PAYPAL)
// SEND VARIABLES

yourObject.addEventListener(MouseEvent.MOUSE_DOWN, buy_6);
function buy_6(event:MouseEvent):void{
b = 6;
var Variables:URLVariables = new URLVariables();
Variables.cmd = “_xclick”;
Variables.business = “yourPayPalEmailAddress@gmail.com”;
Variables.currency_code = “GBP”;
Variables.amount = navData.channel.item.price.text()[b];
Variables.item_name = navData.channel.item.info.text()[b] + ” – ” + navData.channel.item.model.text()[b] + ” – ” + navData.channel.item.number.text()[b];
Variables.shipping = navData.channel.item.shipping.text()[b];
var Send:URLRequest = new URLRequest(“https://www.paypal.com/cgi-bin/webscr”);
Send.data = Variables;
navigateToURL(Send, ‘_blank’);
}

TWEEN
// TWEEN

var storyOneBigOutTween:Tween = new Tween(storyOneLdr, “scaleX”,
Strong.easeOut, 0.5, 1, 1, true);
import fl.transitions.Tween;
import fl.transitions.easing.*;

INCLUDE
// INCLUDE

include “as/menu.as”

COLOUR CHANGE
// COLOUR CHANGE

var myColor:ColorTransform = _home.homeText.transform.colorTransform;
myColor.color = 0xFCDD57;
_home.homeText.transform.colorTransform = myColor;

TIMER
// TIMER

var timer:Timer = new Timer(2000, 5);
timer.addEventListener(TimerEvent.TIMER, doThis);
timer.start();
function doThis(e:TimerEvent):void{
trace(“Times Fired: ” + e.currentTarget.currentCount);
trace(“Time Delayed: ” + e.currentTarget.delay);
//if (timerCounter == 0){
trace(“timer”);
// }

COLOUR CHANGE
// COLOUR CHANGE

var myColor:ColorTransform = _home.homeText.transform.colorTransform;
myColor.color = 0xFCDD57;
_home.homeText.transform.colorTransform = myColor;

TIMER
// TIMER

var timer:Timer = new Timer(1000); timer.addEventListener(TimerEvent.TIMER, onTimer);
function onTimer(evt:TimerEvent):void{
timer.stop();
//timer.removeEventListener(TimerEvent.TIMER, onTimer);
}

ARRAY CREATE LOTS OF MC’s
// ARRAY CREATE LOTS OF MC’s

var i:Number = 0;
var image:Array = [];
trace(navData.channel.item[0].img.text());
var r:Number = navData.channel.item.length();
trace(r);
while(i < r){

// CREATE MOVIE CLIP IMAGE

var image_mc:MovieClip = new MovieClip();
image_mc.name = “image_mc” + i;
container.addChild(image_mc);
image[i] = image_mc;
image_mc.buttonMode = true;
// UPLOAD PICTURE

var pictLdr:Loader = new Loader();
var pictURL:String = navData.channel.item[i].img.text();
var pictURLReq:URLRequest = new URLRequest(pictURL);
pictLdr.load(pictURLReq);
image[i].addChild(pictLdr);
pictLdr.x = 400*i;
i++;
}

decimal places
// 2 decimal places

var num:Number = 4;
trace(num.toFixed(2)); // 4.00

TEXT HEIGHT
//TEXT HEIGHT

myText.textHeight;

IS IT A NUMBER?
// checks to see if Number is defined

if (isNaN(myNumber));

BLUR
// APPLY BLUR

import flash.filters.BitmapFilterQuality;
import flash.filters.BlurFilter;
var blur:BlurFilter = new BlurFilter();
blur.blurX = 55;
blur.blurY = 55;
blur.quality = BitmapFilterQuality.MEDIUM;
yourThing.filters = [blur];

RANDOM COLOUR
// RANDOM COLOUR

colour = Math.random() * 0xffffff;

TALK TO A LOADED MOVIECLIP
// TALK TO A LOADED MOVIECLIP

MovieClip(downloadLdr.content).stop();

REMOVE CHILD
// REMOVE CHILD

containerInvoice.removeChild(downloadLdr);
trace (downloadLdr);
downloadLdr = null;
trace (downloadLdr);

STOP ALL SOUNDS
// STOP ALL SOUNDS

SoundMixer.stopAll();

EMBED FONTS
// EMBED FONTS

var myFont:Font = new Font1();
var myFont2:Font = new Font2();
// “Georgia Italic Bold”;
// “Verdana, Arial”;
// myFont.fontName;
// myFont2.fontName;

// CSS STYLE SHEET
var css:StyleSheet = new StyleSheet;
var body:Object = new Object();
body.color = “#4a2c22″;
body.fontSize = “18”;
body.fontFamily = myFont.fontName;
body.letterSpacing = “1”;
body.leading = “-1″;
// invoiceBodyText.embedFonts = true;
// invoiceBodyText.antiAliasType = true;

// LOADER 100%
// CSS STYLE SHEET

var css:StyleSheet = new StyleSheet;
var body:Object = new Object();
body.fontFamily = “Arial”;
body.color = “#333333″;
body.fontSize = “40”;
body.letterSpacing = “1”;
body.leading = “4”;
var bodyText:Object = new Object();
bodyText.fontFamily = “Arial”;
bodyText.color = “#333333″;
bodyText.fontSize = “40”;
bodyText.letterSpacing = “1”;
bodyText.leading = “4”;
css.setStyle(“body”, body);
css.setStyle(“.bodyText”, bodyText);
// CSS STYLE SHEET END

// TEXT FIELD
var homeTitle:TextField = new TextField();
this.addChild(homeTitle);
homeTitle.x = 0;
homeTitle.y = 0;
homeTitle.width = 300;
homeTitle.height = 300;
homeTitle.styleSheet = css;
// Listeners for large image
var beeLargerequest:URLRequest = new
URLRequest(“img/jockeyBlueStarLarge.png”);
var beeLargeLoader:Loader = new Loader();
beeLargeLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, beeLargeLoadProgress);
beeLargeLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, beeLargeLoadComplete);
// Function for large image PROGRESS
function beeLargeLoadProgress(event:ProgressEvent):void {
var percentLoadedLarge:Number = event.bytesLoaded/event.bytesTotal;
percentLoadedLarge = Math.round(percentLoadedLarge * 100);
homeTitle.htmlText = “”;
homeTitle.htmlText += (“Loading: “+percentLoadedLarge+”%”);
homeTitle.htmlText += “”;
}
// Function for large image COMPLETE
function beeLargeLoadComplete(event:Event):void {
this.addChild(beeLargeLoader);
}
beeLargeLoader.load(beeLargerequest);

11 thoughts on “ActionScript 3 Cheat Sheet

    • Hi Antonio, if you ever need to check to see if a number is NaN, you should always use isNaN().
      if (isNaN(myNumber)) // checks to see if Number is defined

      Adding ! does look correct but the statement should be written as above.

    • If you put ! the result of true (“yes, its NOT a number”) then becomes a check for false (“no, this is a number”)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>