A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Dynamically setting properties of a MC instance. (using a loop)

  1. #1
    Senior Member LeoBeer's Avatar
    Join Date
    May 2002
    Posts
    315
    Hi I ran into a problem you might be able to solve.

    In my application a number of similar MC’s will appear each time the user will press a button. the MC’s will appear randomly in different locations. Assuming there are 5 MC’s (movie1,movie2...) I would like to run a loop so each movie instance name will get one of 5 optional values:

    -------------------------------------------------------------------
    B=0
    while(b<4){
    pos=random(100)
    eval("movie"+b+"._x = pos")
    eval("movie"+b+"._y = pos")
    b=b+1
    }
    -------------------------------------------------------------------

    this script does not work, I understand that it might be because you can only read variables using 'eval' but not write them. however according to the Macromedia Flash MX help :
    "You can also use the eval function to dynamically set and retrieve the value of a variable or instance name..."

    how can I solve this and assign the X,Y property to each instance of an MC using a loop ?

    Thanks.

  2. #2
    hi
    i dont know why loops arent working with me either when i use while. mmmm, for example, if u use while b<4 ..... b=b+1. it will execute the script 4 times alone then it will play the final result, which is when b=4.

    Try the following :

    in frame (4 for example), b= 0;

    in frame 5, put the script that u want to execute.

    in frame 6, put :
    if (b<4) {
    gotoandplay(5);
    }

    this should make the loop work

  3. #3
    Senior Member
    Join Date
    Nov 2000
    Posts
    237
    check it
    Code:
    eval(["movie"+b]._x = pos)
    or
    Code:
    eval(_root["movie"+b]._x = pos)
    use _root, _level....

  4. #4
    Member
    Join Date
    Sep 2001
    Location
    Paris, FRANCE
    Posts
    53
    Hi!

    Maybe you already have find a solution but if you don't, you can try that:

    ________________________________________
    B=0
    while(b<4){
    pos=random(100);
    eval("movie"+b+"._x) = pos;
    eval("movie"+b+"._y) = pos;
    b=b+1;
    }

    _____________________________________

    It's just a suggestion!

  5. #5
    Senior Member LeoBeer's Avatar
    Join Date
    May 2002
    Posts
    315
    Thanks for the responds, I will try to explain my self better this time:

    I am not trying to put a value into the variable ‘Pos’ , I am trying to give a different values to different properties. I guess the main problem is that both the left and the right side of my statement has variables in them that’s because both the MC name and it’s position are dynamic. for some reason this does not allow me to use ‘eval’ the way I want.

    trace("movie"+b+"._x = pos")

    might output: movie1._x = pos

    but writing:

    eval("movie"+b+"._x = pos")

    will not locate my movie in a random location on the screen like you expect it to do ,you can try and see for yourself.

    I guess there is a different way to do it, I did not find it yet.

    Thank you all,

    LeoBeer.

  6. #6
    Senior Member
    Join Date
    Jul 2002
    Posts
    461
    use array notation instead of eval:

    ----------------------------------------
    b=0
    while(b<4){
    pos=random(100)
    this["movie"+b]._x = pos
    this["movie"+b]._y = pos
    b=b+1
    }
    ----------------------------------------


  7. #7
    Senior Member LeoBeer's Avatar
    Join Date
    May 2002
    Posts
    315

    Smile

    Thanks gaz_b, it looks like you just solved the problem for me.


  8. #8
    Senior Member LeoBeer's Avatar
    Join Date
    May 2002
    Posts
    315
    Ok, here is a new problem.
    I've created a function which gets executed each by an external movie. the function will run one of the movies on the screen, the movie number is being sent as a variable (s). again Flash refuses to dynamically run a movie, here's the code:

    -----------------------------------------------------------------------
    function textFadeFunc (s) {
    this["_root.movie"+s+".play()"]

    }
    -----------------------------------------------------------------------
    also tried to replace line 2 with:

    1. this["_root.movie"+s].play()

    2. eval("_root.movie"+s+".play()")

    3. _root.movie[s].play()


    how can I make this work ?
    Thanks


  9. #9
    Senior Member
    Join Date
    Jul 2002
    Posts
    461
    it looks like it should be this:
    -----------------------------------------------------------------------
    function textFadeFunc (s) {
    _root["movie"+s].play()
    }
    -----------------------------------------------------------------------
    if that doesn't work then if you post the name/path of the clip you want to target and where you are calling it from i can correct it.


  10. #10
    Senior Member LeoBeer's Avatar
    Join Date
    May 2002
    Posts
    315
    thanks gaz

    I alrerady solved that:

    eval("_root.word"+s).play()

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