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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Need help with Buttons

    Hello All, I'm trying to put together a little story adventure game, except when I test my code the first button doesn't work. I've been trying to figure out why for a while and I can't seem to figure out the problem. I really appreciate any help you can give me. Thanks in advance.

    My JS code:
    Code:
    // JavaScript Document
    
    var story=[];
    story[0]=new Object ();
    story[0].text="You arrive at the abandoned asylum and walk into the woods behind it.";
    story[0].buttonA="Next";
    story[0].buttonB="Next";
    story[0].DestinationA=1;
    story[0].DestinationB=1;
    story[0].isHiddenA=false;
    story[0].isHiddenB=true;
    
    story[1]=new Object ();
    story[1].text="You continue to walk until a fork in the path comes up. Which direction should you go?";
    story[1].buttonA="Left";
    story[1].buttonB="Right";
    story[1].DestinationA=2;
    story[1].DestinationB=6;
    story[1].isHiddenA=false;
    story[1].isHiddenB=false;
    
    story[2]=new Object ();
    story[2].text="You walk for what seems like two miles and now it's dark. You hear footsteps beind you and quickey spin around. Standing 20 feet behind you is Slender Man and you freeze like a deer in headlights from shock.";
    story[2].buttonA="Next";
    story[2].buttonB="Next";
    story[2].DestinationA=3;
    story[2].DestinationB=3;
    story[2].isHiddenA=false;
    story[2].isHiddenB=true;
    
    story[3]=new Object ();
    story[3].text="After an eternity (really just three seconds) it's time you make a decision.";
    story[3].buttonA="Run";
    story[3].buttonB="Hide";
    story[3].DestinationA=4;
    story[3].DestinationB=5;
    story[3].isHiddenA=false;
    stoey[3].isHiddenB=false;
    
    story[4]=new Object ();
    story[4].text="You sprint back to the safety of your car, get in and start to drive home. Good job you think to yourself but then something catches your eye from the corner of the rearview mirror. It's Slender Man in the back seat of your car. The police never find your body.";
    story[4].buttonA="Next";
    story[4].buttonB="Next";
    story[4].DestinationA=0;
    story[4].DestinationB=0;
    story[4].isHiddenA=false;
    story[4].isHiddenB=true;
    
    story[5]=new Object ();
    story[5].text="You sprint a little ways and decide hiding is your best option for survival and you veer off the path to hide behind a huge oak tree. After some time you see Slender Man walking down the path and you lose sight of him as he passes in front of your tree. You then feel the pressure of a hand pressing down on your shoulder. It's the last feeling you ever have.";
    story[5].buttonA="Next";
    story[5].buttonB="Next";
    story[5].DestinationA=0;
    story[5].DestinationB=0;
    story[5].isHiddenA=false;
    story[5].isHiddenB=true;
    
    story[6]=new Object ();
    story[6].text="You walk for what seems like two miles and now it's dark. You hear footsteps beind you and quickey spin around. Standing 20 feet behind you is a hobo that looks like Santa Claus. After you stare at him with a confused/scared expression on your face for a few seconds he asks in the creepiest voice you've ever heard if you want some candy.";
    story[6].buttonA="Next";
    story[6].buttonB="Next";
    story[6].DestinationA=7;
    story[6].DestinationB=7;
    story[6].isHiddenA=false;
    story[6].isHiddenB=true;
    
    story[7]=new Object ();
    story[7].text="FREE CANDY!?";
    story[7].buttonA="YES, GIVE ME ALL THE CANDY!";
    story[7].buttonB="NO >:(";
    story[7].DestinationA=8;
    story[7].DestinationB=9;
    story[7].isHiddenA=false;
    story[7].isHiddenB=false;
    
    story[8]=new Object ();
    story[8].text="The Santa hobo give you a bag containing five pounds of king-size candy bars. Your eyes begin to water out of pure happiness And you look up to thank the hobo but he disappears into a cloud of pink dust. Awesome, now you won't feel bad for not sharing.";
    story[8].buttonA="Next";
    story[8].buttonB="Next";
    story[8].DestinationA=0;
    story[8].DestinationB=0;
    story[8].isHiddenA=false;
    story[8].isHiddenB=true;
    
    story[9]=new Object ();
    story[9].text="The Santa hobo tells you it's your loss and as he walks away you notice the massive bag of king-size candy he's carrying. You can't believe you just missed out on pretty much the greatest thing since the invention of the candy bar itself. You go walk to your car, go home and cry.";
    story[9].buttonA="next";
    story[9].buttonB="next";
    story[9].DestinationA=0;
    story[9].DestinationB=0;
    story[9].isHiddenA=false;
    story[9].isHiddenB=true;
    
    var page = 0;
    function OnPress (buttonPressed)
    {
    	if (buttonPressed =='A')
    		page = story[page].DestinationA;
    	else if (buttonPressed =='B')
    		page = story[page].DestinationB;
    	
    	document.getElementById("story").innerHTML = story[page].text;
    	page=0;
    	
    document.getElementsByName("NavGameA")[0].value=story[page].buttonA;
    document.getElementsByName("NavGameB")[0].value=story[page].buttonB;
    
    document.getElementsByName("NavGameA")[0].hidden=story[page].isHiddenA;
    document.getElementsByName("NavGameB")[0].hidden=story[page].isHiddenB;
    }
    and the source code:
    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <link href="style.css" rel="stylesheet" type="text/css">
    <script src="game.js"></script>
    </head>
    
    <body>
    <p id="story">To the Woods!</p>
    <input name="NavGameA" type="button" value="Next" Onclick="OnPress('A');"><br>
    <input name="NavGameB" type="button" value="Next" Onclick="OnPress('B')"hidden=true";"><br>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,519
    Thanks
    3
    Thanked 507 Times in 494 Posts
    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <link href="style.css" rel="stylesheet" type="text/css">
    <script src="game.js"></script>
    <script  type="text/javascript">
    /*<![CDATA[*/
    
    
    var story=[];
    story[0]=new Object ();
    story[0].text="You arrive at the abandoned asylum and walk into the woods behind it.";
    story[0].buttonA="Next";
    story[0].buttonB="Next";
    story[0].DestinationA=1;
    story[0].DestinationB=1;
    story[0].isHiddenA=false;
    story[0].isHiddenB=true;
    
    story[1]=new Object ();
    story[1].text="You continue to walk until a fork in the path comes up. Which direction should you go?";
    story[1].buttonA="Left";
    story[1].buttonB="Right";
    story[1].DestinationA=2;
    story[1].DestinationB=6;
    story[1].isHiddenA=false;
    story[1].isHiddenB=false;
    
    story[2]=new Object ();
    story[2].text="You walk for what seems like two miles and now it's dark. You hear footsteps beind you and quickey spin around. Standing 20 feet behind you is Slender Man and you freeze like a deer in headlights from shock.";
    story[2].buttonA="Next";
    story[2].buttonB="Next";
    story[2].DestinationA=3;
    story[2].DestinationB=3;
    story[2].isHiddenA=false;
    story[2].isHiddenB=true;
    
    story[3]=new Object ();
    story[3].text="After an eternity (really just three seconds) it's time you make a decision.";
    story[3].buttonA="Run";
    story[3].buttonB="Hide";
    story[3].DestinationA=4;
    story[3].DestinationB=5;
    story[3].isHiddenA=false;
    story[3].isHiddenB=false;
    
    story[4]=new Object ();
    story[4].text="You sprint back to the safety of your car, get in and start to drive home. Good job you think to yourself but then something catches your eye from the corner of the rearview mirror. It's Slender Man in the back seat of your car. The police never find your body.";
    story[4].buttonA="Next";
    story[4].buttonB="Next";
    story[4].DestinationA=0;
    story[4].DestinationB=0;
    story[4].isHiddenA=false;
    story[4].isHiddenB=true;
    
    story[5]=new Object ();
    story[5].text="You sprint a little ways and decide hiding is your best option for survival and you veer off the path to hide behind a huge oak tree. After some time you see Slender Man walking down the path and you lose sight of him as he passes in front of your tree. You then feel the pressure of a hand pressing down on your shoulder. It's the last feeling you ever have.";
    story[5].buttonA="Next";
    story[5].buttonB="Next";
    story[5].DestinationA=0;
    story[5].DestinationB=0;
    story[5].isHiddenA=false;
    story[5].isHiddenB=true;
    
    story[6]=new Object ();
    story[6].text="You walk for what seems like two miles and now it's dark. You hear footsteps beind you and quickey spin around. Standing 20 feet behind you is a hobo that looks like Santa Claus. After you stare at him with a confused/scared expression on your face for a few seconds he asks in the creepiest voice you've ever heard if you want some candy.";
    story[6].buttonA="Next";
    story[6].buttonB="Next";
    story[6].DestinationA=7;
    story[6].DestinationB=7;
    story[6].isHiddenA=false;
    story[6].isHiddenB=true;
    
    story[7]=new Object ();
    story[7].text="FREE CANDY!?";
    story[7].buttonA="YES, GIVE ME ALL THE CANDY!";
    story[7].buttonB="NO >:(";
    story[7].DestinationA=8;
    story[7].DestinationB=9;
    story[7].isHiddenA=false;
    story[7].isHiddenB=false;
    
    story[8]=new Object ();
    story[8].text="The Santa hobo give you a bag containing five pounds of king-size candy bars. Your eyes begin to water out of pure happiness And you look up to thank the hobo but he disappears into a cloud of pink dust. Awesome, now you won't feel bad for not sharing.";
    story[8].buttonA="Next";
    story[8].buttonB="Next";
    story[8].DestinationA=0;
    story[8].DestinationB=0;
    story[8].isHiddenA=false;
    story[8].isHiddenB=true;
    
    story[9]=new Object ();
    story[9].text="The Santa hobo tells you it's your loss and as he walks away you notice the massive bag of king-size candy he's carrying. You can't believe you just missed out on pretty much the greatest thing since the invention of the candy bar itself. You go walk to your car, go home and cry.";
    story[9].buttonA="next";
    story[9].buttonB="next";
    story[9].DestinationA=0;
    story[9].DestinationB=0;
    story[9].isHiddenA=false;
    story[9].isHiddenB=true;
    
    var page = 0;
    function OnPress (buttonPressed)
    {
    	if (buttonPressed =='A')
    		page = story[page].DestinationA;
    	else if (buttonPressed =='B')
    		page = story[page].DestinationB;
    
    	document.getElementById("story").innerHTML = story[page].text;
    //	page=0;
    
    document.getElementsByName("NavGameA")[0].value=story[page].buttonA;
    document.getElementsByName("NavGameB")[0].value=story[page].buttonB;
    
    document.getElementsByName("NavGameA")[0].hidden=story[page].isHiddenA;
    document.getElementsByName("NavGameB")[0].hidden=story[page].isHiddenB;
    }/*]]>*/
    </script></head>
    
    <body>
    <p id="story">To the Woods!</p>
    <input name="NavGameA" type="button" value="Next" Onclick="OnPress('A');"><br>
    <input name="NavGameB" type="button" value="Next" Onclick="OnPress('B')"hidden=true";"><br>
    </body>
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Tags for this Thread

    Posting Permissions

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