1 Reply Latest reply on Nov 28, 2007 9:41 AM by BSpero

    Random Distribution without Overlapping?

    christian_cruz
      Hello, I have a doubt about " overlappings".
      I have this code:

      on distributeMe me

      repeat with channel = 10 to 17
      pRandomX = random (320,677)
      pRandomY = random (185,374)
      sprite(channel).locH = pRandomX
      sprite(channel).locV = pRandomY
      end repeat

      end

      It distributes the sprites located in channels from 10 to 17 within a "virtual space" delimited by X(320 to 677) and Y (185 to 374) randomly. I was wondering:

      What extra code do I need in order to get all the sprites distributed randomly BUT NOT OVERLAPPING, not even by 1 pixel, the eight of them?

      I was trying with intersect but....well pretty obvious... I cannot get it done.

      Any help will be much appreciated.
        • 1. Random Distribution without Overlapping?
          BSpero Level 1
          Christian,
          Are these sprites irregularly shaped? And are they all different. If you got the size of the bounding box, then you could place them on a virtual grid with the coordinates being as far apart as you need to be. Then keep a list of where each one is to make sure you don't put it in the same place. The second way is using the code you already have going on. It places the objects on the stage, then runs through all the sprites of lower numbers to make sure they don't intersect. If they do, it places the sprite somehwere else. As usual, I am not a master programmer, so this code could probably be better. But here you go...