ios - 在两点/触摸之间拉伸(stretch) SKSpriteNode

标签 ios swift iphone sprite-kit

我有一个 SKSpriteNode,我在其中设置了 centerRect 属性,以便可以拉伸(stretch)节点以使其看起来像一条样式线。我的意图是让用户触摸屏幕,然后用节点绘制/拖动一条直线。该线将围绕 anchor 旋转以保持笔直。

touchesBegan:中,添加节点:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    let positionInScene = touch.location(in: self)
    if let _ = fgNode.childNode(withName: "laser") {
        print("already there")
    } else {
        laser.centerRect = CGRect(x: 0.42857143, y: 0.57142857, width: 0.14285714, height: 0.14285714)
        laser.anchorPoint = CGPoint(x: 0, y: 0.5)
        laser.position = positionInScene
        fgNode.addChild(laser)
    }
}

并在touchesMoved:中调整:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    let positionInScene = touch.location(in: self)
    stretchLaserTo(positionInScene)
}

节点的拉伸(stretch)和旋转有两个函数:

func stretchLaserTo(_ point: CGPoint) {
    let offset = point - laser.anchorPoint
    let length = offset.length()
    let direction = offset / CGFloat(length)
    laser.xScale = length
    rotate(sprite: laser, direction: direction)
}


func rotate(sprite: SKSpriteNode, direction: CGPoint) {
    sprite.zRotation = atan2(direction.y, direction.x)
}

我认为我在某种程度上是在正确的轨道上。这条线随着我的触摸而旋转和扩展,但是,它非常敏感,不会随着我的触摸而停留。也许我做错了。是否有执行此类操作的标准技术?

可以在此处看到此工作的示例:https://imgur.com/A83L45i

最佳答案

我建议您将 Sprite 的 anchor 设置为(0, 0),将 Sprite 的比例设置为 Sprite 位置与当前触摸位置之间的距离,然后旋转 Sprite 。

首先,创建一个 Sprite 并设置其 anchor 。

let laser = SKSpriteNode(color: .white, size: CGSize(width: 1, height: 1))

override func didMove(to view: SKView) {
    laser.anchorPoint = CGPoint(x: 0, y: 0)
    addChild(laser)
}

touchesBegan中,将 Sprite 的位置设置为触摸的位置。在本例中,它也是该行的开头。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    let positionInScene = touch.location(in: self)
    laser.position = positionInScene
    laser.setScale(1)
}

更新 sprite,使其形成一条线,从 sprite 的位置开始,到当前触摸位置结束。

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    let positionInScene = touch.location(in: self)
    stretchLaserTo(positionInScene)
}

通过将 Sprite 的 xScale 设置为线条起点到当前触摸位置的距离来拉伸(stretch) Sprite ,然后旋转 Sprite 。

func stretchLaserTo(_ point: CGPoint) {
    let dx = point.x - laser.position.x
    let dy = point.y - laser.position.y
    let length = sqrt(dx*dx + dy*dy)
    let angle = atan2(dy, dx)
    laser.xScale = length
    laser.zRotation = angle
}

关于ios - 在两点/触摸之间拉伸(stretch) SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56796948/

相关文章:

iphone - iOS : Date conversion results in wrong date

ios - 将用户注册到 firebase 并登录(如果已注册)

ios - 如何将 UISwitch 放置在 UITableViewCell accessoryView 中?

iphone - 通过segue/分段控制传递

iphone - 无法在 iphone 3G 上使用 AVAssetWriter 从 UIImage 数组制作电影

iphone - 将所有声音录制为一个音轨 (iOS)

iOS App 包含开发者路径信息

ios - Instruments 实际报告什么?

当应用程序被杀死时,iOS 不会收到推送通知

arrays - 使用变量分隔符加入数组