Calling functions / set variables in loaded SWF

You know how you can create a Movieclip object, add a function or a variable, and then call this function or change this variable to the SWF file that includes? I want to be able to do it, only with SWFs that are loaded at run time.

My "main" SWF is just something the type of menu. The user clicks a button in my HAND. SWF and the HAND. SWF and then load the SWF associated with this button, using the AS 3.0 method Loader () - URLRequest () - addChild () that everyone always asks questions on prescribed.

It works very well. But now I have a few custom and other objects (ie., not only the channels) in my HAND. SWF I want power sharing with the loaded SWF files. preferably, I'd use a function of the type of accessor (ie., LoadedSWF.GiveMeTheCoolObject (MyCoolObject)); Although at this point, I'm willing to hack the object itself (ie., LoadedSWF.CoolObject.CoolField = MyCoolObject.CoolField ;) if that's what it takes.

I tried to call the functions and setting the fields, but the AS 3.0 compiler doesn't care for this kind of nonsense on its objects defined by the generically. Also note that, at runtime, a call to LoadedSWF.hasOwnObject ("GiveMeTheCoolObject") returns true.

So my colleagues big brain, what is the magic?

In advance, thank you for the help!
Scott

Kglad,

Quote:
Posted by: kglad
the error message, seems, your function expects a member of the class MyCoolObject, correct?
and MyCoolObject is not an instance of this class. so, you have to fix that. That is to say, create a member of this class, and use this class as a parameter of the function member.

Heh, that's what the error message says all right. But the object IS actually an object of class MyCoolObject. I looked at this thing for hours and MyCoolObject IS an instance of this class. I tried different combinations of casting (including the cast on an object and passing that) but had no luck whatsoever.

So I googled this particular runtime error message ("Error #1034: has no Type constraint: cannot convert '" ") and found a ton of links to other people with a similar problem. As a general rule, the problem was just as you say, someone passes a button as a string, that kinda thing. But a guy had a problem with an object that it has derived from the event class. Once the hours spent trying to make it work, he has tried to override clone() of the base class method and got it to work:

http://www.assertTrue.com/articles/2006/10/14/custom-events-in-ActionScript-3-0

As for my project, I took a long, hard look at the overall architecture of my project and decided it would be better if all the 'real work' is held in the HAND. SWF file. This approach would solve some other problems, and it makes sense in a mindset of "separating the display of data.

If (and this is the interesting part) I changed the API for the LOADED. SWF to accept the pairs of strings (ie., LoadedSWF.AddToDisplay (String Name, String Data)) rather than accept MyCoolObj. Ran and Viola, now we work without any throw exceptions in. After this moment of glory, I created a couple of other APIs that comes from base classes (a type int, the other a type function) agreed and was happily on my way.

On this basis, I understand while passing another base class objects (IE, the derived class) to load MovieClips via custom function API is... perhaps not so simple you'd expect. In the grand scheme of things well, not a big deal: as far as I'm concerned AS3 rocks.

Kglad, thanks again for all the help: your comments on how this was the big push that I needed to get through the last barrier of this project.

For those who read this topic for the ' how to call functions or set variables in a loaded SWF file?

The short answer is cast to the SWF file that is loaded as a MovieClip content object and then call the function or change the variable:

Load the SWF file (you saw this code a million times)
var LoadedSWFObj:Loader = new Loader();
var MyURL:URLRequest = new URLRequest("LoadThisSWF.SWF");
LoadedSWFObj.load (MyURL);

It is within your Event.COMPLETE Manager:

Cast as a MovieClip object and work with it.
var LoadedSWF:MovieClip = LoadedSWFObj.content as MovieClip;

or another way to perform a cast:
var LoadedSWF:MovieClip = MovieClip (LoadedSWFObj.content);

Call yer function:
LoadedSWF.CallMe ();

Tags: Adobe Animate

Similar Questions

  • How to call the function lavel root & variable external loaded swf file

    I have little problem in as3.  I load 'mainmenu.swf' file "main.swf". through class loader. so now "main.swf" is children of parents 'mainmenu.swf' file how can call "main.swf" variable and function of "mainmenu.swf".

    The parent of the loaded swf file is the charger.  The main SWF is the parent of the charger.  Then to communicate with the main storyline of the loaded file can use:

    MovieClip (parent.parent) .someFunction ();

  • call the parent of a loaded swf file functions?

    Hi all

    I have a main Flash file that load other SWF files using loader objects.  I want to be able to call the methods of this main purpose since then in the loaded SWF files.  For example, I want the file loaded in order to add an event listener on the stage of the main file.  I found this example:

    http://flexcomps.WordPress.com/2008/09/01/AS3-calling-MyFunction-on-main-timeline-from-Loa ded - swf.

    That seems to do what I want, but I can't understand how to implement it in my setup.

    My "main" file that loads other sovereign funds is called "main.swf" and has a main document class.

    My loaded file is called start.swf and has a class of documents 'Start '.

    in my main constructor:

    var ethics_content:Loader = new Loader();

    ethics_content. Load (new URLRequest ('start.swf'));

    in the constructor of my departure:


    If (this.parent.parent! = null) {/ / IF the STATEMENT FAILS}
    var parentObj:Object = this.parent.parent as an object;
    trace ("parentObj has been found.'");
    } else {}
    trace ("this .parent .parent is null.");
    }

    stage.addEventListener (MouseEvent.CLICK, handle_click); GENERATES AN ERROR WHEN IT IS LOADED TO THE HAND. SWF

    trace ("step: ' + Stadium");  RETURNS A [STAGE] OBJECT BY OPERATING BY ITSELF, BUT NULL WHEN IT IS EXECUTED IN THE HAND. SWF
    trace ("root:" + root); RETURNS AN OBJECT [START] WHEN RUN BY ITSELF OR WITHIN THE HAND. SWF
    trace ("parent: ' + parent"); RETURNS A [STAGE] OBJECT BY OPERATING BY ITSELF, BUT NOTHING OF IN THE HAND. SWF
    trace ("this .parent:" + this.parent); RETURNS OBJECT STAGE ITSELF, NOTHING OF IN THE HAND. SWF
    trace ("root.parent:" + root.parent); RETURNS THE SCENE OBJECT BY ITSELF

    How to get to the scene of a loaded SWF file?  Or at least, how can I call a function in my main class from the loaded file start.swf so I can move the functionality I need the main.swf file?

    Any help is greatly appreciated.

    If your main swf loader is not added to the display list when it try {branch} is running, the charger will not have a parent.

  • Has no type constraint-access variables of loaded swf

    Hey,.

    I have a problem accessing a variable loaded from another swf. In fact, the variable is a class singeltone, which must be initialized. It has the structure presented in the last part of the message.

    The project has the following structure:
    -main.swf - this all the other swf files and holds their load.
    -assets.swf - contains common elements and initializes the SingleTone class
    -interface.swf - contains some elements of the interface and must use the SingleTone class
    -all SWFs three have access to a common class structure, where the SingleTone class is defined.

    The problem I have now, accesses the SingleTone class in the interface.swf file. If I try to send the SingleTone.singleTone.property of the class parameter I get "type coercion failed: cannot convert ClassName @..» to ClassName. If I try to access the SingleTone class directly in the interface.swf file, I get "cannot access a property or an a null object reference...". ». The two errors I have seem logical:
    -If I send a parameter, I send the reference to this object, but the interface.swf file does not have access to the field of assets.swf.
    -If I try to access the SingleTone class directly in the interface.swf file, I get error null because the class is not initialized on the interface.swf domain.

    Are there close to work? I know that a simple solution may be to initialize the variable in the area of the interface.swf, but what if there is a variable that can be instantiated in the assets.swf? I can access this variable to another swf, or if I can change the ApplicationDomain?

    I found the problem. In the main.swf file, when I loaded the other 2 files, I didn't specify the LoaderContext. So now, I changed the context loader so that all files will be responsible for their class definitions in the same field. Here is a small example from senocular and the link.

  • Calling function flex SWF loaded into Flex

    Hi, just a few days in flex development and a question about SWFLoader.

    Is it possible to call a function from flex to a SWF file loaded by SWFLoader control?

    For example, I have a SWF animation with a button, loaded into a Flex application and I would like that when the user clicks the button, it will call a function in the flex application.

    If possible, can we send parameters with the function call?

    Thank you for all your help.
    Mika

    There are two possibilities, depending on what type of SWF you load. If you load a SWF9, then check on ApplicationDomain. If you load SWF8 or earlier, then read this technical note:
    http://www.Adobe.com/go/749eaa47

  • Calls to functions from loaded SWF files

    I have a Flex application that uses the SWFLoader object to load an external SWF file (which has also been developed in Flex) in a section of the page. The parent SWF file defines several variables and global functions in ActionScript I would use from all SWF files loaded in the SWFLoader. Is it possible in ActionScript to access the functions and variables that are defined in the SWF file the SWF load/child parent? The two SWF files are on the same domain, so there should not be problems of security.

    Peter thank you very much for your answer. I have tried loading in the same ApplicationDomain, but he did not what I expected from him. I, however, found the solution to my problem. I needed to understand how cross-script from a SWF file loaded the loaded SWF file. I discovered how and published a description of my conclusions on another thread that was no longer relevant to my question. The described solution has allowed me to do what I needed without touching the ApplicationDomain.

  • call functions in SWFs in a tilelist?

    I used the code below to load SWF files into my tileList and set their colors using setData() in each swf. Everything seems to work except "source: e.currentTarget.content". Sovereign wealth funds seem to be mixed at random whenever it runs. Their colors are properly defined and all the other info tilelist is correct. What did I do wrong with "source: e.currentTarget.content"? I can exchange "source: e.currentTarget.content" with "source: myXMLList [loadPosition] .source" gives me the correct swf but then the colors are not defined.

    for (var f: int = 0; f < myXMLList.length (); f ++) {}
    var ldr:Loader = new Loader();
    ldr.contentLoaderInfo.addEventListener (Event.COMPLETE,
    {function(e:Event):void}
    Object (e.currentTarget.content) .setData (myXMLList [loadPosition] .parameter [0], myXMLList [loadPosition] .parameter [1]);
    myTileList2.addItem ({label: myXMLList [loadPosition] .label path .path: myXMLList [loadPosition], source: e.currentTarget.content, cost: myXMLList [loadPosition] .cost, category:myXMLList[loadPosition].@category});})
    loadPosition ++;
    });
    var urlReq:URLRequest = new URLRequest (myXMLList [f] .source);
    LDR. Load (urlReq);
    }

    Oh, I've been letting you know how to get the values of the correct settings, i.e. the values of the parameters for the corresponding Chargers. These parameter values would be in the call "setData" as you mentioned in the first post.

  • Unable to access MovieClips and functions in a loaded SWF

    Hello world

    I'm just trying access anything whatsoever within the loaded SWF and all I get is 'null '.

    I have the code in the SWF parent who must tell the SWF movieclip child what to do, but nothing works. It's a piee of cake into AS2, and I can't seem to get anything of crossover in AS3.

    Here is my code:

    import com.greensock.TweenMax;
    import flash.display.MovieClip;
    
    var myLoader:Loader = new Loader();
    var url:URLRequest = new URLRequest("SWCS_S3_500x250_exp_panel_2.swf"); // in this case both SWFs are in the same folder 
    myLoader.load(url);  // load the SWF file
    panel2.addChild(myLoader);   // add that instance to the display list, adding it to the Stage at 0,0
      
    panel2 = myLoader.content as MovieClip;
    
    photo_about.alpha = 0;
    photo_downloads.alpha = 0;
    
    //panel2.content_about.alpha = 0;
    //panel2.content_downloads.alpha = 0;
    
    function closeSection():void
    {
              panel2.controlsMC.forcePause();
    
              TweenMax.to(photo_about, .5, {alpha:0});
              TweenMax.to(photo_downloads, .5, {alpha:0});
              TweenMax.to(photo_home, .5, {alpha:0});
      
              TweenMax.to(panel2.content_about, .5, {alpha:0});
              TweenMax.to(panel2.content_downloads, .5, {alpha:0});
      
              TweenMax.to(panel2.controlsMC, .5, {autoAlpha:0});
              TweenMax.to(panel2.content_home, .5, {alpha:0});
    }
    
    

    Panel2 traces null and all clips loaded in draw property undefined errors.

    Can someone please tell me what I'm doing wrong? I would be happy to see what Miss me and feel like this should be a piece of cake.

    Thanks for any help!

    You do not add the content of the scene to 0.0, you add it to panel2 to 0,0.  Then you take panel2 and assign to something that probably does not exist at the time wherever you set it.  You must wait until the loader load before trying to do something with its content, otherwise it has no content (null).  Similarly, you can not control anything in the loaded swf file, until it is fully charged.  So to assign an event listener for the property contentLoaderInfo the loader to determine when loading is complete, and have the event handler works deal from interacting with it.

    var myLoader:Loader = new Loader();

    var url: URLRequest = new URLRequest ("SWCS_S3_500x250_exp_panel_2.swf");

    myLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, processLoadedSWF);

    myLoader.load (url);

    function processLoadedSWF(evt:Event):void {}

    deal with the swf loaded here

    but you do not want to be assigned to the panel2 object that contains the charger

    }

    With regard to the control of anything anyone inside of the swf you are loading, is an AS1/2 or an AS3 swf?

  • How to pass a variable to a swf file in another using the Loader?

    I'm loading a swf file from another swf using the method as follows:

    var myLoader:Loader = new Loader();

    addChild (myLoader);

    var url: URLRequest = new URLRequest ("myExternalMovie.swf");

    myLoader.load (url);

    How to pass a variable to the first SWF in the second?

    I'm not sure that "passage" is a term there, given that this generally applies to what happens with functions that can host variables to pass as arguments in the.

    When using the charger, you can assign an event listener to determine whether the activity of loading is complete and have an event handler that targets its contentLoaderInfo property to the event.currentTarget.content, which in your case would be the loaded swf file.  At this point, you can assign a value to a variable in this swf file by targeting...

    var myLoader:Loader = new Loader();

    addChild (myLoader);

    myLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, loadComplete);

    myLoader.load (url);

    var loadedSWF:MovieClip;

    function loadComplete(evt:Event):void {}

    loadedSWF = MovieClip (evt.currentTarget.content);

    loadedSWF.someVariable = ';?

    }

    It is probably possible you may also have the loaded swf the value rtrieve by targeting to the parent swf file, which would be both parents away because of the charger is the relative of the SWF loaded... unevariable = MovieClip (parent.parent) .someValue;

  • . Loaded SWF loses functionality (Flash 8 Loader component)

    Hi all, long time listener, first - time calling...

    I have a file with a Loader and ProgBar component. The charger draws in a SWF with buttons. The buttons control the sound objects to play MP3 files. When the second swf loads, do not play the MP3. The music is incorporated, not loaded on the outside. If I call the direct URI of the swf file, the dumb thing plays! But loads the swf file loaded via the Loader component..., the display of buttons their bearings... but the sounds won't play... it fails to test the animation too...

    I am sure this is just a fundamental vice that I created (the missing brace syndrome), but I have no idea what it is, and any help would be greatly appreciated.

    Thanks Lon... Sorry I haven't decided to go back but yesterday, it was my birthday and I'm hanging with family all day...
    IM such a BONE head... it's here (referring to the above displayed code...)

    Instead of mySound = new Sound();

    mySound = new Sound (_root);

    Thank you guys for your help... two obviously that would have set the MovieClipLoader thingie too

    Peace

    Stephen McDaniel
    New Century Graphics

  • When I use the library function node call in real time, is loaded only once for all or load the DLL whenever it is called?

    When I use the library function node call in real time, is loaded only once for all or load the DLL every time when it is called?

    I have a critical application in real time, in which I use a piece of DLL function developed in C++.  It is ok?  Make sure any senior developer?

    Thank you in advance.

    The user interface thread is the thread that is used to update the user interface. It's slow. And it's supposed to be that way because humans are slow.

    The call library function node can be configured as this thread to use in the configuration for it dialog box. Please visit the LabVIEW documentation on how to do this.

  • How the get function is available or not on the loaded swf file

    I have 100 swf file in one of my projects. And there is a common feature in some of the SWFs. All these files are loadded in the file main.swf. Here's my Question, "If have the common function is the swf file that is loaded, int how to trigger this funciton of the main.swf.

    Quite simply,.

                if(movieclip.test){
                    movieclip.test();
                }
    

    ('movieclip' is the loaded SWF file and "test()" is the function)

  • loaded SWF does not work properly

    I created a maze game (the idea is to draw a line to connect between 2 video clips in the wall of the maze)

    This is the code of the game:

    Stop();

    import flash.display.Sprite;

    import flash.events.MouseEvent;

    We draw or not?

    var drawing: Boolean;

    var isStart = false;

    var drawingSprite:Sprite = new Sprite();

    addChild (drawingSprite);

    drawingSprite.graphics.lineStyle (3, 0 x 000000);

    drawing = false; to start with

    stage.addEventListener (MouseEvent.MOUSE_DOWN, startDrawing);

    stage.addEventListener (MouseEvent.MOUSE_MOVE, draw);

    stage.addEventListener (MouseEvent.MOUSE_UP, stopDrawing);

    function startDrawing(event:MouseEvent):void {}

    drawingSprite.graphics.moveTo (mouseX, mouseY);

    drawing = true;

    }

    draw function (event: MouseEvent) {}

    {if (Drawing)}

    drawingSprite.graphics.lineTo (mouseX, mouseY);

    }

    }

    function stopDrawing(event:MouseEvent) {}

    drawing = false;

    }

    Adds an event listener on the stage with the move of the mouse event.

    stage.addEventListener (MouseEvent.MOUSE_MOVE, detectHits);

    function detectHits(event:MouseEvent) {}

    If the mouse touches the edges of the labyrinth, return to the

    first scene and remove the mouse move event listener to the

    Stadium.

    If (wall_mc.hitTestPoint (mouseX, mouseY, true)) {}

    If (isStart == true) {}

    drawingSprite.graphics.clear ();

    gotoAndPlay (3);

    isStart = false;

    stage.removeEventListener (MouseEvent.MOUSE_MOVE, detectHits);

    }

    }

    If the mouse button point the brush, proceed to the third stage and

    remove the mouse event listener move scene.

    ElseIf (hit_mc.hitTestPoint (mouseX, mouseY, true)) {}

    If (isStart == true) {}

    drawingSprite.graphics.clear ();

    gotoAndPlay (2);

    isStart = false;

    stage.removeEventListener (MouseEvent.MOUSE_MOVE, detectHits);

    }

    }

    }

    mc_start.addEventListener (MouseEvent.MOUSE_OVER, fl_MouseClickHandler);

    function fl_MouseClickHandler(event:MouseEvent):void

    {

    isStart = true;

    }

    works very well and published as swf

    When I try to load this game to another fla

    the market never properly Yes the loaded swf file and the game starts well

    but when I start to draw the line in the maze and without touching any wall it moves on the chassis (lost u)

    The error is always using mouseX and mouseY.  If you had failed to compensate for the placement of the loaded swf file then you will would not compensate for mouseX/Y for one would be the same value as mouseX/Y for those responsible.

    You need to deal with the overall conversion to local or you must build in compensation.  Because it is a fixed value (35), you can set the code in the swf file to use a Compensator variable that is set to zero by default in the swf file (for when he plays his own).  When you load it into another file, then you need to wait after is fully loaded and loading file assign a different value to this trim... in this case it would be-35

    Another way to solve this problem would be to move all the content in the swf 35 pixels to the right and downwards (in itself).  Then it would be unnecessary to adjust the positioning and the mouseX/Y values would be the same for the main movie and the one responsible.  Then, the only problem might be using the SWF itself.

  • SWF swf communication - swf loaded swf loading

    Hello
    I read the article by kglad SWF SWF communication when two SWF files uses actionscript 3.0 gratefully, but, probably because of my stubbornness, I have swf loaded at loading swf communication problems to work for me.

    Loading of swf (swf1.swf) as loaded swf (swf2.swf) by casting to a MovieClip loader.content worked well for me.

    I want the swf to call a function on the timeline of the swf file loading.  (Aside, I'm doubting it's possible, but I'm really very uneducated on this topic)

    I copied the article code in swf2.fla as below;

    Create a variable to refer to the main scenario for swf1

    var swf1Main_mc:MovieClip; This assumes that swf1 is a movieclip.

    Create your listener to ensure that loaded swf has been added to the display list and can get a

    the reference to the main timeline (which is always the main timeline of the SWF loading).

    this.addEventListener (Event.ADDED_TO_STAGE, initF);

    function initF(e:Event):void {}

    swf1Main_mc = MovieClip (this.root);

    }

    and add a simple button with an event listener for mouse to invoke a function (in swf2) for example

    function btnClicked(e:Event)

    {

    swf1Main_mc.showME ();

    }

    where showME() is a function in swf1.  for example

    function showME()

    {

    trace ("swf1 called swf2 showME");

    }

    All I get is a runtime error "TypeError: Error #1006: showME is not a function."  to swf2_fla::MainTimeline/btnClicked().

    I took out what little hair remains try different variations in the code then, in desperation, please someone can spot what I'm doing wrong or point me in the right direction?

    Thanks for your time,
    Bob

    A loaded swf file should not try to control its parent.  The child must only be send an event to the parent to take action.  In this way, the child is completely independent of the parent as well as to be able to publish on his own.  To load completion than the parent must assign an event listener to the child for this event, and the event handler function.

    Example:

    Add something to raise the event in the child:

    dispatchEvent (new Event ("eventTriggered")); (if problem dispatchEvent, see: http://www.kirupa.com/forum/showthread.php?p=1899603#post1899603)

    In your SWF to loading/parent, listen for the complete event on the Loader.contentLoaderInfo.  In the complete event handler, add a listener for the event on the loaded swf file.

    triggered when external swf loading event handler
    function loaderCompleteHandler(event:Event) {}
    MovieClip (event.currentTarget.content) .addEventListener ("eventTriggered", eventHandler);
    }

    function eventHandler(event:Event):void {}
    trace ("event sent in loaded swf");
    }

  • access the property of the main class in loaded swf.

    Hello

    I have a main.fla, its document class is main.as. the code in main.as is

    package

    {

    import flash.display.MovieClip;

    SerializableAttribute public class Main extends MovieClip

    {

    private var loading: loading;

    public var trovami:Number = 10;

    public void Main()

    {

    stage.frameRate = 31;

    loadSWF();

    }

    private function loadSWF (): void

    {

    loading = new Loading (this);

    }

    }

    }

    LOADIND. ACE is another class where I load another swf name is my_swf.

    class loading code is:

    package

    {

    import flash.display.MovieClip;

    to import flash.display.SimpleButton;

    import flash.text.TextField;

    import flash.display.Loader;

    import flash.events. *;

    import flash.net.URLRequest;

    SerializableAttribute public class loading extends MovieClip

    {

    private var _fla:MovieClip;

    private var url: String;

    public var val: String;

    private var loader: Loader;

    public void Loading(fla:MovieClip)

    {

    _fla = fla.

    init();

    }

    private function init (): void

    {

    URL = 'my_swf.swf';

    var request: URLRequest = new URLRequest (url);

    loader = new Loader();

    initListeners (loader.contentLoaderInfo);

    Loader.Load (request);

    }

    private void initListeners(dispatcher:IEventDispatcher):void

    {

    dispatcher.addEventListener (Event.COMPLETE, complete);

    }

    private void completato(event:Event):void

    {

    _fla. AddChild (Loader);

    Loader.y = 10;

    Val = "Hello".

    }

    }

    }

    and the class of the loaded swf document is Main2. in my_swf.fla, there is a text, that his name is:

    a_txt.

    I so use class in Main2 for loading text property.

    Code of Main.as is

    package

    {

    import flash.display.MovieClip;

    import flash.text. *;

    Main2/public class extends MovieClip

    {

    public void Main2()

    {

    a_txt.text = val;

    }

    }

    }

    where val is the property of class loading.

    so, how do I access.

    You have a reference to your movie loaded into your class loader - instead of simply set val to string - add a method in your class Main2 - and call it loading - passing the string which him. Push not pull...

    Hand 2 becomes:

    public void Main2() {}
    a_txt. Text = Val;

    }

    public void setText(val:String):void {}
    a_txt. Text = val;

    }

    And then in your class loader - when you have finished loading only call setText:

    private void completato(e:Event):void

    {
    addChild (loader);
    Loader.y = 10;
    MovieClip (loader.content) .setText ("hello");
    Val = "Hello";
    }

Maybe you are looking for