A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [help] variable within attached MC not readable immediately

  1. #1
    curiouser and curiouser LittleRed's Avatar
    Join Date
    Mar 2004
    Location
    Nottingham
    Posts
    335

    [help] variable within attached MC not readable immediately

    **sorry to repeat post - I'd posted this in another forum, but got no reply - and this is for my game, and was just hoping someone here might be able to shed some light on this **

    I'm attaching a movieclip (from the library) which has a variable 'v' defined within it, but when I attach that movieclip, the variable isn't immmediately available. The following returns 'undefined',

    code:
    _root.attachMovie("LibraryMovie", "NewMovie", 1000);
    trace("variable: "+NewMovie.v);



    ...and yet, if I do the trace a frame later, the variable is there and the value is returned.

    I'm getting a similar problem reading variables from externally loaded swfs.

    Is there a way to read the variable immediately? thanks for any help!

  2. #2
    Half Empty happinessSam's Avatar
    Join Date
    May 2003
    Location
    London
    Posts
    442
    No. To be absolutely sure the variable will be there you need a delay (1 frame should do it) between attaching a MC and accessing it's properties.

  3. #3
    curiouser and curiouser LittleRed's Avatar
    Join Date
    Mar 2004
    Location
    Nottingham
    Posts
    335
    thanks for the reply - do you know the reason behind this, as I want to reduce any delay to an absolute minimum when loading levels etc.?

  4. #4
    Half Empty happinessSam's Avatar
    Join Date
    May 2003
    Location
    London
    Posts
    442
    I don't know the exact reason. I just accept it as being one of those delightful little quirks that make Flash such a joy to work with.

  5. #5
    curiouser and curiouser LittleRed's Avatar
    Join Date
    Mar 2004
    Location
    Nottingham
    Posts
    335
    ah - it's one of those is it!?

    ok, thanks!

  6. #6
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    I'm not sure if this'll work, but you could try:

    _root.attachMovie("LibraryMovie", "NewMovie", 1000, {v:0});
    trace("variable: "+NewMovie.v);

    This means that when it's attached, it automatically sets the 'v' variable. See if it does anything.
    http://www.birchlabs.co.uk/
    You know you want to.

  7. #7
    the usual
    Join Date
    Jul 2000
    Posts
    1,482
    ok, the problem is that the first frame of the attached movie is not initialised, before youre call:
    Code:
    // _root frame
    var c = attachMovie("LibraryMovie", "NewMovie", 1000);
    trace(c.v);
    // LibraryMovie Frame
    var v = 900;
    trace("me.v: "+v);
    the trace output is:
    undefined
    me.v: 900

  8. #8
    curiouser and curiouser LittleRed's Avatar
    Join Date
    Mar 2004
    Location
    Nottingham
    Posts
    335
    thanks for replies.

    venegance mx - the value of the variable will depend on which MC is attached, so I need the variable defined within the MC itself if possible.

    Trickman - is there some way to get the first frame initialised without actually waiting for that frame to run through, or is there a way to store the variable in such a way that it doesn't need the first frame initialised to have the variable defined?

  9. #9
    Senior Member
    Join Date
    Jun 2000
    Posts
    896
    Hi LittleRed,

    I can tell you exactly why the variable will not be available. It has to do with the order in which frame actions are fired.

    Lets say you have an onEnterFrame event in each of the 3 movieclips in this structure:

    House_mc
    --- Kitchen_mc
    ---------- Stove_mc

    On any particular frame, the onEnterFrame event for Stove_mc is completely fired and handled first. Then that of Kitchen_mc is fired, then that of House_mc.

    So, if you are currently in Kitchen_mc firing an onEnterFrame event and use that event to attach a movieclip. Its actions will not have a chance to even be executed until the next frame.

    That may seem a little confusing at first, but it actually makes perfect sense.

    That said, there are 3 ways to get around this (one of them mentioned by vengence):

    1) Use the 4th parameter of the attachMovie() method to initialize variables.
    2) If you are insisting on using Flash 6, then look up the #initclip# stuff. Its not recommended anymore, but it will work for functions, variables, etc.
    3) If you are using Flash 7, then you can attach an AS 2.0 class to the movieclip. The constructor is fired upon creation of the movie clip and you'll be able to access all of its properties.

    #3 is not hard and is the best way to do it.

  10. #10
    curiouser and curiouser LittleRed's Avatar
    Join Date
    Mar 2004
    Location
    Nottingham
    Posts
    335
    hi jobemjobem

    it's always nice to know why certain things happen, so thanks for that!
    I'll look into implementing your third method.

    also, thanks your game design book - it's been really useful and inspirational!

  11. #11
    curiouser and curiouser LittleRed's Avatar
    Join Date
    Mar 2004
    Location
    Nottingham
    Posts
    335
    hi, I'm having trouble with finding out about attaching classes to a MovieClip. Is there a good tutorial available anywhere?
    thanks

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