如何在spritekit中的屏幕上显示数组中的对象数量
在我的游戏中,我的生命数量挽救了一个阵列,然后在游戏玩法期间将生活显示在游戏视图的右上角。我做到的方式是有效的,但是在搬到一个下一个级别时,我会遇到问题。
这在第一层效果很好,我可能会在命中时丧生,但是当我试图拥有额外的生活拾音器时,我确实遇到了问题。我能够添加到数组中,但无法在屏幕上显示额外的寿命。我最终从游戏中削减了这一功能,但是现在,当玩家发展到下一个水平时,现在遇到了同样的问题。使用相同的循环将导致玩家再次拥有三个生命,无论上一个级别损失了多少。
我知道问题是我使用的循环导致我设置为1至3的循环。我不是一个很好的编程,并且不确定要使用哪种循环以及搜索解决方案时我显然不是在问正确的问题将我指向正确的方向。
如果有人能提供帮助,将不胜感激。谢谢。
这是我的代码:
class GameScene: SKScene, SKPhysicsContactDelegate {
//lives
static var livesArray:[SKSpriteNode]!
override func didMove(to view: SKView) {
//Call Lives method
addLives()
}
func addLives() {
GameScene.livesArray = [SKSpriteNode]()
for life in 1...3 {
let lifeNode = SKSpriteNode(imageNamed: "Life")
lifeNode.position = CGPoint(x: 350 - CGFloat(4 - life) * lifeNode.size.width, y: 525)
addChild(lifeNode)
GameScene.livesArray.append(lifeNode)
}
}
}
In my game I have the number of lives saved in an array, the lives are then displayed in the top right of the game view during gameplay. The way I have it done this is working but I hit problems when moving to the next level.
This works fine in the first level, I can lose lives when hit but I did hit a problem when I tried to have extras life pickups. I was able to add to the array but couldn't display the extra life on the screen. I ended up cutting this feature from the game but now hit the same problem when player advances to next level. Using the same loop will result in the player having three lives again no matter how many are lost in the previous level.
I know the problem is the loop I've used caused I've set the for in loop to be 1 to 3. I'm not great a programming and not sure what kind of loop to use for this and when Googling for a solution I'm obviously not asking the right questions to point me in the right direction.
If anyone can help it would be greatly appreciated. Thank you.
Here's my code:
class GameScene: SKScene, SKPhysicsContactDelegate {
//lives
static var livesArray:[SKSpriteNode]!
override func didMove(to view: SKView) {
//Call Lives method
addLives()
}
func addLives() {
GameScene.livesArray = [SKSpriteNode]()
for life in 1...3 {
let lifeNode = SKSpriteNode(imageNamed: "Life")
lifeNode.position = CGPoint(x: 350 - CGFloat(4 - life) * lifeNode.size.width, y: 525)
addChild(lifeNode)
GameScene.livesArray.append(lifeNode)
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论