Jump to content

checkbox help, please.


rahagnok
 Share

Recommended Posts

Hello everyone,I'm trying to make a simple web-based game. Thanks to some help from another topic, I have 5 images of dice that are randomly assigned a "face" when the user clicks the "roll" button. Now I need a little help with assigning a check box to each die that enables the user to pick and choose which die to keep and which to re-roll. Any help is hugely appreciated, thank you.

Link to comment
Share on other sites

Well so far I have been able to make five different images to appear. I also have a button called roll that causes the images to change randomly each time it is clicked. Now my main problem is figuring out how to make it so when the user wishes to keep one or more of the die, they can select it so it won't be "re-rolled". I was thinking that I could achieve this by either creating an onClick event , with the image being mapped, or by somehow making it when the check box value= checked, the assigned image won't be part of the roll function. I just can't figure out how to do this.Thanks for the help

Link to comment
Share on other sites

I'm thinking Yahtzee. Putting an onclick on the image would be cool. If the images are each in their own container (like a padded inline div), you can change the background color of the container to indicate that it's being kept, like a poker program does.<div style="padding: 5px; background-color: #FFFFFF; display: inline;"><img onclick="toggle_die(this, 1);" src=...></div>

var saved_dice = new Array();function toggle_die(el, id){  var parent_div = el.parentNode;  if (parent_div.style.backgroundColor == "#FFFFFF")  {	parent_div.style.backgroundColor = "#CC0000";	saved_dice.push(id);  }  else  {	parent_div.style.backgroundColor = "#FFFFFF";	foreach (var i in saved_dice)	  if (saved_dice[i] == id)		saved_dice.splice(i, 1);  }}

When you're rolling the dice you can loop through the saved_dice array and check if the ID number of the die is in there before rolling it. If the ID is in the array then the die is being saved.

Link to comment
Share on other sites

An alernate would be to have a different image for each die to be the "selected" state (like a black die with white spots, or the white die with a semi-transparent "saved" over it or something), and instead of changing the background color of the parent div you would change the src of the image.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...