I’ve created a Flash Animation (CS5, ActionScript 3) and converted it to SWF. The flash animation needs the values of 3 variables (defined in the swf timeline) BEFORE it starts running in my Flex application. I’ve embedded the swf file using swfloader in Flex, but I need to pass the parameters from Flex into Flash before the animation starts. How do I do this?
The way I have my flex code setup below, the variables are not being updated. I get an exception every time it gets to the changeParams function because it can’t find “Type”, “Num1”, etc.
Part of My flash code:
//These 3 variables need to be populated via Flex BEFORE the animation starts...
var Num2:int;
var Num1:int;
var Type:String;
var whichNumber:int;
var frameNumber:int;
function playMe():void {
switch (Type) {
case 'type1':
gotoAndPlay(16);
break;
case 'type2':
frameNumber = 27;
whichNumber = 1;
gotoAndPlay(frameNumber);
break;
case 'type3':
frameNumber = 29;
whichNumber = 1;
gotoAndPlay(17);
break;
case 'type4':
whichNumber = 1;
break;
}
}
My flex code:
public function changeParams():void {
idAnimation.content["Type"] = 'type1';
idAnimation.content["Num1"] = 6;
idAnimation.content["Num2"] = 30;
trace ("Type= " + idAnimation.content["Type"]);
trace ("Num1= " + idAnimation.content["Num1"]);
trace ("Num2= " + idAnimation.content["Num2"]);
}
]]>
</mx:Script>
<mx:SWFLoader id="idAnimation" source="animation.swf" init="changeParams()" />
Okay, so I found out what was wrong. As anemgyenge suggested, I changed my function to run at “complete” instead of “init”, but changeParams() never ran until I stopped trying to embed the swf to the swfloader. I guess I didn’t include that’s how I was doing it in my original post- my error. If you try to embed with @embed, it doesn’t run the “complete” event when you expect it to.
The other thing I did was to add a “gotoAndPlay(1)” so it would play the first frame after the parameters were set. As anemgyenge suggested, I added stop() to the beginning of my Flash code so it doesn’t play until Flex tells it to. I use binding setters to run changeParams() any time one of my variables from Flex changes so the parameters will update in the Flash SWF and start the movie over at frame 1. So glad to finally have this figured out! 🙂
Here’s my updated Flex code: