Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-17-2012, 03:01 AM   PM User | #1
java-junkie
New to the CF scene

 
Join Date: Mar 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
java-junkie is an unknown quantity at this point
Combination Script for game not working

I am very new to javascript. At least when it comes to writing scripts. The only thing I'm trying to do with the following script is make it so when you click on the right numbers of the image it will take you to a new page. If you click the wrong numbers it will forward you to an error page explaining that the code/numbers you clicked on were wrong. The code of numbers is four numbers long.

When I try the correct numbers or the incorrect numbers nothing happens either way. I'm not sure why.

Code:
<html><head><title>Untitled</title>
<script type="text/javascript">

			var combination;
			var comboAnswer = "7256";

			combination = "";

			function keycode(n) {
		if (combination.length < (opener.top.comboAnswer.length - 1)) {
					combination = combination + n;
				}
				else {
					combination = combination + n;
					checkCode();
				}
			}

			function checkCode() {
				if (combination == opener.top.comboAnswer) {
					window.location.href = "disarmed.html";
				}
				else {
					window.location.href = "armed.html";
				}
			}

		</script>
<body bgcolor="#000000">
<center>
<div style="text-align:center; width:527px; margin-left:auto; margin-right:auto;">
<img id="combination" src="image.png" usemap="#combination" border="0" alt="" />
<map id="_combination" name="combination">
<area shape="rect" coords="255,184,288,210" href="javascript:keycode(1)" alt="" title="" />
<area shape="rect" coords="288,184,321,210" href="javascript:keycode(2)" alt="" title="" />
<area shape="rect" coords="321,184,354,210" href="javascript:keycode(3)" alt="" title="" />
<area shape="rect" coords="255,213,288,239" href="javascript:keycode(4)" alt="" title="" />
<area shape="rect" coords="288,213,321,239" href="javascript:keycode(5)" alt="" title="" />
<area shape="rect" coords="320,213,353,239" href="javascript:keycode(6)" alt="" title="" />
<area shape="rect" coords="256,240,289,266" href="javascript:keycode(7)" alt="" title="" />
<area shape="rect" coords="288,240,321,266" href="javascript:keycode(8)" alt="" title="" />
<area shape="rect" coords="320,240,353,266" href="javascript:keycode(9)" alt="" title="" />
<area shape="rect" coords="288,265,321,291" href="javascript:keycode(0)" alt="" title="" />
<area shape="default" nohref>
</map>
</div>
</center>
</body></html>

Last edited by java-junkie; 03-17-2012 at 04:06 AM..
java-junkie is offline   Reply With Quote
Old 03-17-2012, 03:16 AM   PM User | #2
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
Can you post/attach the actual .png (image.png) you are using. It'll make it easier to run your actual code or post an alternative code solution.

And why are you referring to opener.top.comboAnswer.length when you have var comboAnswer = "7256"; earlier. What is the difference between the two?

Last edited by webdev1958; 03-17-2012 at 03:19 AM..
webdev1958 is offline   Reply With Quote
Old 03-17-2012, 03:37 AM   PM User | #3
java-junkie
New to the CF scene

 
Join Date: Mar 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
java-junkie is an unknown quantity at this point
Quote:
Originally Posted by webdev1958 View Post
Can you post/attach the actual .png (image.png) you are using. It'll make it easier to run your actual code or post an alternative code solution.

And why are you referring to opener.top.comboAnswer.length when you have var comboAnswer = "7256"; earlier. What is the difference between the two?


The comboAnswer variable is the correct code a visitor would have to enter. This actually isn't suppose to be visible in the code in case someone viewed the source. I was going to link that variable in a separate script, but I thought it was the problem. So I moved it to the present code, but it's still not working. As for "length" it's so it knows the length of the code.
java-junkie is offline   Reply With Quote
Old 03-17-2012, 03:54 AM   PM User | #4
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Are you getting any errors in the console?

Your reference to opener.top indicates that this code has to run in a child window, so what does var comboAnswer = "7256"; do in the current window?
Logic Ali is offline   Reply With Quote
Users who have thanked Logic Ali for this post:
java-junkie (03-17-2012)
Old 03-17-2012, 03:57 AM   PM User | #5
java-junkie
New to the CF scene

 
Join Date: Mar 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
java-junkie is an unknown quantity at this point
Quote:
Originally Posted by Logic Ali View Post
Are you getting any errors in the console?

Your reference to opener.top indicates that this code has to run in a child window, so what does var comboAnswer = "7256"; do in the current window?
Okay I removed opener.top from both parts of the code and now the script works. Thank you!
java-junkie is offline   Reply With Quote
Old 03-17-2012, 04:08 AM   PM User | #6
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
Hopefully this standalone demo, using your .png image map will help.

The user clicks 4 numbers. After the 4th number is clicked the user entered 4 digit number is checked against the "hard wired" correct answer and then redirects to the appropriate url

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <title></title>
        <style type="text/css"></style>
        <script type="text/javascript">
            var correctCode = 6471;
            var curGuess = '', count = 0;
            function keycode(num) {
                if(++count < 4){ //continue building the guessed code string
                    curGuess += num;
                } else { //check if entered 4 digit code string is correct
                    curGuess += num;
                    var redirectURL = (Number(curGuess) == correctCode)? 'url1':'url2';
                    window.location.href = redirectURL;
                }
            }
            function startAgain(){
                curGuess = '';
                count = 0;
            }
            window.onload=function(){
                document.getElementById('btnClear').onclick=startAgain;
            }
        </script>
    </head>
    <body>
        <img id="imgKeypad" src="image.png" usemap="#combination" border="0" alt="" />
        <div>
            <button id="btnClear">Try again</button>
        </div>
        <map id="combination" name="combination">
            <area shape="rect" coords="255,184,288,210" onclick="keycode(1);return false;" alt="" title="" href=""/>
            <area shape="rect" coords="288,184,321,210" onclick="keycode(2);return false;" alt="" title="" href=""/>
            <area shape="rect" coords="321,184,354,210" onclick="keycode(3);return false;" alt="" title="" href=""/>
            <area shape="rect" coords="255,213,288,239" onclick="keycode(4);return false;" alt="" title="" href=""/>
            <area shape="rect" coords="288,213,321,239" onclick="keycode(5);return false;" alt="" title="" href=""/>
            <area shape="rect" coords="320,213,353,239" onclick="keycode(6);return false;" alt="" title="" href=""/>
            <area shape="rect" coords="256,240,289,266" onclick="keycode(7);return false;" alt="" title="" href=""/>
            <area shape="rect" coords="288,240,321,266" onclick="keycode(8);return false;" alt="" title="" href=""/>
            <area shape="rect" coords="320,240,353,266" onclick="keycode(9);return false;" alt="" title="" href=""/>
            <area shape="rect" coords="288,265,321,291" onclick="keycode(0);return false;" alt="" title="" href=""/>
        </map>
    </body>
</html>

Last edited by webdev1958; 03-17-2012 at 04:27 AM..
webdev1958 is offline   Reply With Quote
Users who have thanked webdev1958 for this post:
java-junkie (03-17-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:25 PM.


Advertisement
Log in to turn off these ads.