Flash AS3 - Tutorials and Articles

Actionscript 2 to 3 migration Help

Need help migrating over to the AS3 language. First thing you probably want to download is the “AS3 Migration Cookbook from Adobe. You can use it as a high-level intro to AS3 and a quick reference for executing frequent code task. The guide covers four topics: (more…)

Flash AS3 Slider Tween using Mouse Position

First here is the code without the easing effect. I used the stage as the view scroller width and added an Enter Frame event to the stage for movement. (more…)

Flash AS3 Get Child By Name

Say you wanted to grab a movie clip nested in side of a container movie clip. You can do this using getChildByName:

var referenceMC:MovieClip = MovieClip(containerMC.getChildByName(“targetMC”));

Now you have access to that targeted movie clip’s properties and for example could change the alpha to let’s say 50%

var referenceMC:MovieClip = MovieClip(containerMC.getChildByName(“targetMC”));
referenceMC.alpha = 0.5;

If you wanted to access multiple movie clips nested in a container by name you could use a for loop. This example assume that there are 5 movie clips named img0, img1, img2, img3…etc. We will loop through and set all movie clips alphas to 50%.

for (i=0; i<5; i++) {
var item_mc:MovieClip;
item_mc = MovieClip(cards.getChildByName(“img”+i));
item_mc.alpha=0.5;
}

You can also do this by using getAtIndex. See here:
http://am3media.com/site/flash/loop-through-child-movie-clips-in-as3-and-as2

Flash and SEO. Why not?

Google learns to crawl Flash
Google has been developing a new algorithm for indexing textual content in Flash files…
Source: http://googleblog.blogspot.com/2008/06/google-learns-to-crawl-flash.html

How to SEO Flash
•    Search Engines and Flash
•    Requirements for Successful Use of Flash
•    SEO Flash Programming
•    Example: Making Flash Home Page Spiderable
•    Scalable Inman Flash Replacement
Source: http://www.hochmanconsultants.com/articles/seo-friendly-flash.shtml

Here are a few comments on this topic from SearchEngineGuide.com:
“Stoney, You hit the nail on the head and pounded it down with one stroke. I completely agree, I have turned down clients in my own business due to flash sites. Now that I am with an agency I find Flash more common, as many of the businesses are big enough and niched enough where they feel that they don’t need SEO for their website. Oddly enough they are often right. Unfortunately SEO can have a big effect on PPC marketing, and most of these accounts suffer from bad keyword quality scores and thus higher click costs in AdWords.”

“Susan: I disagree slightly. Mostly because it all comes down to the intent. Building a flash only website for Ozzy Osbourne is fine, but e-commerce is not. I think you know what I mean”

“ Working on seo for hotel sites, I’ve come across some stunning creative sites designed with Flash. If the designers had thoughtfully used the Flash applications to “wow” site visitors, ie embedding in otherwise SE-friendly pages, it could be a win-win situation.
Perhaps all of you have seen the June info about making Flash SE-friendly. ( http://googleblog.blogspot.com/2008/06/google-learns-to-crawl-flash.html )”
Source: http://www.searchengineguide.com/stoney-degeyter/why-i-still-wont-seo-flash-websites.php

And from SearchEngineJournal.com:
“Disney does it. And so does Oprah. Even my favorite pizza place does it…  Granted these are extreme cases of sites which use flash extensively, but there are other cases where even a little flash can be improperly used. There are also cases where flash is not only appropriate, it is recommended. The question then becomes how to best use flash without affecting search engine rankings”

“Yeah this article doesn’t help a bit. I thought after reading I would be able to find a solution for my all flash website for seo purposes. Thefwa.com is an all flash website, they rank no.1, they didn’t follow your mumbo jumbo about not using too much flash” (Note from Alex: TheFWA.com does not rank no.1. They rank 31,905. Still not bad for an all flash site.)
Source: http://www.searchenginejournal.com/flash-and-seo-using-flash-on-websites/2247/

Cool online tool that will probe a flash file:

http://www.flashprobe.com/

Flash AS3 and IE Popup Blocker Fix

Flash button events are blocked sometimes some internet browsers bult-in popup blockers (ad blockers). I had this problem in the past in AS2 with IE on a PC. I could usably resolve the AS2 popup problem by changing the mouse event from onClick to onRelease or by changing the target from _blank to _self.

As with most AS3 code, the workaround is a bit more complicated. We can resolve this problem by calling a Javascript function via Actionscrip using the ExternalInterfac class.

function getMyURL(url:String, window:String ):void {
	var req:URLRequest=url is String?new URLRequest(url):url;
	if (! ExternalInterface.available) {
		navigateToURL(req, window);
	} else {
		var strUserAgent:String=String(ExternalInterface.call("function() {return navigator.userAgent;}")).toLowerCase();
		if (strUserAgent.indexOf("firefox") != -1 || (strUserAgent.indexOf("msie") != -1 && uint(strUserAgent.substr(strUserAgent.indexOf("msie") + 5, 3)) >= 7)) {
			ExternalInterface.call("window.open", req.url, window);
		} else {
			navigateToURL(req, window);
		}
	}
}

// call the getMyURL using a button mouse event
btn_mc.addEventListener(MouseEvent.MOUSE_UP,navClicked);
function navClicked(e:MouseEvent):void {
	trace("clicked");
	getMyURL("http://newsourcemedia.com", "_self");
}

AS3 access root from loaded swf

In the root path of your external swf file, defined an object that represent the root path of the container swf.

var rootObj:Object=this.parent.parent.parent as Object;

Get SWF url path and domain name using AS3

In as2 you would use:
trace(_url);

Now in as3 you will have to use loadInfo:
trace(loaderInfo.url);

If you know there is a .com in the domain name, just do a split on the info using “.com”. Now the third item in that array (index 2) should be your domain name.
var swfDomain = loaderInfo.url;
var pathArray = swfDomain.split(“.com”);
trace(pathArray[2]); // should be yourdomain.com if on a web server

Flash AS3 Target Root Path

In ActionScript 2, it was easy to access root, parents, and grandparents. However, in ActionScript 3, we are in for a big surprise when trying to access root the old way like trace(_root.myVar). The AS3 compiler will throw a error the next time you try to use code that looks something like this:

//this code will throw a compiler error
this
._root.myMC.x = 10;

If you must access a child of root, you must explicitly tell the compiler, root is a MovieClip! This is called type casting. The following example will work error free.

//this code casts root as MovieClip, and it’s fully legal
MovieClip
(this.root).myMC.x=20;
trace(
MovieClip(this.root).myVar);

Flash AS3 Set Mask Dynamically

You can set a mask in AS3 using the following actionscript.

function init(){
container_mc.mask = mask_mc;
}
init();

If you have an alpha gradient mask, you will have to use cacheAsBitmap:

function init(){
container_mc.cacheAsBitmap = true;
mask_mc.cacheAsBitmap = true;
container_mc.mask = mask_mc;
}
init();

How to trigger a button click in AS3

I know you could do this vary easy in AS2 just by calling the myButton.onClick(); function. But, we all know with AS3, there is much more code to write.

This will give you a trace of “hi world” just as if you clicked the button in actionscript 3:

myButton.addEventListener(MouseEvent.CLICK, onClick);
myButton.myVar = "hi world";

function onClick(e:Event = null):void {
trace(e.target.myVar)
}

myButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));