Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Object Expected

  1. #1
    Regular Coder
    Join Date
    Nov 2004
    Location
    Perth, UK
    Posts
    105
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Object Expected

    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:

    Code:
    <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.
    Geodesic_D
    "I'd much rather be called GeodesicDragon."

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Javascript is case sensitive, make sure rolldice() is spelt the same for the onclick and the function
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Geodesic_D View Post

    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:

    Code:
    <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.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    And your random function

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

    would be better like this

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


    Code:
    <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>
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    Regular Coder
    Join Date
    Nov 2004
    Location
    Perth, UK
    Posts
    105
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah, I knew I was forgetting something.

    Anyway, it works now - thanks for the help!
    Geodesic_D
    "I'd much rather be called GeodesicDragon."

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    The code could be simplified to:
    Code:
    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];
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •