简体   繁体   English

当节点在屏幕上通过某个y坐标时,如何更新Sprite Kit中的分数?

[英]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. 在我的Sprite Kit游戏中,当一个节点经过另一个节点时会得分。 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. 我尝试对这种机制进行编码,以便当障碍节点的y坐标大于玩家节点的y坐标时,会得到一个点。 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语句应为:

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语句为:

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. 您将必须为障碍添加一些BOOL属性,以免同一障碍物的得分不断增加。 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. 另一种选择是将所有障碍物排在一个数组中(以得分为目的),并在障碍物通过玩家后将其从数组中清除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift Sprite-Kit:触摸屏幕时如何增加游戏得分? - Swift Sprite-Kit: How do I increase my game's score when screen is touched? 在精灵工具包中,当我在节点上运行一个动作以将其上移到某个y值时,它会逐渐变慢。 为什么? - In sprite kit with swift when i run an action on a node to move it up to a certain y value it gradually slows down. Why? Sprite Kit - 不允许节点移出屏幕 - Sprite Kit - Do not allow node to move out of screen Sprite-Kit在触摸屏幕时更改节点的图像 - Sprite-Kit Change Image of Node when screen gets touched 放开屏幕时如何释放节点? 雪碧套件 - How To Release Node When You Let Go Ot Screen? Sprite Kit 如何在Sprite Kit中循环播放音乐? - How do I loop music in Sprite Kit? 在Sprite Kit中,当我生成节点时,为什么它们都出现在屏幕的右上方? - In Sprite Kit, when I am spawning nodes, why do they all appear on the top right of the screen? Swift Sprite Kit:如何制作它,以便iAd在一定时间间隔后显示新的添加? - Swift Sprite Kit: How do I make it so that iAd displays a new add after a certain time interval? Sprite Kit - 节点离开屏幕的通知程序 - Sprite Kit - Notifier that a node is leaving the screen 如何仅影响Sprite-Kit中Sprite的一个坐标? - How to only affect one coordinate of a sprite in sprite-kit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM