1

I am currently making a Flappybird game in Javacript and right now I am implementing a cookie to store the highscore. But here is the problem, the cookie is always undefined and won't change.

So that nobody is confused, I should say that I use the Framework p5.js, which helps me to draw stuff. The code is on my gitHub repository (https://github.com/HaasStefan/challengeRepo/tree/master/FlappyBird). The main code is in the file named sketch.js, but here are some snippets:

First, this is where I initialize everything, and also the cookie:

function setup() {
createCanvas(400, 600);
bird = new Bird();
menu = new Menu();
pipes.push(new Pipe());

alert(navigator.cookieEnabled);

if (typeof (document.cookie == "undefined"))
  document.cookie = "highscore=0; expires=Sun, 1 Dec 2030 12:00:00 UTC; path=/";
}

Next we have the part, where the cookie is read and changed:

let str = document.cookie.split(';');
highscore = str[0].split('=')[1];
if (score > highscore) {
  highscore = score;
  document.cookie = "highscore=" + highscore + "; expires=Sun, 1 Dec 2030 12:00:00 UTC; path=/";
}

I really hope you can help me with this problem, because I have no idea what the bug is. Thank you!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Stefan
  • 1,122
  • 3
  • 14
  • 38
  • `if(typeof ...)` will always execute as typeof returns a string that is a truthy value – jro Apr 06 '19 at 14:00
  • @jro so, i should make a strict equal (===)? But this won't fix the problem – Stefan Apr 06 '19 at 14:07
  • You can use the code you have for getting the highscore then check if its undefined – jro Apr 06 '19 at 14:27
  • You have the parentheses wrong. It should be `if (typeof document.cookie == "undefined")` – Barmar Apr 06 '19 at 15:06
  • But that's the wrong way to tell whether you've set the cookie. There might be some other cookies, but not the `highscore` cookie. – Barmar Apr 06 '19 at 15:08
  • Guys, this doesn't help. And no, this question has not already been answered, because this is very specific. The cookie does not even set the value, even if I use the sugggested code from the other questions. and changing the paranthese didn't change a thing. @Barmar I only set the highscore cookie, so it must be the only cookie – Stefan Apr 06 '19 at 16:20
  • Can you please post a [mcve] that focus just on writing and then reading the cookie, without any of the extra code? – Kevin Workman Apr 06 '19 at 17:07
  • Your code to read the cookie doesn't look correct. You're never searching `str` for the entry that begins with `highscore=`. – Barmar Apr 06 '19 at 17:07
  • But I've reopened the question. – Barmar Apr 06 '19 at 17:07
  • `document.cookie` will never be undefined, it will be an empty string if no cookies have been set. Use the functions in the linked question to read and set your cookie. – Barmar Apr 06 '19 at 17:12

1 Answers1

1

As it was already mentioned in the comments, the first problem is with the if condition

if (typeof (document.cookie == "undefined"))

which must be written as

if (typeof document.cookie != "undefined")

The next problem is with reading the value from the 'highscore' cookie. Assuming there are several other cookies, the way to read its value should be:

var highscore = ('; '+document.cookie).split('; highscore=').pop().split(";").shift();

The result of the line above is of type string so you need to convert it to int before doing any comparisons.

highscore = parseInt(highscore)

To sum things up:

if (typeof document.cookie != "undefined") {
  document.cookie = "highscore=0; expires=Sun, 1 Dec 2030 12:00:00 UTC; path=/";
}

and then

let str = ('; '+document.cookie).split('; highscore=').pop().split(";").shift();
highscore = str ? parseInt(str) : 0;
if (score > highscore) {
  highscore = score;
  document.cookie = "highscore=" + highscore + "; expires=Sun, 1 Dec 2030 12:00:00 UTC; path=/";
}
Mihai
  • 371
  • 3
  • 14