gpt4 book ai didi

ios - 从 Sprite 拖一条直线到 SpriteKit 中的手指位置

转载 作者:行者123 更新时间:2023-11-29 05:23:51 27 4
gpt4 key购买 nike

我想知道问题是否与节点的排列有关,而不是与代码有关,但我似乎无法让这条线正常工作。我想从触摸的节点到触摸的位置绘制一条可拖动的直线。然后该节点将移动到触摸结束的位置。我的代码基于我在这里看到的一些内容,但仍然无法显示出来。此相关节点是 SKSpriteNode 的子类,并且此代码位于该类中,而不是在主 GameScene 类中。

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
self.childNode(withName: "line")?.removeFromParent() // Remove any previous line
guard let touch = touches.first else { return }
let positionInScene = touch.location(in: self.parent!) // Use 'parent' here?
let path = CGMutablePath()
let line = SKShapeNode(path: path) // Create shape node with path as path
line.name = "line"
line.zPosition = 5000 // Just trying to make sure it's on top
line.strokeColor = UIColor.red
line.lineWidth = 20
line.fillColor = UIColor.red
path.move(to: CGPoint(x: self.position.x, y: self.position.y))
path.addLine(to: CGPoint(x: positionInScene.x, y: positionInScene.y))
self.addChild(line)
}

当我在 touchesBegan 中创建并添加路径时,它会显示出来,但这会绘制一堆线条,而我只想要一条。

最佳答案

简单的错误。只需在绘制线条后为其分配path即可。

let path = CGMutablePath()    
path.move(to: CGPoint(x: self.position.x, y: self.position.y))
path.addLine(to: CGPoint(x: positionInScene.x, y: positionInScene.y))

let line = SKShapeNode(path: path) // Create shape node with path as path
self.addChild(line)

关于ios - 从 Sprite 拖一条直线到 SpriteKit 中的手指位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58381237/

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