Skip navigation
elimann
Currently Being Moderated

returning to a random cordinate?

Apr 12, 2012 3:05 PM

basically i have an object set to appear at random co ordinate

 

start_x = random(800)

start_y = 300 + random(500)

sprite(ch).locH = start_x

sprite(ch).locV = start_y

 

when moving the object and then detect a collision with another object i want it too return to that random coordinate created above so far i can only set it

 

eg sprite(ch).locH = -1000

 
Replies
  • Currently Being Moderated
    Apr 13, 2012 12:32 PM   in reply to elimann

    It's going to depend on your code but you just need to store the location in either a user-defined sprite property (best method) or a global variable. Where is this code found that you have posted, in a sprite behavior?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 13, 2012 12:39 PM   in reply to elimann

    I am guessing your code is not in a sprite behavior, but that it is in a movie script somewhere. If that is the case it could look like this:

     

    global gRandomStartingLoc

    gRandomStartingLoc = point(random(800), 300+random(500))

    sprite(ch).loc = gRandomStartingLoc

     

    Then after the collision just reset the loc again

     

    global gRandomStartingLoc

    sprite(ch).loc = gRandomStartingLoc

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 13, 2012 6:11 PM   in reply to elimann

    Actually it would, but it would be better to use a sprite property. Same code though.

     

    -------------------------------------------------------------------

    property pRandomStartingLoc

    pRandomStartingLoc = point(random(800), 300+random(500))

    sprite(ch).loc = pRandomStartingLoc

     

    on CollisionHandler me

         sprite(spriteNum).loc = pRandomStartingLoc

    end

    ------------------------------------------------------------------

     

    So is this behavior attached to sprite(ch) ? If so, there's not need to reference the hard coded sprite name. You remove the hard coded sprite name so that it could be applied to another sprite and not need modifying. It makes for more "reuseable" code.

     

    -------------------------------------------------------------------

    property spriteNum, pRandomStartingLoc

     

    pRandomStartingLoc = point(random(800), 300+random(500))

    sprite(spriteNum).loc = pRandomStartingLoc

     

     

    on CollisionHandler me

         sprite(spriteNum).loc = pRandomStartingLoc

    end

    ------------------------------------------------------------------

     

    Going a step further, suppose you have multiplle sprites with similar behavior, except their random starting loc different. You can remove all hard coded values. This behavior could be applied to any number of sprites without changing the code. Each would be customizable through the parameters dialog.

     

    ---------------------------------------------------------------------- ------------------

    property spriteNum, pRandomStartingLoc, pMaxRandomX, pMaxRandomY, pXmodifier, pYmodifier

     

    on beginSprite me

      pRandomStartingLoc = point(random(pMaxRandomX) + pXmodifier, random(pMaxRandomY) + pYmodifier)

      sprite(spriteNum).loc = pRandomStartingLoc

    end

     

    on CollisionHandler me

      sprite(spriteNum).loc = pRandomStartingLoc

    end

     

    on getPropertyDescriptionList

      description = [:]

      addProp description,#pMaxRandomX, [#default:800, #format:#integer, #comment:"Max Random LocX"]

      addProp description,#pMaxRandomY, [#default:800, #format:#integer, #comment:"Max Random LocY"]

      addProp description,#pXmodifier, [#default:0, #format:#integer, #comment:"Add this amount to the random LocX"]

      addProp description,#pYmodifier, [#default:0, #format:#integer, #comment:"Add this amount to the random LocY"]

      return description

    end

    ---------------------------------------------------------------------- ------------------

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 15, 2012 7:11 AM   in reply to elimann

    For fun I modified Mister MUX's behavior to pick random locations with no overlapping sprites.

     

    This is accomplished by adding a global list of rects that are in current use, and making sure no new sprite rects overlap any rects in the list.

     

    The global list is initialized in a Movie script:

     

    global gOccupiedRects -- list of rects that are currently used by sprites

     

    on prepareMovie

      gOccupiedRects = []

    end  prepareMovie

     

     

    And the modified behavior is:

     

    property spriteNum, pRandomStartingLoc, pMaxRandomX, pMaxRandomY, pXmodifier, pYmodifier

    property Sp  -- this sprite

     

    global gOccupiedRects -- list of rects that are currently used by sprites. Cleared in movie script

     

    on beginSprite me

      Sp = sprite(spriteNum)

      pRandomStartingLoc = point(random(pMaxRandomX) + pXmodifier, random(pMaxRandomY) + pYmodifier)

    Sp.loc = pRandomStartingLoc


      cnt = 0

      repeat while (me.overlaps(Sp.rect))

        cnt = cnt + 1

        if cnt > 10000 then  -- don' t want to get stuck in an infinite loop

          put("Can not find non-overlapping location to place sprite. Aborting")

          abort()

        end if

     

        -- try new random location

        pRandomStartingLoc = point(random(pMaxRandomX) + pXmodifier, random(pMaxRandomY) + pYmodifier)

        Sp.loc = pRandomStartingLoc

      end repeat

     

      gOccupiedRects.add(Sp.rect)

    end beginSprite

     

     

    -- check if testRect intersects/overlaps any rect in gOccupiedRects list

    on overlaps me, testRect

      EmptyRect = rect(0,0,0,0)

      repeat with aRect in gOccupiedRects

        if (aRect.intersect(testRect) <> EmptyRect) then return true  -- true means overlap

      end repeat

     

      return false

    end overlaps

     

     

    on CollisionHandler me

      Sp.loc = pRandomStartingLoc

    end CollisionHandler

     

     

    on getPropertyDescriptionList

     

      description = [:]

     

      addProp description,#pMaxRandomX, [#default:800, #format:#integer, #comment:"Max Random LocX"]

     

      addProp description,#pMaxRandomY, [#default:800, #format:#integer, #comment:"Max Random LocY"]

     

      addProp description,#pXmodifier, [#default:0, #format:#integer, #comment:"Add this amount to the random LocX"]

     

      addProp description,#pYmodifier, [#default:0, #format:#integer, #comment:"Add this amount to the random LocY"]

     

      return description

    end getPropertyDescriptionList

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points