A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Randomly Position MoveClip without Overlapping?

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    21

    Question Randomly Position MoveClip without Overlapping?

    I'm using AS 3 and Flash CS 6.

    I'm using this code to randomly place movie clips from the library in a container on the stage:
    Code:
    var container:Sprite = new Sprite();
    addChild(container);
    container.x = 300;
    container.y = 200;
    
    var food:food_mc = new food_mc();
    container.addChild(food);
    food.x = Math.random()*450;
    food.y = Math.random()*350;
    
    var food1:food_mc = new food_mc();
    container.addChild(food1);
    food1.x = Math.random()*450;
    food1.y = Math.random()*350;
    There are actually eight movie clips I am adding to the stage but just pasted the code from two.

    Unfortunately they are overlapping, which isn't what I want.

    Is there a way to ensure they don't overlap?

    Many thanks.

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You could use a timer to position the a movieclip and test whether it overlaps by using hitTestObject(object). You let the timer run changing x and y until there is no overlap and then stop it. However, this is ok if you have two or may be 3 clips. When the number is higher you have to test all of them against each other and then it gets complicated.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    21
    Thanks for the reply.

    There would be about 6-8 objects I want to place on the stage so a timer, like you say, might be too intensive.

    Is there a way I could use a grid type system and assign each object to its own grid (or area) on the stage.

    The select at random, which objects to display?

    That way there would be no chance of overlapping as each objects would only have one possible place it could be located, which would be unique to that object.

    Any ideas?

    Thanks.

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Here is a class (GridMaker) that creates a grid
    http://www.flashscript.biz/flashas3/biz/index.html
    However, usually you can only make a grid from one type of object. So one possibility is to create a grid of containers.
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    21
    Thanks, I will have a look.

    I should point out that I still need that in random positions, despite being placed on a grid.

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Then you need to have the random x and y max not larger not to overlap with any neighbor image.
    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    21
    Thanks, can you point me in the right direction for finding the AS for that?

    Would I be setting the numbers here with min and max values?:
    food.x = Math.random()*450;
    food.y = Math.random()*350;

    What would that look like?
    food1.x = Math.random()*< 100 > 500;
    food1.y = Math.random()* < 100 > 500;

    food2.x = Math.random()*< 501 > 1000;
    food2.y = Math.random()* < 501 > 1000;

    Or something else entirely?

    Thanks.

  8. #8
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    If an x bigger than 100 would cause overlap, I would set the limit as 100 (Math.random()*100). I don't think you can have the smaller exception unless you do an if statement. Another possibility is to create an array with numbers and then use Math.random on the array. Then you can control easily the lower and upper limit.
    - The right of the People to create Flash movies shall not be infringed. -

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    21
    I'm not really sure what would cause the overlap in terms of size. The objects (is that the right word?) are 155 x 69 pixels in terms of size.

    I have uploaded my rough file if you want to have a look. Ignore the blue fish, its the orange fish that appear that are randomised.

    The code to make them appear is:
    Code:
    var container:Sprite = new Sprite();
    addChild(container);
    container.x = 300;
    container.y = 400;
    
    var food:food_mc = new food_mc();
    container.addChild(food);
    food.x = Math.random();
    food.y = Math.random();
    
    var food1:food_mc = new food_mc();
    container.addChild(food1);
    food1.x = Math.random()*200;
    food1.y = Math.random()*200;
    
    var food2:food_mc = new food_mc();
    container.addChild(food2);
    food2.x = Math.random()*300;
    food2.y = Math.random()*300;
    
    var food3:food_mc = new food_mc();
    container.addChild(food3);
    food3.x = Math.random()*475;
    food3.y = Math.random()*350;
    
    var food4:food_mc = new food_mc();
    container.addChild(food4);
    food4.x = Math.random()*475;
    food4.y = Math.random()*350;
    
    var food5:food_mc = new food_mc();
    container.addChild(food5);
    food5.x = Math.random()*475;
    food5.y = Math.random()*350;
    
    var food6:food_mc = new food_mc();
    container.addChild(food6);
    food6.x = Math.random()*475;
    food6.y = Math.random()*350;
    and is in frame 40.

    Do you know where I could find someone who could sort this out for me and how much it might cost?

    Thanks for your help.
    Attached Files Attached Files

  10. #10
    Junior Member
    Join Date
    Dec 2012
    Posts
    21
    I found someone on Odesk to do this for me.

    I'm attaching the fla file in case anyone else wants to know how to do this.

    random-fish-odesk-ivan.fla

Tags for this Thread

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