2 Replies Latest reply on Jan 10, 2008 12:22 PM by gtrufitt

    Random Image / Sprite Location

    gtrufitt
      Hi,

      I have 12 images that I wish to appear in random locations within a certain area of the stage when the movie is begun or a button is pressed. I found a small bit of code, but I am unable to decipher it:

      on mouseUp me
      sprite (2).loc = point (((random ((the stageright) - 700)) + 500), ((random ((the stageBottom) - 500)) + 400))
      end

      If any one is able to help me with this or suggest a better way to do it that would be great.

      Cheers, Gareth
        • 1. Re: Random Image / Sprite Location
          robdillon Most Valuable Participant
          The loc is a sprite property that holds the horizontal and vertical position of the sprite. It is expressed in pixels and is called a point. For example:
          put sprite(2).loc
          -- point (5,16)

          Random is a function that will return a random number between a minimum and maximum value. If only one value is given, then the random number will be between 1 and the number given. For example:
          put random(3)
          -- 2
          put random(3, 17)
          -- 10

          The stageRight, and the stageLeft, the stageTop and the stageBottom, are properties of the staqe. Each of these properties will return the position of that edge of the stage on the user's monitor. To find out how wide the stage is, for example, you would subtract the stageLeft from the stageRight. There is, oddly enough, no built in property to describe the width or height of the stage. In any case, the calculation in your function is used to give the random function two numbers, with a difference of 500 pixels left to right and 400 pixels top to bottom.

          The result is that sprite 2 will be positioned somewhere on the stage in a 500 by 400 pixel space on the stage.

          When working with this sort of function, keep in mind that the registration point for a bitmap sprite will usually be at its center. So the center of the sprite will be positioned somewhere in that space, not necessarily the whole sprite.
          • 2. Re: Random Image / Sprite Location
            gtrufitt Level 1
            Thank you very much for the help

            Gareth