Bubbel
11-10-2010, 12:01 PM
Hi there!
I need some help developing a dice game. I'm totally new at this so a bit confused with the javascript language... Anyways, heres my code:
/.../
<script type="text/javascript" language="javascript">
var diceValue1 = 1;
var diceValue2 = 1;
var diceValue3 = 1;
function showDice(imgNr, dice) {
for (var i=0; i<=5; i++)
{
if (imgNr == 1) var imgUrl = "1.gif";
else if (imgNr == 2) var imgUrl = "2.gif";
else if (imgNr == 3) var imgUrl = "3.gif";
else if (imgNr == 4) var imgUrl = "4.gif";
else if (imgNr == 5) var imgUrl = "5.gif";
else if (imgNr == 6) var imgUrl = "6.gif";
document.getElementById("dice" +i).src = imgUrl;
}
}
function randomInteger(high) {
var randNumber = Math.random();
var randNumber = Math.floor(high*randNumber)+1;
return randNumber;
}
function showRandomPict(dice) {
var imgNr = randomInteger(6);
showDice(imgNr, dice);
return imgNr;
}
function rollAll(pictNr) {
diceValue1 = showRandomPict(1);
diceValue2 = showRandomPict(2);
diceValue3 = showRandomPict(3);
}
function changePict(bigDice) {
if (bigDice) document.bigD.src = "tarning.jpg";
else document.bigD.src = "tarning1.jpg";
}
</script>
</head>
<body>
<h3>Tärningskastning</h3>
<div id="pics">
<p>
<img id="dice1" src="1.gif" alt="tarning1" width=60px onclick="showRandomPict(1)" />
<img id="dice2" src="2.gif" alt="tarning2" width=60px onclick="showRandomPict(2)" />
<img id="dice3" src="3.gif" alt="tarning3" width=60px onclick="showRandomPict(3)" />
</p>
</div>
<div id= "BigDice"
<p><img name="bigD" src="tarning.jpg" alt="stor tärning" width=80px onmouseover="changePict(false)
"onmouseout="changePict(true)" onclick="rollAll()" /><br /></p>
</div>
</body>
</html>
So what i need help with is my pictures. They wont show. Firebug says: "document.getElementById("dice" + i) is null", and I have tried to solve that but with no progress...
So what should i do??
Next step is to show the sums of the dices on the screen and also save this sums to be able to show them later on in an alert window... But that is for later, right now the dices needs to show the right (random) sides...
Help anyone?
I need some help developing a dice game. I'm totally new at this so a bit confused with the javascript language... Anyways, heres my code:
/.../
<script type="text/javascript" language="javascript">
var diceValue1 = 1;
var diceValue2 = 1;
var diceValue3 = 1;
function showDice(imgNr, dice) {
for (var i=0; i<=5; i++)
{
if (imgNr == 1) var imgUrl = "1.gif";
else if (imgNr == 2) var imgUrl = "2.gif";
else if (imgNr == 3) var imgUrl = "3.gif";
else if (imgNr == 4) var imgUrl = "4.gif";
else if (imgNr == 5) var imgUrl = "5.gif";
else if (imgNr == 6) var imgUrl = "6.gif";
document.getElementById("dice" +i).src = imgUrl;
}
}
function randomInteger(high) {
var randNumber = Math.random();
var randNumber = Math.floor(high*randNumber)+1;
return randNumber;
}
function showRandomPict(dice) {
var imgNr = randomInteger(6);
showDice(imgNr, dice);
return imgNr;
}
function rollAll(pictNr) {
diceValue1 = showRandomPict(1);
diceValue2 = showRandomPict(2);
diceValue3 = showRandomPict(3);
}
function changePict(bigDice) {
if (bigDice) document.bigD.src = "tarning.jpg";
else document.bigD.src = "tarning1.jpg";
}
</script>
</head>
<body>
<h3>Tärningskastning</h3>
<div id="pics">
<p>
<img id="dice1" src="1.gif" alt="tarning1" width=60px onclick="showRandomPict(1)" />
<img id="dice2" src="2.gif" alt="tarning2" width=60px onclick="showRandomPict(2)" />
<img id="dice3" src="3.gif" alt="tarning3" width=60px onclick="showRandomPict(3)" />
</p>
</div>
<div id= "BigDice"
<p><img name="bigD" src="tarning.jpg" alt="stor tärning" width=80px onmouseover="changePict(false)
"onmouseout="changePict(true)" onclick="rollAll()" /><br /></p>
</div>
</body>
</html>
So what i need help with is my pictures. They wont show. Firebug says: "document.getElementById("dice" + i) is null", and I have tried to solve that but with no progress...
So what should i do??
Next step is to show the sums of the dices on the screen and also save this sums to be able to show them later on in an alert window... But that is for later, right now the dices needs to show the right (random) sides...
Help anyone?