...

View Full Version : Object Expected



Geodesic_D
06-14-2007, 02:23 PM
I am trying to write a small piece of code that will act as a die roller. That is to say, it generates two numbers, each between one and six, and changes the base image accordingly.

However, whenever I click the button that executes this code, I get an 'object expected' error on line 87 of my page.

But the code on line 87 is this:


<div><form id="form"><input type="button" value="Roll Dice" onClick="rolldice()">&nbsp;<input type="button" value="Reset Dice" onClick="dicereset()"></form></div>

Why is it doing this? It is starting to get rather irritating, as I see nothing wrong with my actual code.

Mr J
06-14-2007, 03:03 PM
Javascript is case sensitive, make sure rolldice() is spelt the same for the onclick and the function

Arty Effem
06-14-2007, 03:20 PM
However, whenever I click the button that executes this code, I get an 'object expected' error on line 87 of my page.

But the code on line 87 is this:


<div><form id="form"><input type="button" value="Roll Dice" onClick="rolldice()">&nbsp;<input type="button" value="Reset Dice" onClick="dicereset()"></form></div>
Why is it doing this? It is starting to get rather irritating, as I see nothing wrong with my actual code.
I presume we're talking about
http://www.curquhart.co.uk/wargame/dieroller.html

You should be getting an error previous to that, indicating the missing parentheses around your if condition.

Mr J
06-14-2007, 07:21 PM
And your random function

1 + (6-1)*Math.random()

would be better like this

Math.ceil(Math.random()*6)



<script language="JavaScript" type="text/javascript">
function rolldice(){

var die1 = Math.ceil(Math.random()*6)
var die2 = Math.ceil(Math.random()*6)

if(die1 == 1){document.image.die1.src='/wargame/images/dice/one.GIF'}
if(die1 == 2){document.image.die1.src='/wargame/images/dice/two.GIF'}
if(die1 == 3){document.image.die1.src='/wargame/images/dice/three.GIF'}
if(die1 == 4){document.image.die1.src='/wargame/images/dice/four.GIF'}
if(die1 == 5){document.image.die1.src='/wargame/images/dice/five.GIF'}
if(die1 == 6){document.image.die1.src='/wargame/images/dice/six.GIF'}

if(die2 == 1){document.image.die2.src='/wargame/images/dice/one.GIF'}
if(die2 == 2){document.image.die2.src='/wargame/images/dice/two.GIF'}
if(die2 == 3){document.image.die2.src='/wargame/images/dice/three.GIF'}
if(die2 == 4){document.image.die2.src='/wargame/images/dice/four.GIF'}
if(die2 == 5){document.image.die2.src='/wargame/images/dice/five.GIF'}
if(die2 == 6){document.image.die2.src='/wargame/images/dice/six.GIF'}
}

function dicereset(){
document.image.die1.src='/wargame/images/dice/one.GIF'
document.image.die2.src='/wargame/images/dice/one.GIF'
}
</script>

Geodesic_D
06-14-2007, 08:32 PM
Ah, I knew I was forgetting something.

Anyway, it works now - thanks for the help!

glenngv
06-14-2007, 08:41 PM
The code could be simplified to:

var imgs = new Array("one.gif", "two.gif", "three.gif", "four.gif", "five.gif", "six.gif");

function rolldice(){
var die1 = Math.ceil(Math.random()*6);
var die2 = Math.ceil(Math.random()*6);

document.images["die1"].src = "/wargame/images/dice/" + imgs[die1 - 1];
document.images["die2"].src = "/wargame/images/dice/" + imgs[die2 - 1];
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum