A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: Transferring variables across Swfs?

  1. #1
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167

    Transferring variables across Swfs?

    The sudden thought occured to me because thats exactly what Im going to need to do for my game - transfer the score and lives across different swfs

    Why use multiple swf's then you might ask?, because of the fact that one level is nearly 2MB big, so I thought it would be better to use a different swf for each level

    Anyways, can someone tell me how to transfer my variables lives and score to another swf?

    Many thanks

  2. #2
    Registered User Ask The Geezer's Avatar
    Join Date
    Jul 2002
    Location
    Out In The Pasture
    Posts
    20,488
    You mean two swf's but one loaded into another, or two swf's on one html page?

  3. #3
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Hey,

    Well this is for a game so the two swf's will be seperate levels. It will go on the internet though so Im not sure which of the two would be the one Im doing?

    I guess it would fall under the first one


    I have one swf and when you complete the level it loads the second one but also gives the second swf two variables : lives and score

  4. #4
    Registered User Ask The Geezer's Avatar
    Join Date
    Jul 2002
    Location
    Out In The Pasture
    Posts
    20,488
    Oh, well, that's easy. You have to think of it like this; any swf loaded into another, becomes as much a part of the main movie as if it were made there. In fact, when it is loaded, level or empty clip, it's just exactly like a movie clip object in the main movie.

    so to store variables, always name them _root.something. That way, they are stored in the root movie memory, and can be called into use by and when any other swf is loaded into it. so they would be;

    _root.lives = and _root.score =.... Those can then be accessed in any movie loaded into the main movie or by the main movie just using the same names.

    Using _root is much better than using _global, more dependable. I've had trouble using more than 2 global variables in the past, where you can name as many as you want with _root, and they always work.
    Last edited by Ask The Geezer; 11-25-2005 at 09:11 PM.

  5. #5
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Hey thanks alot

    Speaking of using _global, I was also having trouble with that too. Had a variable _global.podCount; , but I would never change when you wanted it to , some messed up

    Ill try using _root instead , I never knew you could name variables lik that


    As for the swf's, sounds exactly what I need , Ill try it out

    Thanks alot

  6. #6
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Hey sorry to be a bother but I tested it and it didnt seem to work , heres what I did:

    Swf Number 1
    - Put an input text box with the instance name 'input'
    - Places a button
    - On the actions layer I placed the variable _root.test;

    - When the user types something in the input box and clicks the button,
    _root.test = input.text; and it also _root.loadMovie("anotherswf.swf");

    Swf Number 2
    - Simply has a dynamic text box with the instance name 'answer'
    - In the actions layer I have the code: answer.text = _root.test;


    It comes out undefined , so I know Im missing something. Could you please help?

    Thanks

  7. #7
    Registered User Ask The Geezer's Avatar
    Join Date
    Jul 2002
    Location
    Out In The Pasture
    Posts
    20,488
    this.onEnterFrame = function(){
    answer.text = _root.test;
    trace(_root.test);
    }

  8. #8
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Hey,

    Still doesnt work , and I know its something small. Anyways, heres the two really small and simple .fla's

    If you have time, please take a look

    Thanks
    Last edited by Osteel; 05-01-2009 at 02:39 AM.

  9. #9
    Registered User Ask The Geezer's Avatar
    Join Date
    Jul 2002
    Location
    Out In The Pasture
    Posts
    20,488
    Well, I know it does work, since I made my examples before I posted the answer.
    Attached Files Attached Files

  10. #10
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Oh I see what I was doing wrong. I was loading the new swf movie from _root. instead of a movieclip.

    But what if I wanted to load a completly new level. By using a blank movieclip, would it 'overwrite' the first level in that you will actually completly see the second?

    Because in your example, the text box appeared with what I typed in it as well as showing the original swf combined

    Thanks

  11. #11
    Registered User Ask The Geezer's Avatar
    Join Date
    Jul 2002
    Location
    Out In The Pasture
    Posts
    20,488
    Well, I didn't waste any time on this, but make the answer movie stage samller, no bigger than the text box. Then place the empty clip in the position you want to load it. Once that answer movie is loaded, you can type in a new word, hit the button again and it will change the answer text, or load another answer1 movie into the same clip for a different answer. It will overwrite the one already loaded.

  12. #12
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Oh ok,

    Well I'll mess around with it and see if it works for my needs, thanks for the help

  13. #13
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Hey,

    Ok so I was playing around with this. I was able to load a second swf into the movieclip 'holder' and it would display the whole thing.

    But I have a few questions:

    1) Is there a way to make it so that the loaded movieclip is centered on the screen so that you dont have to keep moving it around the stage until you find it?

    2) Would transferring variables work with loading the other swf from the _root. because I tried and it didnt but that might be because I did it wrong

    Thanks

  14. #14
    Registered User Ask The Geezer's Avatar
    Join Date
    Jul 2002
    Location
    Out In The Pasture
    Posts
    20,488
    OK, you lost me there.

  15. #15
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Haha sorry ,

    Ok I suppose Ill start by explaining what exactly is happening. Its a game. To reduce load times and such , we decided to make each level a different swf instead of different scenes since each level by itself is over 1MB

    But as you know with games, there is scores and lives and so we need to transfer these two variables to the next levels to keep consistancy


    The thing is with the example you sent me yesterday is that you can still see the first swf when loading the second into the blank movieclip.


    Im asking is it possible to hide the first one only showing the second one (sort of being on top or overwriting)

    Secondly, is it possible to use the _root.loadMovie("url"); instead of using a _root.blankMovieclip.loadMovie("url"); and still be able to share _root.variables?


    Hope that makes sense , thanks

  16. #16
    Registered User Ask The Geezer's Avatar
    Join Date
    Jul 2002
    Location
    Out In The Pasture
    Posts
    20,488
    I think if you load into root, your going to lose the variables, as your replacing the root movie.

    You might work around it by starting with a root movie that doesn't have much of anything in it, then load the first movie into a level, say level 1. Then load the next one into level 1, replacing it. Or it would work for an empty clip too. That way the variable would remain in root.

  17. #17
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Oh I see what your saying:

    - Have a starting swf with nothing in it except for the two variables that I need. It then loads 'level1' into a blank movieclip

    - When level1 is competed, it loads level2 into the SAME movieclip previously containing the first level, so it would overwrite


    Great, Ill give it a try

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center