A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: A question about mutliple instances on depths

  1. #1
    Senior Member
    Join Date
    Mar 2002
    Location
    In the sky, apple pie!
    Posts
    105

    A question about mutliple instances on depths

    Code:
    var maxflakes:Number = 70;
    for (var i = 0; i<maxflakes; i++) {
    	var flake:MovieClip = attachMovie("snow", "flake", i);
    	flake._x = Math.random()*Stage.width;
    	flake._y = Math.random()*Stage.height;
    	flake._alpha = 20+Math.random()*75;
    	flake._xscale = flake._yscale=30+Math.random()*40;
    }
    ok, playing around with snowflake particles. I understand this code, but I don't know exactly why it works.

    my interstanding is the variable flake, which is a movieclip, is recreated 70 times in this case. but why are 70 SEPARATE instances created, when they all have the same varialbe name? is it because of depths?

    if in my script, I have cow="moo" then cow="moomo", cow will be equal to momo since that was run after, overwriting the previous. so how can this code not cause conflict?
    and also, where I have 'flake' as my 'new identifier' parameter, how can I have 70 objects with the same name?




    please clarifiy! thank you very much?


    I understand HOW it works, but all I'm wondering is how can two instances, with the same name, exist at the same time. is it because they are on separate depths? we have 70 instances all named the same, how does that work?
    Last edited by flashbeast; 03-01-2006 at 03:37 AM.

  2. #2
    Ninja Rider Jhonte's Avatar
    Join Date
    Sep 2004
    Location
    Uppsala,Sweden
    Posts
    253
    attachMovie returns an id that you can use to manipulate the information of the mc.
    That is your var flake:MovieClip.

    So you dont use the "real" mc. If you later on want to change some attribute of all your mc:s you will find that you cant since they have the same name.

    A simple solution is to add your flake into an array afterwards. (or change the name for all movieclips) (attachMovie("snow", _root["flake"+i], i);
    Code:
    var maxflakes:Number = 70;
    var flakes_array:Array = new Array();
    for (var i = 0; i<maxflakes; i++) {
    	var flake:MovieClip = attachMovie("snow", "flake", i);
    	flake._x = Math.random()*Stage.width;
    	flake._y = Math.random()*Stage.height;
    	flake._alpha = 20+Math.random()*75;
    	flake._xscale = flake._yscale=30+Math.random()*40;
            flakes_array.push(flake);
    }
    And then use for instance:
    Code:
    flakes_array[0]._x = 100;
    ... and so on.
    Show me the way to the next whiskeybar ...

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