gpt4 book ai didi

ios - 操纵杆,将控制杆夹在操纵杆底座内

转载 作者:行者123 更新时间:2023-11-28 15:14:49 26 4
gpt4 key购买 nike

我有一个在 SpriteKit 中创建的圆形操纵杆,我如何将较小的控制杆夹在较大的操纵杆底座内,并以编程方式防止它超出操纵杆底座。

在我的 didMove(toView:) 方法中设置操纵杆的代码。

var DPad = SKSpriteNode(imageNamed: "dpad")
DPad.size = CGSize(width: 150, height: 150)
DPad.position = CGPoint(x: -270, y: -100)

var thumbNode = SKSpriteNode(imageNamed: "joystick")
thumbNode.size = CGSize(width: 50, height: 50)
thumbNode.position = CGPoint(x: DPad.position.x, y: DPad.position.y)

touchesBegan 方法

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if thumbNode.contains(location) && isTracking == false {
isTracking = true
}

}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if isTracking == true {
thumbNode.run(SKAction.move(to: location, duration: 0.01))
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
thumbNode.run(SKAction.move(to: DPad.position, duration: 0.01))
if thumbNode.position.x > DPad.size.width || thumbNode.position.y > DPad.size.height {
thumbNode.position = DPad.position
}
}
}

override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
if thumbNode.physicsBody?.velocity.dx > Float(0) || thumbNode.physicsBody?.velocity.dx < Float(0) {
print("greater")
}
}

最佳答案

为此,您可以使用 SKConstraint.distance(SKRange(upperLimit: radius), to: baseNode)

要添加它,只需执行 joystickNode.constraints = [SKConstraint.distance(SKRange(upperLimit: radius), to: baseNode)]

关于ios - 操纵杆,将控制杆夹在操纵杆底座内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47040911/

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