|
|
[S] mouse->sprite vector behaviour
Added on 6/28/2000
|
Tolerence controls how slowly the mouse has to move before ignoring the mouse vector and using any previous vector. The tolerance and limiting area are measured in pixels.
Download PC Source Download Mac Source
--Properties -
--pTolerance = Amount mouse can move and sprites follow it.
--pLimitToArea = Whether to limit the sprite to a certain area.
--pLimitX1 = Left-most value of limiting area (if used).
--pLimitY1 = Top-most value of limiting area (if used).
--pLimitX2 = Right-most value of limiting area (if used).
--pLimitY2 = Bottom-most value of limiting area (if used).
--Written by Barry Swan (gerbil@theburrow.co.uk).
----------------------------------------
-- Mouse->sprite vector behaviour
-- Barry Swan, 28/06/2000
-- gerbil@theburrow.co.uk
-- http://www.theburrow.co.uk
-- All rights reserved
----------------------------------------
property pTolerance
property pLimitToArea
property pLimitX1, pLimitY1, pLimitX2, pLimitY2
property pOldX, pOldY
property pDX, pDY
on beginSprite me
-- Set up necessary properties
pOldX = the mouseH
pOldY = the mouseV
pDX = 0
pDY = 0
end
on prepareFrame me
-- Find amount moved this frame
tMouseX = the mouseH
tMouseY = the mouseV
tDX = pOldX - tMouseX
tDY = pOldY - tMouseY
-- Check if distance is below tolerance level
tDistance = tDX * tDX + tDY * tDY
if tDistance < pTolerance * pTolerance then
tDX = pDX
tDY = pDY
else
pDX = tDX
pDY = tDY
end if
-- Update position variables
tNewX = sprite(me.spriteNum).locH - tDX
tNewY = sprite(me.spriteNum).locV - tDY
-- If set to limit to bounding area, do so
if pLimitToArea then
tNewX = min(max(tNewX, pLimitX1), pLimitX2)
tNewY = min(max(tNewY, pLimitY1), pLimitY2)
end if
-- Update sprite position
sprite(me.spriteNum).loc = point(tNewX, tNewY)
-- Store mouse position to compare for next frame
pOldX = tMouseX
pOldY = tMouseY
end
on getPropertyDescriptionList me
-- Define boundaries
tX1 = 0
tY1 = 0
tX2 = the stageRight - the stageLeft
tY2 = the stageBottom - the stageTop
-- Create property dialog box
tlData = [:]
tlData[#pTolerance] = [#format: #integer, #default: 3, #comment: "Tolerance of mouse speed:"]
tlData[#pLimitToArea] = [#format: #boolean, #default: TRUE, #comment: "Limit sprite to area:"]
tlData[#pLimitX1] = [#format: #integer, #default: tX1, #comment: "Left limit:"]
tlData[#pLimitY1] = [#format: #integer, #default: tY1, #comment: "Top limit:"]
tlData[#pLimitX2] = [#format: #integer, #default: tX2, #comment: "Right limit:"]
tlData[#pLimitY2] = [#format: #integer, #default: tY2, #comment: "Bottom limit:"]
return tlData
end
on getBehaviorDescription me
tText = "[Sprite] Mouse->Sprite vector behaviour." & RETURN & RETURN
tText = tText & "Tolerence controls how slowly the mouse has to move before ignoring the mouse vector and using any previous vector." & RETURN
tText = tText & "The tolerance and limiting area are measured in pixels." & RETURN & RETURN
tText = tText & "Properties -" & RETURN
tText = tText & "pTolerance" & TAB & "= Amount mouse can move and sprites follow it." & RETURN
tText = tText & "pLimitToArea" & TAB & "= Whether to limit the sprite to a certain area." & RETURN
tText = tText & "pLimitX1" & TAB & TAB & "= Left-most value of limiting area (if used)." & RETURN
tText = tText & "pLimitY1" & TAB & TAB & "= Top-most value of limiting area (if used)." & RETURN
tText = tText & "pLimitX2" & TAB & TAB & "= Right-most value of limiting area (if used)." & RETURN
tText = tText & "pLimitY2" & TAB & TAB & "= Bottom-most value of limiting area (if used)." & RETURN & RETURN
tText = tText & "Written by Barry Swan (gerbil@theburrow.co.uk)."
return tText
end
|
|