|
What's New? |
|
Eposic JavaScript D6 Dice CodePart Three: Expanding Your Dice Rolling CapabilitiesOK, so now you've got a dice roller on your web site. Your visitors can roll dice all day long on your site, and that's a good thing. But if you're like me, you want to do something with those dice rolls. Let's make the numbers mean something! To make the dice rolling code trigger actions on your site, you register a callback with the roller. This is what's done in this example. If you want, go ahead and load this example file in your browser and roll the dice a few times to see what happens before we continue with our discussion...
This example file is set up to perform a simple attack roll,
determining by a simple test whether or not an attack against
an opponent is successful. For this test, I've hardcoded in
the value of 6 as the minimum dice roll result on two dice
to mean the opponent is hit; a total result of 5 or less
on the dice means the opponent was missed. For this example,
I use the Here's the code:
So let's discuss the pertinent code, shall we? Line 4 loads the Eposic Dice Roller code into the web page. Lines 9 through 14 are JavaScript code that defines our callback. A callback may accept up to three arguments; in this case, we only need the first one, which is the total result of the dice roll. That is, the argument that will be passed to our callback function by the dice roller is the sum of the individual die rolls. We thus only need to check the result against our required 'to-hit' value, which is what we're doing in line 10. If the result of the dice roll is less than 6, then line 11 is executed, telling us that we've missed the opponent. If the result is 6 or greater, the else statement on line 12 directs us to line 13, where we are told that we hit the opponent.
Line 15 is our call to the And that's it. But let's say that you want to do something a little more clever than just popping up an alert box. For instance, let's say you want to send your visitor to one web page if an attack succeeds, and to another page if the attack fails. You can do that by modifying the above callback. Here's some code that demonstrates one way of doing that:
The interesting code here is in lines 9 through 17. In line 9,
we define a global variable,
In line 13, we create a timer. This timer will delay for half a second
(500 milliseconds) and then call the
If you've already got some programming experience under
your belt, your head is probably reeling with ideas at this
point. With the ability to register callbacks on the dice roller,
the sky is the limit for someone that knows JavaScript. But, hey,
there's more still to see! There are more arguments available
to your callbacks, and there are more arguments available to
the Next up, we look at a more sophisticated example, where we implement a sample but functional combat system, in Part Four: A Sample, Functional Combat System. Part Four: A Sample, Functional Combat System --> <-- Part Two: Creating Your First Dice Roller Home | What's New? | RPG | Diversions | Source Code | Policy | Links | Contact
Copyright © 2006 by Michael K. Eidson Eposic is a servicemark of Michael K. Eidson The Trolf character is a trademark of Michael K. Eidson
|
Fantasy Anthology!
Space Opera RPG! |