>i just want to make a behaviour which makes the sprite
>move randomly on the stage and also the restricts the
>movement of the sprite to the stage only and at the
>same time when the sprite touchec the edges of teh
>stage it should bounce back normally with an option
>for the use to control the elasticity as well as the
>speed of the bounce
Hi Sreedhar--
Here is a behavior that doesn't do everything you want, but it comes close.
Robert Wingate provided the start for me, and I've expanded it to use
elasticity. This is frame-based, but if you use the timeout object that is
commented out in the beginSprite handler, and change the exitFrame to
moveMe, you can control the speed by the timeout length. Elasticity is
controlled by pSquishMaxV and pSquishMaxH--you could add those to the
getPropertyDescriptionList and make it user configurable.
Cordially,
Kerry Thompson
Learning Network
property kSpeed, kBound, spriteNum
property kLimitL, kLimitT, kLimitR, kLimitB
property iDirH, iDirV
property pOrigWidth, pOrigHeight
property pSquishCycles, pSquishMaxV, pSquishMaxH
property pSquishIn, pSquishOut
property pSide
--=============================
on beginSprite me
theSprite = sprite(spriteNum)
pOrigWidth = theSprite.width
pOrigHeight = theSprite.height
w2 = pOrigWidth / 2 + 1
h2 = pOrigHeight / 2 + 1
pSquishCycles = 0
pSquishMaxV = INTEGER(theSprite.height - theSprite.height * 0.6) / 2
pSquishMaxH = INTEGER(theSprite.width - theSprite.width * 0.6) / 2
pSquishIn = FALSE
pSquishOut = FALSE
-- set boundaries
rStage = (the stage).rect
kStageWidth = rStage.right - rStage.left
kStageHeight = rStage.bottom - rStage.top
kLimitL = w2
kLimitT = h2
kLimitR = kStageWidth - w2
kLimitB = kStageHeight - h2
iDirH = [ kSpeed,-kSpeed ][ random(2) ]
iDirV = [ kSpeed,-kSpeed ][ random(2) ]
-- timeout("bounceTime").new(10, #moveMe, me)
end
--=============================
on exitFrame
theSprite = sprite(spriteNum)
curH = theSprite.locH
curV = theSprite.locV
if pSquishIn = TRUE then -- shrink the sprite
pSquishCycles = pSquishCycles + 1
if (pSide = #r) or (pSide = #l) then
if pSquishCycles < pSquishMaxH then --set to unsquish
theSprite.width = theSprite.width - 2
if NOT (pSquishCycles MOD 2) then
if pSide = #r then
theSprite.locH = curH + 1
else
theSprite.locH = curH - 2
end if
if iDirV < 0 then -- slide it along the side in the direction
it's moving
if curV > kLimitT then
theSprite.locV = curV -1
end if
else
if curV < kLimitB then
theSprite.locV = curV + 1
end if
end if
end if
else
pSquishIn = FALSE
pSquishOut = TRUE
pSquishCycles = 0
end if
else
if pSquishCycles < pSquishMaxV then
theSprite.height = theSprite.height - 2
if NOT (pSquishCycles MOD 2) then
if pSide = #b then
theSprite.locV = curV + 1
else
theSprite.locV = curV - 2
end if
if iDirH < 0 then -- slide it along the side in the direction
it's moving
if curH > kLimitL then
theSprite.locH = curH -1
end if
else
if curH < kLimitR then
theSprite.locH = curH + 1
end if
end if
end if
else
pSquishOut = TRUE
pSquishIn = FALSE
pSquishCycles = 0
end if
end if
else if pSquishOut = TRUE then -- grow the sprite back
-- pSquishCycles = pSquishCycles + 1
if (pSide = #r) or (pSide = #l) then
if theSprite.width < pOrigWidth then --set to unsquish
theSprite.width = theSprite.width + 2
if NOT (pSquishCycles MOD 2) then
if pSide = #r then
theSprite.locH = curH - 1
else
theSprite.locH = curH + 2
end if
if iDirV < 0 then -- slide it along the side in the direction
it's moving
if curV > kLimitT then
theSprite.locV = curV -1
end if
else
if curV < kLimitB then
theSprite.locV = curV + 1
end if
end if
end if
else
pSquishOut = FALSE
pSquishCycles = 0
end if
else
if theSprite.height < pOrigHeight then
theSprite.height = theSprite.height + 2
if NOT (pSquishCycles MOD 2) then
if pSide = #t then
theSprite.locV = curV + 2
else
theSprite.locV = curV - 1
end if
if iDirH < 0 then -- slide it along the side in the direction
it's moving
if curH > kLimitL then
theSprite.locH = curH -1
end if
else
if curH < kLimitR then
theSprite.locH = curH + 1
end if
end if
end if
else
pSquishOut = FALSE
pSquishCycles = 0
end if
end if
else -- move the sprite
theSprite.loc = point( curH,curV ) + point( iDirH,iDirV )
sendAllSprites(#moveUs)
if ( curH < kLimitL ) or ( curH > kLimitR ) then
pSquishIn = TRUE
if (curH < kLimitL) then
pSide = #l
else
pSide = #r
end if
iDirH = -iDirH
end if
if ( curV < kLimitT ) or ( curV > kLimitB ) then
pSquishIn = TRUE
if (curV < kLimitT) then
pSide = #t
else
pSide = #b
end if
iDirV = -iDirV
end if
end if
end
--=============================
on getPropertyDescriptionList
def = the currentSpriteNum - 1
ls = [:]
ls.addProp( #kBound, [ #comment:"Bounding rect sprite:",¬
#format:#integer, #default: the currentSpriteNum - 1 ])
ls.addProp( #kSpeed, [ #comment:"Bounce Speed:", #format:#integer,¬
#range:[#min:1,#max:10], #default:1 ])
return ls
end
[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/LUJ/lingo-l.cgi To post messages to the list,
email [EMAIL PROTECTED] (Problems, email [EMAIL PROTECTED])
Lingo-L is for learning and helping with programming Lingo. Thanks!]