Author Topic: Need Help with HTML/Javascript Simple Game Code  (Read 422 times)

So for my school computer science class I have to write a web page in HTML and Javascript, and decided to make a game where you explore different locations and collect the money there while trying to beat your record time.

My problem is with using cookies. I cannot seem to make them ever work, and don't even know how to use the js cookie library- nor do I have time this week to find out. I will also need to eventually figure out how to store the final time variable, check if it is a high score, and save it as a cookie. The current web page code is as follows:
Code: [Select]
<!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>
<title>
Treasure Hunter
</title>

<script type="text/javascript">
var c_time = 0
var c_coins = 0

function countUP () {
 c_time = c_time + 1;//increment the counter by 1
 //display the new value in the div
 document.getElementById("timer_container").innerHTML = "Seconds ellapsed: " + c_time;
 }

function incCoins () {
 c_coins = c_coins + 1;
 document.getElementById("coin_counter").innerHTML = "Coins collected: " + c_coins;
 }
function changeImg(img, nocoins) {
img.src = nocoins;
}

function setCookie(c_coins,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_coins + "=" + c_value;
}

function getCookie(c_coins)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_coins)
    {
    return unescape(y);
    }
  }
}

</script>
</head>

<body onload='timer=setInterval("countUP()", 1000 );'>
<img src="courtyardbackground.png">

<a href="garden.html">
  <img style="position:absolute; TOP:450px; LEFT:170px" src="courtyardgardensign.png">
</a>

<div onclick="incCoins()">
<img onclick="changeImg(this, 'nocoins.png')" style="position:absolute; TOP:550px; LEFT:470px" src="coins.png">
</div>

<div id="timer_container" style="position:absolute; TOP:780px; LEFT:50px">Seconds ellapsed: 0</div>
<div id="coin_counter" style="position:absolute; TOP:780px; LEFT:400px">Coins collected: 0</div>

</body>

</html>
Note that this code does not include my attempt at loading a cookie, but merely modifies the variables. Any help with this cookie business would be greatly appreciated.

No, you will not get a cookie for your help.

Have you looked at the w3schools page? http://www.w3schools.com/js/js_cookies.asp

EDIT: Now that I look it seems as though you copied part of it almost exactly.
« Last Edit: March 15, 2013, 09:21:23 PM by Doomonkey »

Have you looked at the w3schools page? http://www.w3schools.com/js/js_cookies.asp

EDIT: Now that I look it seems as though you copied part of it almost exactly.

Yes, I've looked at it, but I'm still having trouble figuring out how to transfer the cookies between web pages and load them again. Sorry, I should have specified that I'm using multiple pages.

What the hell when did you learn to code

don't use w3 schools. and i'm not basing my knowledge off of just this website, w3 has taught me a lot of bullstuff that annoyed me later on when i learned that it was actually bullstuff

on a more positive note, i think it'd be simpler if you used localStorage. the only problem i know of with this is that it may not stay on the user's computer as long as cookies do, but i'd say it's good enough for your purposes.

you could just do

    localStorage.setKey('coins',c_coins)

and get it later with

    var c_coins = parseInt(localStorage.getKey('coins'))

the "parseInt" is there to convert it from a string to a number. hope i helped.