gpt4 book ai didi

swift - 遵循 Spritekit EXC_BAD_ACCESS 中的路径

转载 作者:搜寻专家 更新时间:2023-11-01 05:36:08 26 4
gpt4 key购买 nike

我有一个简单的贪吃蛇游戏,其中的头部绘制了一条 UIBezier 路径。那部分工作正常:

func addLineToSnakePath(snakeHead: SnakeBodyUnit) {
//add a new CGPoint to array
activeSnakePathPoints.append(CGPoint(x: snakeHead.partX, y: snakeHead.partY))

let index = activeSnakePathPoints.count-1

if (index == 1) {
path.moveToPoint(activeSnakePathPoints[index-1])
}

path.addLineToPoint(activeSnakePathPoints[index])
shapeNode.path = path.CGPath
}

当头部在屏幕上移动时,路径会随着滑动而生成。现在,我添加了一个主体单元以遵循 UIBezier 路径,但出现了错误的访问错误。

 func addBodyPart() {
let followBody = SKAction.followPath(path.CGPath, asOffset: true, orientToPath: false, duration: 1.0)
snakePart.runAction(followBody)
}

崩溃时间:0 SKCFollowPath::cpp_willStartWithTargetAtTime(SKCNode*, 双)

线程 1 EXC_BAD_ACCESS

最佳答案

查看代码我宁愿这样加强我的代码:

func addLineToSnakePath(snakeHead: CGPoint) {
let count = activeSnakePathPoints.count
if count == 0 { return } // The snake don't have an head..
//add a new CGPoint to array
activeSnakePathPoints.append(CGPoint(x: snakeHead.x, y: snakeHead.y))
guard count > 1 else {
// There are only two element (the head point and the new point) so:
path = UIBezierPath.init()
path.moveToPoint(activeSnakePathPoints[count-1])
shapeNode.path = path.CGPath
return
}
// There are some elements:
path.addLineToPoint(activeSnakePathPoints[count-1])
shapeNode.path = path.CGPath
}

接下来,当您制作 followPath 时action 我看到你将 offset 值设置为 true(我不知道你是否需要这样做......):

asOffset : If YES, the points in the path are relative offsets to the node’s starting position. If NO, the points in the node are absolute coordinate values.

换句话说,true 表示路径相对于 Sprite 的起始位置,false 表示绝对

最后一行:

snakePart.runAction(followBody)

我不知道什么是 snakePart 但你在函数中使用了 shapeNode

更新:

考虑到你的崩溃,似乎 snakePart 不是一个有效的 SKNode(比如当你尝试使用一个 Sprite 或形状而不初始化自己时)

关于swift - 遵循 Spritekit EXC_BAD_ACCESS 中的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39155028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com