19 Replies Latest reply on Jul 25, 2007 4:18 PM by kglad

    using loadmovienum + calling function

    nik c
      Hi,

      I would like to load a new swf into level 2 (previously empty) and then - on the next line of the calling function - call a function in the newly loaded swf on level 2. Is this possible?

      It seems to me that I should use an .onload event, or similar to determine if the new swf on level 2 has finished loading and the scripts in it are available. But what would I use, how would I determine that?

      My script currently:

      *********************
      loadMovieNum("data/games/games.swf", 2);
      tBGcolor = gamesArr[clickedUnit][2];
      tGamePath = gamesArr[clickedUnit][1];
      _level2.playGame(tBGcolor, tGamePath);
      *********************

      Many thanks,

      Nik
        • 1. Re: using loadmovienum + calling function
          kglad Adobe Community Professional & MVP
          you must wait until loading is complete. if you use loadMovieNum(), you must use preloader code. if you use the moviecliploader class, you can use the mcl listener's onLoadInit() method.
          • 2. Re: using loadmovienum + calling function
            nik c Level 1
            thanks, what is preloader code?

            Is this because I am loading into a different level? Is there a way of using the moviecliploader and loading the swf into a different level than the swf from which the call is made?

            Can I modify this line so it creates a container on level 2, instead of creating one in the same level as the scritp?

            thanks,
            Nik
            • 3. Re: using loadmovienum + calling function
              kglad Adobe Community Professional & MVP
              preloader code is code that compares the target movieclip's (or _level's) getBytesLoaded() and getBytesTotal() to determine when the external swf has completed loading and is ready to respond to actionscript.

              it doesn't matter whether you are loading into a movieclip or a _level. what matters is that you are using loadMovie() or loadMovieNum() instead of the moviecliploader class.

              yes, you can use the moviecliploader class. use it's loadClip() method to load into _level2 and its listener's onLoadInit() method to determine when the loaded swf is ready to respond to actionscript.

              you can't create a container in _level2 until something exists in _level2. so, that's probably not what you want though it can be made to work if there was a need.
              • 4. Re: using loadmovienum + calling function
                nik c Level 1
                thanks a lot!

                Am I right then that the following code should work and open the games.swf on level and then execute the playGame function in the games.swf? It does not seem to work. I suspect I have to specify the target level differently?

                *****************
                var mcLoader:MovieClipLoader = new MovieClipLoader();
                mcLoader.addListener(this);
                mcLoader.loadClip("data/games/games.swf", _level2);

                function onLoadInit(mc:MovieClip) {
                tBGcolor = gamesArr[0][2]; // back ground colour value
                tGamePath = gamesArr[0][1]; // path to specific game to be opened
                playGame(tBGcolor, tGamePath); // function that sits on first (and only) frame of games.swf
                }
                *****************

                Thanks,
                Nik
                • 5. Re: using loadmovienum + calling function
                  kglad Adobe Community Professional & MVP
                  not quite:

                  • 6. Re: using loadmovienum + calling function
                    nik c Level 1
                    Thank you - that is working very well. I don't know why the loadClip line did not work before when I tried to load the clip into the level!?

                    I have tied everything to a PLAY button now in the way shown in the code below.

                    But one small problem remains: the background colour code, meant to change the colour of a movie clip at the background of the stage, does not change. This works fine when I access the games directly though. I have included the playGame script as well. It is in the swf that is loaded into level 2.

                    Thanks again, you truly know what you are talking about!

                    Nik


                    • 7. Re: using loadmovienum + calling function
                      kglad Adobe Community Professional & MVP
                      are tBGcolor and tGamePath defined and what you expect in your onPress handler? you can use a trace() function to check. and you may as well check to see they are passed to playGame(), too.
                      • 8. using loadmovienum + calling function
                        nik c Level 1
                        Hi, yes, tBGcolor and tGamePath are defined and deliver the right values to playGame() .

                        Is there a way to further target the 'games_bg_mc' ? Could it be a path issue to the movieclip?

                        Should I use the flash.geom.ColorTransform instead? I just read in the help files that the color constructor has been deprecated?

                        Thanks,

                        Nik
                        • 9. Re: using loadmovienum + calling function
                          kglad Adobe Community Professional & MVP
                          the path to games_bg_mc shouldn't be a problem if by, "This works fine when I access the games directly though." you mean the color changes when you call playGame from within your loaded swf. is that what you meant?

                          the setRGB() method works for as2 so that's not causing the problem. you'll have greater control of the color by using the color transform, but it won't solve your problem.
                          • 10. Re: using loadmovienum + calling function
                            nik c Level 1
                            No, it won't solve my problem. Ithink there is something else not quite right with the assets I am loading in, or the path from the shell swf to the final games, I'll have to investigate ....

                            Thanks,
                            Nik
                            • 11. Re: using loadmovienum + calling function
                              kglad Adobe Community Professional & MVP
                              you're welcome.
                              • 12. Re: using loadmovienum + calling function
                                nik c Level 1
                                Hi,

                                I have sorted most of the issues out now. It boild down to the 'lo.onLoadInit' actually not executing AFTER the first frames scripts, but before them. [for reference from the flash help page: The MovieClipLoader.onLoadInit handler is invoked after the actions in the first frame of the clip have executed, so you can begin manipulating the loaded clip. --> is this wrong then?]

                                This meant that the code on this script which sets up the games section ready with game one selected, always executed AFTER the playGame I wrote to set up the screen with the game, background colour,e tc I needed for the game.

                                In essence the way this worked was that I sent all the right stuff along to playGame, it was set up correctly, then the finctions on the page executed and undid everything.

                                Is there a way of getting my playGame to execute last? I have moved the script all the way down on the page, hoping it will have an effect on the order in which scripts are executed, but this didn't work.

                                Thanks.

                                NIk
                                • 13. Re: using loadmovienum + calling function
                                  kglad Adobe Community Professional & MVP
                                  frame 1 code in your external swf executes first, then the onLoadInit() method executes.
                                  • 14. Re: using loadmovienum + calling function
                                    nik c Level 1
                                    OK, I think I know what is causing this current problem of mine. The external swf executes some XML loading code, that in itself has some stuff happening on the XMLs .onload (success).

                                    So this may cause the code that depends on the successfull loading of the XMLs (there are 2) to execute after the 'normal' code of that first frame has executed and also after the 'lo.onLoadInit' has executed.

                                    Is there a way of making the 'lo.onLoadInit' dependant on the successful loaing and execution of the XMLs? I got 2 scripts dependant on the XMLs (one constructing a menu based on the XML, the second one for configuring it)?

                                    Thanks,
                                    Nik
                                    • 15. Re: using loadmovienum + calling function
                                      kglad Adobe Community Professional & MVP
                                      there are several ways you could handle this.

                                      you could just have the external swf call its own function when the data needed by that function are ready.

                                      or you could use a watch() function in your _level0 swf that checks for a variable in your _level2 swf to change value indicating everything is ready to go for calling that _level2 swf.

                                      or you could use a loop like onEnterFrame or setInterval to repeatedly check if the _level2 function is ready to use.
                                      • 16. Re: using loadmovienum + calling function
                                        nik c Level 1
                                        thanks - I thought of something along the lines of the watch(), but wasn't sure how to go about it in Flash (yet, -- kind of a steep learning curve on some things coming from Director .. ;)

                                        nik-c

                                        • 17. Re: using loadmovienum + calling function
                                          kglad Adobe Community Professional & MVP
                                          you can use something like:

                                          • 18. Re: using loadmovienum + calling function
                                            nik c Level 1
                                            you are very good! I'd like to be able to write such short code that does that much!
                                            It all works now and I have learnt a lot.
                                            thanks, kglad

                                            nik
                                            • 19. Re: using loadmovienum + calling function
                                              kglad Adobe Community Professional & MVP
                                              you're welcome.