문제

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()" />
도움이 되었습니까?

해결책 2

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:

public function changeParams():void {
      if (idAnimation.content != null) {
        idAnimation.content["Type"] = myString;
        idAnimation.content["Num1"] = myNum1;
        idAnimation.content["Num2"] = myNum2;
        MovieClip(idAnimation.content).gotoAndPlay(1);
        trace ("Type= " + idAnimation.content["Type"]);
        trace ("Num1= " + idAnimation.content["Num1"]);
        trace ("Num2= " + idAnimation.content["Num2"]);
      }
    }
]]>
</mx:Script>

<mx:SWFLoader id="idAnimation" source="assets/animation.swf" complete="changeParams()" />

다른 팁

If idAnimation is the instance name of the movieclip what was embedded in the beginning, then it also can be a good method, that you create a function in your swf:

function setParamsAndAutoplay(value1:*, value2:*, vlaue3:*):void
{
     Num2 = value1;
     Num1 = value2;
     Type = value3;

     playMe();
}

This way for sure you set the parameters and started the animation at the same time. You can also put stop(); in front of every code in the swf for stopping the play.

So you would add this to your code, before your playMe() function:

stop();

function setParamsAndAutoplay(value1:*, value2:*, vlaue3:*):void
{
     Num2 = value1;
     Num1 = value2;
     Type = value3;

     playMe();
}

Would this solve the issue?

EDIT

Also you can try to change init="changeParams()" to complete="changeParams();".

EDIT 2

Or try this to load the swf into your flex. This won't embed it, if your intention was that, but you can check if the function works fine. And maybe this can solve the problem.

var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener("complete", ldrDone);
ldr.load(new URLRequest("your-swf-file"));

function ldrDone(evt:*):void
{
    var movie:MovieClip = MovieClip(evt.target.content);
    movie.setParams(0, 1, 2);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top