Jump Sprite Behavior
– Make a sprite “jump” by moving it along a parabolic arc.
– Behavior by Ken Loge -
– 06-01-02
– Based on an algorithm by Dan Livingston -
– A variation of the standard parabola formula of y = x^2
– Reworked to be more versatile, the formula is:
– y = h - (2 * sqrt(h) x/d - sqrt(h))^2
– h = the height of the jump
– d = distance of the jump
property pJumpLeft — if true the sprite jumps to the left
property pJumpRight — if true the sprite jumps to the right
property pJumpDirection — stores the direction of each jump
property pConvexArc — sets the parabola type to convex
property pConcaveArc — sets the parabola type to concave
property pArcDirection — if 1, the arc is upward, if -1, the arc is downward
property pSqrootHeight — stores the square root of the height of the parabola
property pWidthFactor
property pXCounter — keeps track of jump iteration values
property pXHop
property pXIncrement — the “speed” of horizontal movement during a jump. Too big a number will “overshoot” the span.
property pYInitial — the original Y position of the sprite
property pYFactor1 — used to determine the sprite Y position during a jump
property pYFactor2
property pYFactorFinal
property pHeight — the height of the curve
property pDistance — the horizontal span of the curve. The smaller this number, the faster the sprite will move.
property pJump — used to trigger jump movement
on getBehaviorDescription me
return “Moves a sprite on a parabolic curve so it appears to jump in an arc.”
end
on beginSprite me
pSqrootHeight = sqrt(pHeight)
pWidthFactor = (2.0 * pSqrootHeight) / pDistance
pYInitial = sprite(me.spriteNum).locV
pXCounter = 0
— jumps will move to the right
if pJumpLeft = TRUE then pJumpDirection = -1
— jumps will move to the left
if pJumpRight = TRUE then pJumpDirection = 1
— set jumps to to the right if user checks both directions
if pJumpLeft AND pJumpRight = TRUE then pJumpDirection = 1
— parabolic arc will be convex
if pConvexArc = TRUE then pArcDirection = 1
— parabolic arc will be concave
if pConcaveArc = TRUE then pArcDirection = -1
— set to convex arc if user checks both
if pConvexArc AND pConcaveArc = TRUE then pArcDirection = 1
end
on getPropertyDescriptionList me
list = [:]
addProp list, #pHeight, [#comment: “Height of jump:”, #format: #integer, #default: 100, #range: [#min: 0, #max: 500]]
addProp list, #pDistance, [#comment: “Span of jump:”, #format: #integer, #default: 150, #range: [#min: 0, #max: 600]]
addProp list, #pXIncrement, [#comment: “X offset per iteration:”, #format: #integer, #default: 5, #range: [#min: 0, #max: 50]]
addProp list, #pJumpRight, [#comment: “Jump to the right:”, #format: #boolean, #default: TRUE]
addProp list, #pJumpLeft, [#comment: “Jump to the left:”, #format: #boolean, #default: FALSE]
addProp list, #pConvexArc, [#comment: “Convex arc:”, #format: #boolean, #default: TRUE]
addProp list, #pConcaveArc, [#comment: “Concave arc:”, #format: #boolean, #default: FALSE]
return list
end
on exitFrame me
if (pXCounter < = pDistance) AND pJump = TRUE then
— set jumping height
pYFactor1 = (pWidthFactor * pXCounter) - pSqrootHeight
pYFactor2 = power(pYFactor1, 2)
pYFactorFinal = (pHeight - pYFactor2) * pArcDirection
— pJumpDirection determines the direction of the jump
pXHop = pJumpDirection * pXIncrement
— move the sprite
sprite(me.spriteNum).locH = sprite(me.spriteNum).locH + pXHop
sprite(me.spriteNum).locV = pYInitial - pYFactorFinal
— prepare for next iteration
pXCounter = pXCounter + 5
else
— move sprite back to original Y position to correct for iteration offset
— sprite(me.spriteNum).locV = pYInitial
— reset so user can click the sprite again
pJump = FALSE
pXCounter = 0
end if
end
– as written, each time the mouse is clicked the ball “jumps”
on mouseUp me
— toggle jump state
if pJump = FALSE then
pJump = TRUE
else
pJump = FALSE
end if
end