6 Replies Latest reply on Feb 5, 2008 1:29 PM by spudee

    Set X,Y Position for loaded SWF file

      I'm sure this question has been asked before, but I have been reading the forum and get seem to find the answer. (Yes I looked at the AS2 dictionary before I posted.) I'm trying to load an external SWF into my stage. I have entered X,Y, coordinates, but the external SWF loads at 0,0 I have attached the code I was trying. Any help would be appreciated. This code is on the button, should it be on the timeline? Thanks!
        • 1. Re: Set X,Y Position for loaded SWF file
          Rothrock Level 5
          In somewhat reverse order.

          Yes your code should be on the timeline. Not because it won't work using on(event) handlers, but because that is old style that is more difficult to maintain and it will be going away. See this excellent tutorial about the issue:


          Next, you must not have read the help files too closely! :) You have mixed two different techniques here. You are using loadMovie which is used to load content into a MovieClip, but then instead of giving an instance of a movieclip you are giving a level number. It will work, but it is sloppy. So if you are going to use a level number you should use loadMovieNum(). (And additionally, unloadMovieNum()!)

          The next issue would be that you are trying to change the _x and _y of a level. This can be done, but unlike an empty movie clip which (even without content) exists, a _level doesn't exist until content has been loaded into it. since it will take photos.swf at least a few milliseconds (and most likely much longer) to load, _level2 won't exist until that is done. So the executing the next lines say, "Change level 2 _x to...oh, never mind there is no such thing as level 2."

          Since there is no _level class with attendant methods, properties and events, I generally try to avoid them. You really don't have much control over them and not having any tools to manipulate them I find I get stuck a lot. The MovieClip class on the otherhand has a lot of things you can do with it.

          You could set up an interval or an enterFrame to keep checking for level2 and once it exists change the x and y, but that would just be through good money after bad -- so to speak.

          SO HERE is what I would advise you to do:

          Move this off the clip and onto the timeline.
          Don't use _levels instead use createEmptyMovieClip.
          Don't use loadMovie. Use MovieClipLoader class and its nifty events.

          If you decide you want to modernize your code, post back and we can get you through it!
          • 2. Re: Set X,Y Position for loaded SWF file
            spudee Level 1
            I certainly appreciate your reply and I guess I certainly need to update my coding. (I'm sure it's obvious I don't do a lot of it) I knew I got off track somewhere. In one of my original configurations I had a empty movie clip on the stage with an instance name, and I was using loadMovieNum. Anyway, I looked at the link you sent. I am going to take another look at the MovieClipLoader class, when I was reading before, I guess this gives me more control of the SWF that is being loaded. I'll give it another shot and post back. Thanks again!
            • 3. Re: Set X,Y Position for loaded SWF file
              spudee Level 1

              Just wanted to thank you for pointing me in the right direction! I have everything on the time line AND working! Here is what I came up with:

              volunteerDonate.onRelease = function() {
              var container:MovieClip = createEmptyMovieClip("roscoe", 1);
              setProperty("roscoe", _x, "5");
              setProperty("roscoe", _y, "6");
              var mcLoader:MovieClipLoader = new MovieClipLoader();
              function onLoadInit(mc:MovieClip) {
              trace("onLoadInit: " + mc);

              Since I'm so behind the times can you give it the once over to see if it should be written better? Again, thanks a lot!!!!
              • 4. Re: Set X,Y Position for loaded SWF file
                Rothrock Level 5
                Glad you took that in the good spirit it was offered. After I posted it I was worried that I sounded too mean. Your new code is a big improvment IMHO! I've made a few changes and attached that and I'll go through them with ya.

                The first cool thing you did and you might not even be aware of all of it is that you were using the return type of createEmptyMovieClip. Whenever you checkout something in the help files, notice the return type. That tells you that cEMC returns a reference to the newly created clip. If you capture it and assign it to a variable you can then use that variable as a "short cut" of sorts. So even though you named the new clip roscoe, I'm refering to it as container. Not such a big deal for this example, but in the future you might attach a bunch of clips with names/paths like:


                So remember that trick. I've also added a this to the cEMC. It is a good habit to always prefix methods with an instance of the class they belong too. For example there might be times when the scope isn't clear and your clip will not get attached where you think. Of course using this inside a function is one of those times! So sometimes I would create a variable called home (outside of any functions) which refered to my main timeline. Then I could use home.createEmptyMovieClip() inside pretty much any function or handler and not worry about where my clip was being created.

                Next, setProperty is also the old style. I find it much easier just to set the property with the assignment operator (that would be the equal sign) and the dot. So:



                someObject.someproperty = somevalue;

                I think most people find that easier to read.

                I'm not sure, but generally I don't think I would create my MCL instance inside the onRelease handler. No real reason I could give for that, except perhaps if you have several buttons and each one will load a different clip. In that case they could all use the same loader and you wouldn't need to repeat the code over an over. Hey! that seems to be a pretty good reason to me.

                Finally. The way you handled the onLoadInit will work, but for some reason it isn't the prefered way. It has never really been clear to me why, but they prefer to have you create an object, add your event handlers to the object, and then add the object as a listener. I've shown you how here.

                BTW, if you hadn't figured it out, the onLoadComplete is pretty useless. Several of the properties, like _width and _height are not yet available at that event, so you picked the right one (onLoadInit) if you want to scale the content after it loaded.

                Good luck and let me know if you need to figure out something else.
                • 5. Re: Set X,Y Position for loaded SWF file
                  Rothrock Level 5
                  I had the code open in another window so I could refer to it as I typed my answers and forgot to attach it.
                  • 6. Set X,Y Position for loaded SWF file
                    spudee Level 1
                    First of all I didn't take anything you said as being mean. I was asking for help and your direction really helped me start to think through it. I figured the setProperty was not going to an up to date way of doing things. I appreciate the review of what I have done so far. It's just like everything else, if you want to learn (or relearn) anything, you really just have to jump right in, make your mistakes and hopefully learn something in the process. You have given me a lot to chew on. I'll definitely post again if I need to as I make my way through.