Flash - Tutorials and Articles

Loop through child movie clips in AS3 and AS2

To iterate through all children in a display object container…

ActionScript 3 Code Sample:

for(var i=0; i

ActionScript 2 Code Sample:

for (var Item in Parent) {
	if (typeof (Parent[Item]) == "movieclip") {
		Parent[Item].doSomething();
	}
}

Problem targeting Child MovieClip by name in AS3

import flash.display.Sprite;
import flash.display.DisplayObject;

var container:Sprite = new Sprite();

var sprite1:Sprite = new Sprite();
sprite1.name = "sprite1";
var sprite2:Sprite = new Sprite();
sprite2.name = "sprite2";

container.addChild(sprite1);
container.addChild(sprite2);

var target:DisplayObject = container.getChildByName("sprite1");
trace(container.getChildIndex(target)); // 0

Flash AS3 Webservice Solution

I recommend downloading a webservice component from WellConsidered.be to be used with the sample code below:

Sample AS3 Code:

import be.wellconsidered.services.WebService;
import be.wellconsidered.services.Operation;
 
import be.wellconsidered.services.events.OperationEvent;
 
var ws = new WebService("http://www.webservicex.net/WeatherForecast.asmx?wsdl");
var op:Operation = new Operation(ws);
 
op.addEventListener(OperationEvent.COMPLETE, onResult);
op.addEventListener(OperationEvent.FAILED, onFault);
 
op.GetWeatherByPlaceName("new york");
 
function onResult(e:OperationEvent):void { trace(e.data); }
function onFault(e:OperationEvent):void { trace(e.data); }

Download AS3 Web-service Component Here:

http://www.wellconsidered.be/blog/as3-webservice-component/

Flash AS2 using setInterval and clearInterval as a timer

SetInterval comes in handy as a timers or to set time delays for action events. To stop the setInterval from repeating use the the clearInterval(myID) function using the id that you specified for the setInterval. See code sample below for a fully functional counter that ends after 4 seconds. Note that setIntervals time is in milliseconds. So 1000 = 1 second.

var count = 0;
var stopCount = 4;
myFunction = function (message) {
	trace (count +" "+message);
	count++;
	if (count >= stopCount) {
		count = 0;
		clearInterval (intervalID);
	}
};
intervalID = setInterval (myFunction, 1000, "Optional Message Passed");

Time Delay in Flash AS2 with setInterval

It’s relatively simple. Say you want to delay a slideshow of images every 5 seconds using actionscript. You would achive this with setInterval at 5000 milliseconds (every 5 seconds) calling predefined or anonymous function like so:

setInterval( function(){ trace("interval called"); fadeImage(); }, 5000 );

AS3 Pass Variable to FlashVars using LoaderInfo

Unlike AS2, you can nolonger access embeded flashVars from _root using Actionscript 3. You must now using LoaderInfo.

function loaderComplete(myEvent:Event)
{
  var flashVars=this.loaderInfo.parameters;
  userNameTextField.text=flashVars.userName;
}
this.loaderInfo.addEventListener(Event.COMPLETE, loaderComplete);

What is a FlashVar?
FlashVars are nothing more than a list of variables structured as a Query Strings of data (flash.swf?user=bob&pass=123). It’s a way to pass variables from html to a Flash movie.

Actionscript 3: Name Loops to stop parent loop

Say you had a nested loop and you wanted to stop the outer loop if a conditions was met. You would use the Name Loop like so:

// my top loop
outsideLoop:for(var i:uint = 0; i < 10; i++)
{
	trace(i + ": called from outsideLoop.");

	//my inside loop
	insideLoop:for(var j:uint = 0; j < 5; j++)
	{
		trace(j + ": called from insideLoop.");

		if(j < 2) break outsideLoop; // stop the outside loop
	}
}

Unlike as2, in as3, you can name your loop conditions giving you the ability to target those loops to call break when needed. This is very important for when you have deeply nested loops and you want to exit one of the outer loops.

Adobe Flash (previously called Shockwave Flash and Macromedia Flash)

Developed and distributed by Adobe Systems: Since its introduction in 1996, Flash has become a popular method for adding animation and interactivity to web pages; Flash is commonly used to create animation, advertisements, and various web page components, to integrate video into web pages, and more recently, to develop rich Internet applications.

Flash can manipulate vector and raster graphics and supports bi-directional streaming of audio and video. It contains a scripting language called ActionScript. Several software products, systems, and devices are able to create or display Flash content, including Adobe Flash Player, which is available for most common web browsers, some mobile phones and other electronic devices (using Flash Lite). The Adobe Flash Professional multimedia authoring program is used to create content for the Adobe Engagement Platform, such as web applications, games and movies, and content for mobile phones and other embedded devices.

Files in the SWF format, traditionally called “ShockWave Flash” movies, “Flash movies” or “Flash games”, usually have a .swf file extension and may be an object of a web page, strictly “played” in a standalone Flash Player, or incorporated into a Projector, a self-executing Flash movie (with the .exe extension in Microsoft Windows). Flash Video (FLV) files have a .flv file extension and are either used from within .swf files or played through a flv aware player, such as VLC, or QuickTime and Windows Media Player with external codecs added.