I'm creating a game using Swift 2.0
and Sprite-Kit
with Xcode7
. I want to implement 4 purple balls that are suppose to resemble lives. So every time the player gets hit he loses one purple ball. They are supposed to appear side by side. I was wondering if instead of hardcoding 4 balls on to the scene I could instead use a for loop
to display 4 balls.
我正在使用Swift 2.0和Sprite-Kit与Xcode7创建游戏。我想实现4个紫色球,假设它们类似于生命。所以每次球员被击中时他都会失去一个紫色的球。他们应该并排出现。我想知道如果不是硬编码4个球到现场我可以改为使用for循环来显示4个球。
let purpleBall = SKSpriteNode(texture: purpleTexture)
purpleBall.position = CGPointMake(self.frame.size.width * 0.65, self.frame.size.height * 0.92)
self.addChild(purpleBall)
I haven't been successful on getting 4 balls to appear on the scene. This was one of my attempts.
我没有成功让4个球出现在现场。这是我的一次尝试。
for(var i = 0.50; i <= 0.90; i = i + 0.10) {
let purpleBall = SKSpriteNode(texture: purpleTexture)
purpleBall.position = CGPointMake(self.frame.size.width * i, self.frame.size.height * 0.92)
self.addChild(purpleBall)
}
Here I get an error: Binary operator cannot be applied to operands of type 'CGFloat' and 'Double' Do I have to convert i
to CGFloat? and will this code actually place 4 different balls side by side or only move the single one to each new position.
这里我得到一个错误:二进制运算符不能应用于'CGFloat'和'Double'类型的操作数我是否必须将i转换为CGFloat?并且这段代码实际上会并排放置4个不同的球,或者只将单个球移动到每个新位置。
1 个解决方案
#1
0
Your code has an off by one error in the for loop. It should be a less than sign rather than less than or equal too. Your current code is going to create five purple balls.
您的代码在for循环中有一个错误。它应该小于符号而不是小于或等于。您当前的代码将创建五个紫色球。
You shouldn't really let i equal anything but an integer it's confusing. You could rewrite the code like this and it would do the same thing, without the worry of the float/double errors you're currently getting. Note that I will also use a constant called maxBalls, and distanceBetweenBalls so that you can easily change the number of balls and the distance without any complicated rewriting:
你不应该让我相等,只是一个整数令人困惑。你可以像这样重写代码,它会做同样的事情,而不用担心你目前得到的浮动/双重错误。请注意,我还将使用一个名为maxBalls和distanceBetweenBalls的常量,以便您可以轻松更改球的数量和距离,而无需任何复杂的重写:
let maxBalls = 4
for(var i = 0; i < maxBalls; i++) {
let purpleBall = SKSpriteNode(texture: purpleTexture)
let ballXPosition = .50 + (i * .1)
purpleBall.position = CGPointMake(self.frame.size.width * ballXPosition, self.frame.size.height * 0.92)
self.addChild(purpleBall)
This should avoid the issues you were facing before, hope that helps.
这应该避免你以前遇到的问题,希望有所帮助。
#1
0
Your code has an off by one error in the for loop. It should be a less than sign rather than less than or equal too. Your current code is going to create five purple balls.
您的代码在for循环中有一个错误。它应该小于符号而不是小于或等于。您当前的代码将创建五个紫色球。
You shouldn't really let i equal anything but an integer it's confusing. You could rewrite the code like this and it would do the same thing, without the worry of the float/double errors you're currently getting. Note that I will also use a constant called maxBalls, and distanceBetweenBalls so that you can easily change the number of balls and the distance without any complicated rewriting:
你不应该让我相等,只是一个整数令人困惑。你可以像这样重写代码,它会做同样的事情,而不用担心你目前得到的浮动/双重错误。请注意,我还将使用一个名为maxBalls和distanceBetweenBalls的常量,以便您可以轻松更改球的数量和距离,而无需任何复杂的重写:
let maxBalls = 4
for(var i = 0; i < maxBalls; i++) {
let purpleBall = SKSpriteNode(texture: purpleTexture)
let ballXPosition = .50 + (i * .1)
purpleBall.position = CGPointMake(self.frame.size.width * ballXPosition, self.frame.size.height * 0.92)
self.addChild(purpleBall)
This should avoid the issues you were facing before, hope that helps.
这应该避免你以前遇到的问题,希望有所帮助。