How you do this will depend on what you're trying to do. The original "calcNeeded" that I use in my games is about what's needed changing as the game progresses. If you're in scene1, you need a 5 to continue, but if you're in scene 2, you need an 8, etc. It also handled the difference between difficulties.
If, on the other hand, you just need fixed values, like "5 for hug, 10 for kiss, 15 for squeeze" etc, there are easier ways to do it. I like what Kexter suggested (aside from the crazy bracket indention :-).
Kexter talked about this, but it's really important, so I'm going to reiterate. In all "c like" languages (c, c++, c#, java, javascript, etc) there's a huge difference between "=" and "==".
"=" is "set the thing on the left to the thing on the right."
"==" is "check if the thing on the left is the same as the thing on the right."
So, say you have
var score = 5;
At that point, score is equal to 5.
Say you then have
score = 6;
Now score is equal to 6.
But say you have
if (score == 7)
This checks if score is equal to 7 (which it's not, it's equal to 6) and returns "true" or "false" (false in this case).
It's really unfortunate, but in most of those "c like" languages, this is legal.
if (score = 7)
But this doesn't do what you think it does. It changes "score" to 7 and then checks to see if that value is "true" or "false." In these languages, anything that isn't 0 is true, so this makes it true, even though that's not what you wanted. One of my first coding professors (more years ago than I'd like to admit) once told me that I was definitely going to make this mistake. I thought he was kind of a jerk and said, "No I'm not!" Unfortunately, he was right...
So, imagine the following code. (Note that things that start with // are comments and aren't used by the code.)
- Code: Select all
var score = 0;
// Something happens that gives you a lot of points
score = 4;
if (score = 8)
{
// allow the player to kiss
}
if (score == 5)
{
// allow the player to hug
}
Here's what happens.
The player has 4 points and shouldn't be allowed to kiss or hug. But when you test your code, you find that he's allowed to both kiss AND hug. You pull your hair out for a bit, before you notice that you only used one = in "if (score = 8)".
After that line, score is equal to 8, not 4. It allows him to kiss and then, later, when it correctly uses two equals on the hug, score is 8, so he can hug too.
Moral of the story: if your code isn't working right, check your equals.
Tlaero