简体   繁体   中英

How do I update the score in sprite kit when a node passes a certain y coordinate on the screen?

In my sprite kit game, a point is scored when a node moves past another node. I tried to code this mechanic so that when the y-coordinate of the obstacle node is greater than the y coordinate of the player node, a point is scored. However, this does not work. This code one works when the greater than sign is changed to a less than sign, and the score is updated as soon as the obstacles appear on the screen. I only want the score to update when the obstacle pass the player. Any help will be appreciated, thanks

    if (obstacle1.position.y > person1.position.y) {
    scoreNumber++;
    scoreLabelNode.text = [NSString stringWithFormat:@"%i", scoreNumber];
    }

If your player is at the bottom of the screen and the obstacle starts at the top, the correct if statement should read:

if (obstacle1.position.y < person1.position.y)

If however your player is at the top and the obstacle starts at the bottom, the correct if statement is:

if (obstacle1.position.y > person1.position.y)

You will have to include some BOOL property for the obstacles in order not to have your score increased over and over again for the same obstacle. Another option is to have all your obstacles in an array (for purposes of scoring) and remove any obstacle from the array once it passes the player.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM