keeping score

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

keeping score

Post by ethanCodes » Wed Feb 24, 2016 2:42 am

I am trying to make this simple program keep score. The user guesses a number between 0-15, and if the guess is correct, then add 2 points, incorrect, take 1 point away. The GUI has a submit button, which all the code is written through, an instruction label, a text box for user input, a "score" label and a score text box starting at 0. I'm not sure why, but if I guess incorrect, it goes down to -1, but won't go further into the negative? I'm not sure why. Here is the code I have so far.

Code: Select all

on mouseUp
   put the text of fld "fld_guess" into theGuess
   
   put 0 into score --adds the fun of keeping score to the game!
   
   set the randomSeed to the long seconds
   put random(15) into theAnswer --changed the range to make it easier, and somewhat less frustrating
   if theGuess is theAnswer then
      answer "Correct"
      add 2 to score --adds 1 point to score if answer is correct
   else
      answer "Wrong! The correct answer was " & theAnswer & "."
     add -1 to score --subtracts 1 point from score if answer is incorrect
   end if
   put empty into fld "fld_guess" 
   
   put score into field "scoreField" --displays the current score
end mouseUp


Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: keeping score

Post by Simon » Wed Feb 24, 2016 3:34 am

ooops

Code: Select all

 put 0 into score --adds the fun of keeping score to the game!
Take a look at where that is again :)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: keeping score

Post by dunbarx » Wed Feb 24, 2016 3:45 am

Hi.

What Simon said.

But did you step through the handler when you were scratching your head? This would have given you the reason.

Craig Newman

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: keeping score

Post by ethanCodes » Wed Feb 24, 2016 3:52 am

Thank you both, how do I step through a handler?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: keeping score

Post by Simon » Wed Feb 24, 2016 3:54 am

I guess we should add, look-up "breakpoint" in the dictionary.
And where is a good description of the little red dots?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: keeping score

Post by ethanCodes » Wed Feb 24, 2016 3:57 am

I know how the dots work and breakpoints. I will look into that. It still is only going to -1.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: keeping score

Post by Simon » Wed Feb 24, 2016 4:01 am

add "breakpoint" before putting 0 into score.

Oh... and I bet it doesn't go above 2 :)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: keeping score

Post by ethanCodes » Thu Feb 25, 2016 3:28 am

So I guess I'm not getting this. I put breakpoints in but it doesn't show me anything? Clearly the program is replacing the 0 with 2 or -1, which makes me think that 'add' is not the right keyword to use, but I don't know what else you would use? This language is so screwy. If i'm adding 2 to 2, why is score not 4? If I'm adding -1 to 2, why is score not 1? This doesn't make sense.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: keeping score

Post by Simon » Thu Feb 25, 2016 3:38 am

Ok I'll just give it to you.

Code: Select all

put 0 into score --adds the fun of keeping score to the game!
Do you see every time you go through that bit of code it ALWAYS puts 0 into score. Even if you had -1 in it before you just cleared it out.

Do you see it now?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: keeping score

Post by ethanCodes » Thu Feb 25, 2016 4:20 am

right but that is in the on openCard section. I thought that would mean it would only get set when the card opens?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: keeping score

Post by Simon » Thu Feb 25, 2016 4:35 am

It's in an on mouseUp not openCard.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: keeping score

Post by ethanCodes » Thu Feb 25, 2016 4:41 am

Oh I'm sorry, I already changed that. Here is my updated code:

Code: Select all

on mouseUp
   put the text of fld "fld_guess" into theGuess
   
   set the randomSeed to the long seconds
   put random(15) into theAnswer --changed the range to make it easier, and somewhat less frustrating
   if theGuess is theAnswer then
      answer "Correct"
      add 2 to score --adds 1 point to score if answer is correct
   else
      answer "Wrong! The correct answer was " & theAnswer & "."
     add -1 to score --subtracts 1 point from score if answer is incorrect
   end if
   put empty into fld "fld_guess" 
   
   put score into field "scoreField" --displays the current score
end mouseUp
and here is the on cardOpen code attached to the card:

Code: Select all

on openCard

   put 0 into score --adds the fun of keeping score!

   put 0 into field "scoreField"

end openCard

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: keeping score

Post by Simon » Thu Feb 25, 2016 4:51 am

Well that changes a lot of things!
You just need a "local score" above "on mouseUp"

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: keeping score

Post by ethanCodes » Thu Feb 25, 2016 5:00 am

I see...does that link the local score with the score initialized to 0 in the on openCard? or do I even need to initialize that score on on openCard?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: keeping score

Post by Simon » Thu Feb 25, 2016 5:10 am

Don't need it because they are unconnected.

Now if you make it a global variable then you could use it in both places but you have to make sure they are declared in both places.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply