【发布时间】:2017-05-12 06:02:21
【问题描述】:
在我使用--theX > 0 之前,它运行良好。
旧代码摘录:
if --theX < 0 {
...
if ++theX < 0 {
...
if ++theX > worldSize.width + 1 {
...
if --theX > worldSize.height {
...
您可以在下面看到具有theX - 1 > 0 或类似内容的四行代码。现在我已经更新到 Swift 3,我认为将 --theX 更改为 theX - 1 或 theX -= 1 会很容易。
尝试更新代码:
func move(_ point:Point, worldSize:WorldSize) -> (Point) {
var theX = point.x
var theY = point.y
switch self {
case .left:
if theX - 1 < 0 {
// theX = worldSize.width - 1
print("g.o.")
stopped = true
}
case .up:
if theY + 1 < 0 {
// theY = worldSize.height - 1
print("g.o.")
stopped = true
}
case .right:
if theX + 1 > worldSize.width + 1 {
// theX = 0
print("g.o.")
stopped = true
}
case .down:
if theY - 1 > worldSize.height {
// theY = 0
print("g.o.")
stopped = true
}
}
return Point(x: theX, y: theY)
}
}
但是,这似乎不起作用(“无法将 'Bool' 类型的值转换为预期的参数类型 'Int'” ---> 使用 -= 或 += 时)。如果你们想知道,这是一个 Snake 街机游戏,上面的函数是当蛇移动(左、右、上、下)时发生的情况
对于为什么会发生此问题或可能如何使用不同但相似版本的增量和减量(- 和 ++)的任何帮助?
【问题讨论】:
-
请用更有用的标题更新您的问题,具体到您遇到的问题。
-
你在哪一行得到了这个错误?
标签: swift xcode int swift3 boolean