Friday, June 24, 2005
 
A few weeks ago I won a copy of Studio MX2004 in Actionscript.org's 'Massive Competition'. It arrived today, and I just wanted to take a moment to thank both Actionscript.org and Macromedia (in particular Mike Chambers) for supplying the prize ... and some cool extra goodies to go along with it.

Thanks also to Jesse and Strok, the founders of Actionscript.org, who provided an additional prize out of their own pockets (no, I don't mean bits of fluff and half eaten polo mints) when their second sponsor pulled out (an Amazon voucher, which I used to purchase the Extending-Flash JSFL book) :)




Sunday, June 19, 2005
 
Using MovieClip Setters to Store Counter Values

Sometimes you want to associate a simple numerical value (e.g. that of an iterator) with an instance of an attached/duplicated movieclip.
In certain situations movieclip instances get reset, and any properties which had been dynamically assigned to them get overwritten (...therefore lost forever).
There are various ways around this, but when replying to a question tonight, I used a quick, hassle free method which I thought I'd pop up here on the blog. Properties available via getters/setters do not seem to suffer the same fate. "_x", and "_rotation" for example retain their values even after external content has been loaded in, so where possible you can utilise one of these.


Consider a situation where you're extracting URL values from XML data, and within the same loop you start loading images into n-instances of a duplicated movieclip using the MovieClipLoader class. These images could finish loading in any order and trigger their respective onLoadComplete listeners, but "onLoadComplete" only accepts one argument - the movieclip instance, yet it would be helpful to also pass along a numerical id so that you can associate [for example...] data in arrays with the correct clip. There are various ways to do this, but they involve a few steps, and you can't simply do the following, because the value will get lost when the image loads in to the clip:

//In scope of XML object
//This won't work
for(var i=0;i<nodeLength;++i)
{
mc = this.attachMovie("....etc
//create new property to store iterator...
mc.iVal= i;//this gets overwritten!!
}

However, you can simply store this value in an unused property of MovieClip, via the setter, then retrieve it later (and reset the proprty if necessary. It will make more sense to see it in code:

//-----------------------------|
//inside scope of XML object...
for(var i=0;i<nodeLength;++i)
{
mc = this.attachMovie("....etc
mc._rotation = i; //store iterator value here
mc._visible = false; //hide rotated clip
mc.loadClip(someImg, mc);//load img using MCL
}
//-----------------------------|
//inside scope of MCL
mclListener.onLoadComplete = function(clip:MovieClip)
{
//Retrieve iterator value.
//Use Math.round here because Flash is
//inaccurate and returns a floating point number
var iVal = Math.round(clip._rotation)
clip._rotation = 0; //reset rotation
clip._visible = true; //make visible again
clip.onRelease = function()
{
//use iterator value
getURL(someURLArray[iVal], "_blank");
//etc...
};
}




ARCHIVES

  AS Hero   blog feed

nwebb.co.uk - flash tutorials, php and more.