不能使数字为负
Can't make number negative
我有这个 swift
代码:
x = Float(arc4random_uniform(30))
y = Float(arc4random_uniform(30))
z = Float(arc4random_uniform(30))
coords.x = 0.00 - x
coords.y = 0.00 - y
coords.z = 0.00 - z
print("\(coords.x) \(coords.y) \(coords.z) xyz")
出于某种原因,它总是输出正数,我也试过:
x = -Float(arc4random_uniform(30))
y = -Float(arc4random_uniform(30))
z = -Float(arc4random_uniform(30))
coords.x = x
coords.y = y
coords.z = z
和...
x = -1 * Float(arc4random_uniform(30))
y = -1 * Float(arc4random_uniform(30))
z = -1 * Float(arc4random_uniform(30))
coords.x = x
coords.y = y
coords.z = z
...甚至还有一个我不得不将随机数转换为 Int 以在 Float
转换中将其乘以 -1。
x = Float(-1*Int(arc4random_uniform(30)))
...
控制台输出总是类似于:
24.0 27.0 29.0 xyz
...没有负数。
我做错了什么?
编辑
coords
是我创建的 MyDict
类型:
class MyDict : NSDictionary {
var x: Float = 0.0
var y: Float = 0.0
var z: Float = 0.0
}
这就是我打印值的方式:
print("\(coords.x) \(coords.y) \(coords.z) xyz")
编辑
MyDict
现在是:
struct Coords {
var x: Float = 0.0
var y: Float = 0.0
var z: Float = 0.0
}
编辑
上下文代码 - 这是循环发生的:
for nodeInnermost in nodeInner.childNodes {
if (nodeInnermost.name?.localizedStandardContains("ship"))! {
print(nodeInnermost.childNodes.first ?? "FIRST DOESNT EXIST")
var seqArray = [fadeOut, fadeIn]
var displacements = [] as Array<Coords>
var count = 0
var coords = Coords()
while count < 5 {
if childCount < 5 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 4 && childCount < 10 {
coords.x = -Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 9 && childCount < 15 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 14 && childCount < 20 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 19 && childCount < 25 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 24 && childCount < 30 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 29 && childCount < 35 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 34 && childCount < 40 {
coords.x = -Float(arc4random_uniform(30))
coords.y = -Float(arc4random_uniform(30))
coords.z = -Float(arc4random_uniform(30))
}
//print("\(x) \(y) \(z) xyz")
displacements.append(coords)
print("\(coords.x) \(coords.y) \(coords.z) xyz")
let moveBy = SCNAction.move(by: SCNVector3Make(coords.x, coords.y, coords.z), duration: 0.5)
seqArray.append(fadeOut)
seqArray.append(moveBy)
seqArray.append(fadeIn)
count+=1
}
while count < 10 {
let moveBy = SCNAction.move(by: SCNVector3Make(displacements[9 - count].x, displacements[9 - count].y, displacements[9 - count].z), duration: 0.5)
seqArray.append(fadeOut)
seqArray.append(moveBy)
seqArray.append(fadeIn)
count+=1
}
let sequence = SCNAction.sequence(seqArray)
let animation = SCNAction.repeatForever(sequence)
nodeInnermost.childNodes.first?.runAction(animation)
}
}
样本输出:
您的 MyDict
扩展 NSDictionary
没有任何意义,也不应该是 class
。一旦你使它成为正确的 struct
,你的代码就可以正常工作。
struct MyDict {
var x: Float = 0.0
var y: Float = 0.0
var z: Float = 0.0
}
var coords = MyDict()
coords.x = -Float(arc4random_uniform(30))
coords.y = -Float(arc4random_uniform(30))
coords.z = -Float(arc4random_uniform(30))
print("\(coords.x) \(coords.y) \(coords.z) xyz")
输出:
-24.0 -28.0 -4.0 xyz
我有这个 swift
代码:
x = Float(arc4random_uniform(30))
y = Float(arc4random_uniform(30))
z = Float(arc4random_uniform(30))
coords.x = 0.00 - x
coords.y = 0.00 - y
coords.z = 0.00 - z
print("\(coords.x) \(coords.y) \(coords.z) xyz")
出于某种原因,它总是输出正数,我也试过:
x = -Float(arc4random_uniform(30))
y = -Float(arc4random_uniform(30))
z = -Float(arc4random_uniform(30))
coords.x = x
coords.y = y
coords.z = z
和...
x = -1 * Float(arc4random_uniform(30))
y = -1 * Float(arc4random_uniform(30))
z = -1 * Float(arc4random_uniform(30))
coords.x = x
coords.y = y
coords.z = z
...甚至还有一个我不得不将随机数转换为 Int 以在 Float
转换中将其乘以 -1。
x = Float(-1*Int(arc4random_uniform(30)))
...
控制台输出总是类似于:
24.0 27.0 29.0 xyz
...没有负数。
我做错了什么?
编辑
coords
是我创建的 MyDict
类型:
class MyDict : NSDictionary {
var x: Float = 0.0
var y: Float = 0.0
var z: Float = 0.0
}
这就是我打印值的方式:
print("\(coords.x) \(coords.y) \(coords.z) xyz")
编辑
MyDict
现在是:
struct Coords {
var x: Float = 0.0
var y: Float = 0.0
var z: Float = 0.0
}
编辑
上下文代码 - 这是循环发生的:
for nodeInnermost in nodeInner.childNodes {
if (nodeInnermost.name?.localizedStandardContains("ship"))! {
print(nodeInnermost.childNodes.first ?? "FIRST DOESNT EXIST")
var seqArray = [fadeOut, fadeIn]
var displacements = [] as Array<Coords>
var count = 0
var coords = Coords()
while count < 5 {
if childCount < 5 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 4 && childCount < 10 {
coords.x = -Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 9 && childCount < 15 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 14 && childCount < 20 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 19 && childCount < 25 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 24 && childCount < 30 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 29 && childCount < 35 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 34 && childCount < 40 {
coords.x = -Float(arc4random_uniform(30))
coords.y = -Float(arc4random_uniform(30))
coords.z = -Float(arc4random_uniform(30))
}
//print("\(x) \(y) \(z) xyz")
displacements.append(coords)
print("\(coords.x) \(coords.y) \(coords.z) xyz")
let moveBy = SCNAction.move(by: SCNVector3Make(coords.x, coords.y, coords.z), duration: 0.5)
seqArray.append(fadeOut)
seqArray.append(moveBy)
seqArray.append(fadeIn)
count+=1
}
while count < 10 {
let moveBy = SCNAction.move(by: SCNVector3Make(displacements[9 - count].x, displacements[9 - count].y, displacements[9 - count].z), duration: 0.5)
seqArray.append(fadeOut)
seqArray.append(moveBy)
seqArray.append(fadeIn)
count+=1
}
let sequence = SCNAction.sequence(seqArray)
let animation = SCNAction.repeatForever(sequence)
nodeInnermost.childNodes.first?.runAction(animation)
}
}
样本输出:
您的 MyDict
扩展 NSDictionary
没有任何意义,也不应该是 class
。一旦你使它成为正确的 struct
,你的代码就可以正常工作。
struct MyDict {
var x: Float = 0.0
var y: Float = 0.0
var z: Float = 0.0
}
var coords = MyDict()
coords.x = -Float(arc4random_uniform(30))
coords.y = -Float(arc4random_uniform(30))
coords.z = -Float(arc4random_uniform(30))
print("\(coords.x) \(coords.y) \(coords.z) xyz")
输出:
-24.0 -28.0 -4.0 xyz