0

I'm developing my first game app for the iphone, and I'm still a bit nooby in objective-c.

The game is coming along fine but i'm stuck on a little problem...

I want to make it so that for every 20 kills, you get +1 life, I know how to do that but with poorly written code such as this...:

    switch (kills) {

        case 20:

            lives++;

            livescount.text = [NSString stringWithFormat:@"%i", lives];

            break;

        case 40:

            lives++;

            livescount.text = [NSString stringWithFormat:@"%i", lives];

            break;

        case 60:

            lives++;

            livescount.text = [NSString stringWithFormat:@"%i", lives];

            break;
        case 80:

            lives++;

            livescount.text = [NSString stringWithFormat:@"%i", lives];

            break;
        case 100:

            lives++;

            livescount.text = [NSString stringWithFormat:@"%i", lives];

            break;

        case 120:

            lives++;

            livescount.text = [NSString stringWithFormat:@"%i", lives];

            break;
        case 140:

            lives++;

            livescount.text = [NSString stringWithFormat:@"%i", lives];

            break;
        case 160:

            lives++;

            livescount.text = [NSString stringWithFormat:@"%i", lives];

            break;

        case 180:

            lives++;

            livescount.text = [NSString stringWithFormat:@"%i", lives];

            break;

        case 200:

            lives++;

            livescount.text = [NSString stringWithFormat:@"%i", lives];

and so on.... I don't want to keep writing this till infinity lol


I know for a fact that this is an improper way to write the code. Can anyone give me a proper code for every 20 kills = +1 life. I would probably spot on and understand how the code works too.

2 Answers 2

4

Use the modulo operator (%) that return the remaider of division by 20 ; also check that the number of kills is different from zero.

if (kills != 0 && kills % 20 == 0) {
    lives++;
    livescount.text = [NSString stringWithFormat:@"%i", lives];
}
Sign up to request clarification or add additional context in comments.

4 Comments

This gives an extra life if there are no kills.
Can someone further explain what it means? I want to make sure I know what it is so that I can understand Objective-C a bit more.
The % operator is the modulo operator - it returns the remainder of the division. For example, the quotient of the integer division of 23 by 10 is 2 : 23 / 10 = 2 ; the remainder of the integer division of 23 by 10 is 3 : 23 % 10 = 3. See wikipedia, paragraph "Division of integers"
In your case, as you need to run the code whenever kills = 20, 40, 60, 80... it is equivalent to check that the reminder of the division by 20 is zero.
0

You should use mod, so:

if (kills && kills % 20 == 0) {
  lives++;
}

Mod (%) returns the remainder of the division: so in this case if kills is exactly divisible by 20 it will return zero.

Good luck!

11 Comments

This gives an extra life if there are no kills.
what about this if(kills != 0 && kills%20 == 0 )
This would probably work but i don't know i'm not on my mac at the moment... However I don't want my kills to be 0 again, it will mess up the score, I guess i'll make another invisible int such as kills2 but it would be so much work lol
@user2748685 This does not modify the value of kills.
@rmaddy hmmm then why does it have 20 == "0"?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.