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
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
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
---------------------------------------------------------------------- ------------------
thanks it worked. there is another problem i have come across which is seperate
heres my dilema
i currently have 5 sprites and 5 different set co ordinates for each one to spawn into with individual behaviour scripts, however i want each sprite to spawn into one of the set co ordinate without more than one of them spawning into the same point.
so eventually each sprite will spawn into a random set co ordinate
is this possible?
so something like
pRandomStartingLoc = (200,100) (300,100) (400,100) so each sprite spawns into only one set of cordinates without more than one spawning into the same one
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
North America
Europe, Middle East and Africa
Asia Pacific