Search code examples
iosscoring

Score system not working Obj-C


So i am developing a game and have coded the score system. I want it to be so that when my object that is scrolling down is equal to the same y.co-ordinate as the "Box" object, the score system adds one to the score.

However, upon trying this the socre does not update and stays at 0. Im not quite sure why, could you please tell me where I may be going wrong and how i could fix it.

This is the code I am using to code the score system. Any help would be appreciated, thanks.

-(void)Score{

ScoreNumber = ScoreNumber + 1;
ScoreDisplay.text = [NSString stringWithFormat:@"%i", ScoreNumber];

}

[There is also some code in the view did all. but the problem is not with this part of the code as i have used it before and it works fine.]

This is where it is meant to be implemented but is not working...

if (ROne.center.y == IView.center.y) {
        [self Score];
    }

    if (RTwo.center.y == IView.center.y) {
        [self Score];
    }

    if (RThree.center.y == IView.center.y) {
        [self Score];
    }

    if (RFour.center.y == IView.center.y) {
        [self Score];
    }

    if (RFive.center.y == IView.center.y) {
        [self Score];
    }

    if (RSix.center.y == IView.center.y) {
        [self Score];
    }

Solution

  • Your problem is likely that is the y-increment doesn't add up to the y-coordinate of the target object, then it won't be strictly equal. You'll just sort of pass by it. This is why >= worked, except once you passed it, it will keep adding to the score.

    So, what you need to test for is "passing by". That is to say that prior to moving, ROne.center.y < IView.center.y and after moving ROne.center.y >= IView.center.y. That is the only condition that effectively check for "ROne just arrived at the desired height".

    Alternatively, you can use >= and use a flag arrived on ROne to tell it to quit counting the score. Set the arrived flag to false when (ROne.center.y >= IView.center.y && ROne.arrived) is true. Then reset the arrived flag to true when you decide to reset the object to the top of the screen.