A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Attach mcs randomly without overlaping each other

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    17

    Attach mcs randomly without overlaping each other

    Hello, I have a few mc's in the library ("mc1, mc2, mc3, mc4...) how can I attach them from the library to the stage randomly, but without getting it out of the stage and without overlapping to each other?

    Thank you!

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Right-click each one of your movieclips in the library, choose Linkage.. (or Properties->Advanced in CS3-CS5), check Export for ActionScript, and in the identifier field, give them a unique ID, for example, mc1, mc2, etc.

    Click on your Frame, and use a code like this:

    Actionscript Code:
    attachMovie("mc1", "mc1", 1);
    mc1._x = Math.random(0)*Stage.width;
    mc1._y = Math.random(0)*Stage.height;
    mc1.onEnterFrame = function(){
        if(mc1.hitTest(_root.mc2) || mc1.hitTest(_root.mc3) || mc1.hitTest(_root.mc4)){
            mc1._x = Math.random(0)*Stage.width;
            mc1._y = Math.random(0)*Stage.height;
        } else {
            mc1._x = mc1._x;
            mc1._y = mc1._y;
            delete mc1.onEnterFrame;
        }
    }

    Add another for mc2, for example:

    Actionscript Code:
    attachMovie("mc2", "mc2", 2);
    mc2._x = Math.random(0)*Stage.width;
    mc2._y = Math.random(0)*Stage.height;
    mc2.onEnterFrame = function(){
        if(mc2.hitTest(_root.mc1) || mc2.hitTest(_root.mc3) || mc2.hitTest(_root.mc4)){
            mc2._x = Math.random(0)*Stage.width;
            mc2._y = Math.random(0)*Stage.height;
        } else {
            mc2._x = mc2._x;
            mc2._y = mc2._y;
            delete mc2.onEnterFrame;
        }
    }

    What the code does, is that it attaches the movieclip from the library, to the stage, and gives it a random X and Y, between 0 and the Height of your Stage, making it NOT go out of stage. Then, a loop is assigned to check if the movieclip is hitting another movieclip - if it is, then it should be given new coordinates, else, it should stay there!

    You can also use a while loop to loop through the same code, as many times as you want, instead of copying and pasting a LONG code for each mc, like this:

    Actionscript Code:
    count = 5; // how many movieclips you have
    i = 1;

    while(i<count+1){
        attachMovie("mc"+i, "mc"+i, i);
        this["mc"+i]._x = Math.random(0)*Stage.width;
        this["mc"+i]._y = Math.random(0)*Stage.height;
        this["mc"+i].onEnterFrame = function(){
            for(a=1;a<count+1;a++){
                if(this["mc"+i].hitTest(_root["mc"+a])){
                    this["mc"+i]._x = Math.random(0)*Stage.width;
                    this["mc"+i]._y = Math.random(0)*Stage.height;
                } else {
                    this["mc"+i]._x = this["mc"+i]._x;
                    this["mc"+i]._y = this["mc"+i]._y;
                    delete this["mc"+i].onEnterFrame;
                    i++;
                }
            }
        }
    }

    I'm not sure if it'll work properly, because I haven't tried it, but what it should do, is to loop through the code, attach a movieclip on stage, and when it's attached with the correct coordinates, increase i variable, so that it starts checking the same for mc2, etc. this["mc"+i] can be mc1, mc2, mc3, mc4 and mc5, because, the while loop will continue as long as i is smaller than count!
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    17
    Thank you so much Nig 13. I will try with it.

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    17
    When I compile the movie, Flash slows down and I get a flash player window saying that the script is running slowly and if I want to stop it.....

    I think that is better to attach the first movie clip randomly and about 4 seconds later delete it and attach the following mc and so on until all are loaded because I have to attach about 20 or 30 mcs and if I do like you're saying, the mcs don't have space enough to are all separated. The only thing to consider is that the following mc do not be loaded in the same position as the above.

    How this could be done?
    Last edited by maxpower78; 08-25-2011 at 08:46 AM.

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Oh, just checked the code, and it didn't work :/

    Well, I'm not sure what you mean by NOT OVERLAPPING each other. Like, that their coordinates should not be the same as some other movieclip's, or that no parts of the movieclip should touch another? The non-touching part is difficult, while the non-same-coordinates part, I think, is easy
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  6. #6
    Junior Member
    Join Date
    Apr 2011
    Posts
    17
    I mean the non-same-coordinates.

  7. #7
    Junior Member
    Join Date
    Apr 2011
    Posts
    17
    I finally got it. thank you!

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