0

Is this possible ? If yes how ?

Otherwise what's the alternatives ?

By dynamically I mean using

    loader = new Loader();                      
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
    loader.load(new URLRequest("MySWF.swf"));   
3
  • Can you elaborate on your question a bit? Clarify your meaning of "dynamically". Are you talking about a factory method, or are you speaking of something else entirely? Commented Apr 11, 2011 at 16:50
  • see my update I mean using Loader Commented Apr 11, 2011 at 16:58
  • Ah. Are you specifically trying to load a SWF and set its parameters? A SWF isn't a class object, so it shouldn't have a "constructor" per se. Commented Apr 11, 2011 at 17:13

2 Answers 2

2

A SWF is not a class by itself, more like a collection of classes and other things (like images or audio bytes) all archived and ready to use. You can't have a constructor for a SWF. However, what you can do, is loading a SWF and then, after the loading is complete, you can instantiate a class from that SWF and pass whatever arguments you want to it's constructor.

Also, it's possible to send parameters to the SWF and then process them as flashvars inside the swf, but that's no constructor of course :)

loader.load(new URLRequest("MySWF.swf?day=tue&week=3"));

And then you can get them like this:

var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
trace(paramObj.day);
trace(paramObj.week);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks how "instantiate a class from that SWF and pass whatever arguments you want to it's constructor"
well, let's say you have a 'Train' class in that SWF, after you load the swf that class becomes available like any other class so you can simply do new Train(power) where power it's the variable you need to pass to Train constructor. However, if you need to pass variables for the entire SWF, I guess the second option would be better.
but what is Train is the code behind a fla. If I create a new instance of Train while passing color OF BACKGROUND for example, will the swf BACKGROUND reflect that color ?
Train it's a class in that SWF, like any other class, after you instantiate it , you can use it (for example, add it to the child list). However, if you want to change the color of a instance inside the SWF (like background instance for example) you simply: loader.content.background.setColor(0xCCCCCC); Where loader it's your Loader class, and background is a class with a custom method, setColor, implemented by yourself.
1

If you have a document class in your SWF, then rather than using parameters in the constructor create a public init method with your parameters, or even use getters and setters:

import flash.display.MovieClip;

public class MySWF extends MovieClip
{
    private var _var1   :String;
    private var _var2   :int;

    public function MySWF():void
    {

    }

    public function init(var1:String):void
    {
        _var1 = var1;
    }

    public function get var2():int
    {
        return _var2;
    }
    public function set var2(value:int):void
    {
        _var2 = value;
    }
}

Then you can call these after you have loaded your swf like this:

private function finishLoading(event:Event):void
{
    var mySWF:MySWF = event.target.content as MySWF;
    addChild(mySWF);
    mySWF.init("This is a string");
    mySWF.var2 = 5;
{

1 Comment

Thanks if not possible with constructor I'll do that but I prefer constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.