|
|
|
Rotator 1.1
Added on 6/10/1999
|
Note that you can easily add more motion options... the exitFrame handler dispatches the event to the chosen method.
property spriteNum
property myStartArt, numberOfGraphics
property rotateType, spriteTarget, rotationDuration
property oldLoc, rotateAngle, startTime
--====== EXTERNAL EVENTS:
on beginSprite me
GetArtwork me
set oldLoc to the loc of sprite spriteNum
set rotateAngle to 360.0/numberOfGraphics
set startTime to the ticks
set rotationDuration to rotationDuration * 60.0/ numberOfGraphics
end
on exitFrame me
case (rotateType) of
"Align with sprite's motion": RotateWithMotion me
"Point to mouse": PointToMouse me
"Point at another sprite": PointAtSprite me
"Time based": IncrementTimer me
end case
end
--====== VARIOUS OPTIONS FOR MOTION:
on RotateWithMotion me
set newLoc to the loc of sprite spriteNum
set delta to newLoc - oldLoc
if abs(the locH of delta) < 1 AND abs(the locV of delta) < 1 then exit
set theAngle to GetAngle(newLoc - oldLoc)
set offset to integer(theAngle/rotateAngle) mod numberOfGraphics
set the member of sprite spriteNum to myStartArt + offset
set oldLoc to newLoc
end
on PointToMouse me
set theAngle to GetAngle(point(the mouseH, the mouseV) - the loc of sprite spriteNum)
set offset to integer(theAngle/rotateAngle) mod numberOfGraphics
set the member of sprite spriteNum to myStartArt + offset
end
on PointAtSprite me
set myLoc to the loc of sprite spriteNum
set targetLoc to the loc of sprite spriteTarget
set theAngle to GetAngle(targetLoc - myLoc)
set offset to integer(theAngle/rotateAngle) mod numberOfGraphics
set the member of sprite spriteNum to myStartArt + offset
end
on IncrementTimer me
set now to (the ticks - startTime) / rotationDuration
set offset to integer(now) mod numberOfGraphics
set the member of sprite spriteNum to myStartArt + offset
end
--====== UTILITY ROUTINES:
-- Takes x,y position, converts to 0-360 angle.
-- Origin is at top and direction is clockwise.
on GetAngle thePoint
set x to the locH of thePoint
set y to the locV of thePoint
if y = 0 then set y = 0.0001
set angle to atan(float(x) / y) * -180/pi()
if y >= 0 and x >= 0 then
set angle = 180 + angle
else if x < 0 and y >= 0 then
set angle = 180 + angle
else if y < 0 and x < 0 then
set angle = 360 + angle
else
nothing
end if
return angle
end
-- Counts how many graphics follow this one in the Cast:
on GetArtwork me
set myStartArt to the number of member (the member of sprite spriteNum)
set numberOfGraphics to 0
repeat while the type of member (myStartArt + numberOfGraphics) = #bitmap
set numberOfGraphics to numberOfGraphics + 1
end repeat
end
--====== STANDARD BEHAVIOR HANDLERS:
on getPropertyDescriptionList me
set theProps to [:]
set c to "What causes sprite to rotate?"
set f to #string
set r to ["Align with sprite's motion", "Point to mouse", "Point at another sprite", "Time based"]
set d to getAt(r,1)
set typeProps to [#comment: c, #format: f, #range: r, #default: d]
addProp theProps, #rotateType, typeProps
set c to "If tracking sprite, which?"
set f to #integer
set d to 1
addProp theProps, #spriteTarget, [#comment: c, #format: f, #default: d]
set c to "If timebased, how many seconds?"
set f to #float
set d to 3
addProp theProps, #rotationDuration, [#comment: c, #format: f, #default: d]
return theProps
end
on getBehaviorDescription me
set line1 to " This behavior will rotate a sprite so that it is always¬
aligned with its motion, or so it always points to the mouse or another¬
sprite, or so that it rotates fully in a certain amount of time." &RETURN
set line2 to " It uses consecutive graphics in the Cast. These start¬
from the upright position and proceed clockwise. End the sequence with an¬
empty cast slot or a member which is not a graphic. Drag the first,¬
upright, member to the Stage." & RETURN
set line3 to " (Example: Member 15 is an upright arrow. It is rotated¬
360d in the Paint Window and AutoDistort is used to make 8 more members.¬
Delete the duplicate upright arrow in slot 23. You now have 8 graphics in a¬
row, rotating clockwise.)" & RETURN
set line4 to " This behavior only affects the sprite's graphic, and so¬
other simultaneous behaviors can affect motion, or handle mouse events,¬
etc. It does not test for whether it is applied to a graphic." & RETURN
set line5 to " I hope the options are clear. I caught divide-by-zero¬
errors and think the math is okay. There can be a flip if y=0. I haven't¬
tested all options in all environments yet. Please let me know what I'm not¬
seeing, okay? "
return line1 & line2 & line3 & line4 & line5
end
|
|