Articles Archive
Articles Search
Director Wiki
 

Progress Bar Penalty/Reward

May 29, 2000
by Pat McClellan

Dear Multimedia Handyman,

I absolutely love the progress bar! I'm making a game that will use this script, but I'm looking to modify the script so that when a sprite intersects another sprite (i.e. power ups, etc.) the progress bar goes up by say 15%, or whatever. Can ya' help?

Ross Lemenille

Dear Ross,

You've got the right idea: take existing scripts and modify them for your own needs. And that's a great way to learn from the work of others. Speaking of the work of others, I'll refer you to Gary Rosenzweig's great series of Lingo Lounge articles about sprite collision detection. I'll bet it'll come in handy for your game.

Let's take a quick look back at the progress bar behavior. The concept there is that we scale a progress bar graphic -- either horizontal or vertical -- based on the percentage of time that has elapsed. So how do we know what percentage of time has elapsed?

When you drop the behavior on the sprite, you specify the total time (in seconds) that the progress bar will take. Next, in the beginSprite handler, we convert this time to ticks and store it in a property called pTotalTicks. We also set pStartTicks equal to the ticks. After that, we simply check each time we enter the frame to see how many ticks have elapsed and scale the sprite accordingly. For example:

timeElapsed = the timer - pStartTicks
percentTime = (timeElapsed * 1.0000/pTotalTicks)
pSprite.width = pWidth - (percentTime * pWidth)

Now we need to figure out where's the best place to change these calculations so that the player looses time. I think the easiest thing to do is to alter the value of pTotalTicks. And since you may want to reward the player with more time as well, we should make the changes to the behavior so that you can add or subtract time.

Here's a demo:

D7 download for Mac or Windows.

For this demo, I simply added a new handler to the previous behavior. The new handler does nothing except add (or subtract) ticks from pTotalTicks.

on changeTicks me, deltaTicks
  pTotalTicks = pTotalTicks + deltaTicks
end changeTicks

To activate it, you'll need to issue a command such as the following:

sendSprite(2,#changeTicks,300)

In this example, the progress bar is in sprite 2, so the sendSprite() command goes to sprite 2, issues the #changeTicks command, and supplies the value of 300 (ticks) as the change value. This command would add five seconds to the pTotalTicks, so in this case, it's a reward. To penalize the player 10 seconds, the command would be...

sendSprite(2,#changeTicks,-600)

If you want to have the penalty/reward calculated as a percentage instead of a specific number of ticks, you could change the changeTicks handler as follows:

on changeTicks me, percentTicks
  pTotalTicks = pTotalTicks * percentTicks
end changeTicks

Then, you'll need to supply the percentTicks like this: for a 10% penalty...

sendSprite(2,#changeTicks,.90)

And for a 10% reward:

sendSprite(2,#changeTicks,1.10)

It's pretty easy and you'll see that the effect is displayed immediately on the progress bar. Good luck with your program.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.