PDA

View Full Version : AS3: reading color value from the URL


felisan
05-20-2008, 07:09 AM
hi everybody.


I'd like to be able to pass a parameter to my Flash-file, so that it can have a customizable background-color.

to do this, I've made this little test:
I've placed a black movieclip on stage, and instancenamed it; myBox.

then Ive tried this code:
import caurina.transitions.Tweener;

var myBackgroundColor:Number = 0x53616E;
Tweener.addTween(myBox, {_color:myBackgroundColor, time:0.7, transition:"linear"});

it works perfectly, myBox changes color.

then I've tried this code:

import caurina.transitions.Tweener;

var myBackgroundColor:Number = Number(root.loaderInfo.parameters.whatColor);
Tweener.addTween(myBox, {_color:myBackgroundColor, time:0.7, transition:"linear"});


with this: myBox.swf?whatColor=0x53616E

but this doesn't work.
what am I doing wrong?


thanks
felisan

Slowburn
05-20-2008, 03:20 PM
when you grab a flashVar from the root timeline it comes in as a String. so you'll have to convert it to a uint first.

here is a little function that will do that for you.

// These are the strings coming from your FlashVars ( diff. variations )
var flashVarColor1:String = "#FF00FF";
var flashVarColor2:String = "0xFF00FF";
var flashVarColor3:String = "FF00FF";

// parse/convert string value to uint
function getColorValue( value:String ):uint
{
var COLOR_VALUE:RegExp = /^#{1}|0x{1}(([a-f0-9]){3}(([a-f0-9]){3})?)/i;
if( value.search( COLOR_VALUE ) > -1 )
value = value.replace( COLOR_VALUE, "0x$1" );
return parseInt( value, 16 );
}

// test output
trace( "Test1: ", getColorValue( flashVarColor1 ) ); // 16711935
trace( "Test2: ", getColorValue( flashVarColor2 ) ); // 16711935
trace( "Test3: ", getColorValue( flashVarColor3 ) ); // 16711935