First lets look at what a status bar does. We have a maximum value for an "energy bar" and then a current value to compare it to. The width of the bar will be equal to the maximum width times the percentage of the max value we currently have. Something like this...
So, by using this, a few "if" statements and a little lingo, we can build an all purpose behavior that can make a status bar for virtually any value! The first thing we need to do is set up our getPropertyDescriptionList handler so that the user can define what value to track, the maximum for that value, and the direction it should "grow". Using the "value" command we can take this string the user inputs and compare it. This can be a variable, property, or most anything that has a numeric value.
The next thing we will need is a handler to evaluate and move the bar to the appropriate coordinates. First we get the current value and the max value and verify that they are both numbers. From there we make sure that the current value is not larger than the max value and not smaller than zero. Then we divide the two to get our current percentage. If the bar is moving up or down we change the height, and if it is left or right we modify the width. Then using the original rectangle (which we get in the beginSprite handler) the new rectangle is set. Take a look at the completed behavior below.
--Copyright 1999 Chuck Neal
--chuck@mediamacros.com
--If you find this code helpful, send me an e-mail and let me know. :-)
on getPropertyDescriptionList me
p_list = [:]
p_list.addProp(#monitorValue, [#format : #string, #comment : "What value to compare:", #default : "the timer / 60"])
p_list.addProp(#maxValue, [#format : #string, #comment : "Max value allowed:", #default : "500"])
p_list.addProp(#constant, [#format : #boolean, #comment : "Run automatically?", #default : true])
p_list.addProp(#direction, [#format : #symbol, #comment : "Direction to grow:", #default : #right, #range : [#right, #left, #up, #down]])
return p_list
end
on beginSprite me
maxRect = sprite(spriteNum).rect
calcSize()
end
on exitFrame me
calcSize()
end
on calcSize me
curValue = value(monitorValue)
topVal = value(maxValue)
if [#integer, #float].getOne(curValue.ilk) <> 0 and [#integer, #float].getOne(topVal.ilk) <> 0 then
--is a number, continue
if curValue > maxvalue then curValue = topVal
if curValue < 0 then curValue = 0
percentage = float(curValue)/float(topVal)
newRect = duplicate(maxRect)
case direction of
#right :
newRect[3] = maxRect[1] + integer(maxRect.width * percentage)
#left :
newRect[1] = maxRect[3] - integer(maxRect.width * percentage)
#up :
newRect[2] = maxRect[4] - integer(maxRect.height * percentage)
#down :
newRect[4] = maxRect[2] + integer(maxRect.height * percentage)
end case
sprite(spriteNum).rect = newRect
else
--not a number
put "Error, returned value is not a number"
end if
end
on getBehaviorDescription me
describe = "This is an all purpose bar slider. Drop it on the sprite that will be the indicator and assign it the values it will use to compare the current bar."
describe = describe & return & "For example, to track a sprite 5's property called life against a max of 500, the first field would be entered as ..." & return
describe = describe & "sprite(5).life and the second field would be 500."
return describe
end
This can be used as is with most any stored value and you can either let it run automatically or update only when you send the command to it. Play with it and see what you come up with.
Contact
MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA