【发布时间】:2018-02-01 03:19:25
【问题描述】:
我已经能够检测度数,但我不确定如何将精灵移动 360 度。 我不希望精灵只能在某些部分移动,如上图所示,而是让它能够移动一个完整的圆圈。 代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if (ball.frame.contains(location)) {
stickActive = true
}else {
stickActive = false
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if (stickActive == true) {
var v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y)
let angle = atan2(v.dy, v.dx)
var deg = angle * CGFloat(180 / M_PI)
print(deg + 180)
let lenght:CGFloat = base.frame.size.height / 2 - 20
let xDist: CGFloat = sin(angle - 1.57079633) * lenght
let yDist: CGFloat = cos(angle - 1.57079633) * lenght
ball.position = CGPoint(x: base.position.x - xDist, y: base.position.y + yDist)
if (base.frame.contains(location)) {
ball.position = location
}else {
ball.position = CGPoint(x: base.position.x - xDist, y: base.position.y + yDist)
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if (stickActive == true) {
let move: SKAction = SKAction.move(to: base.position, duration: 0.2)
move.timingMode = .easeOut
ball.run(move)
}
}
【问题讨论】:
-
你在做一个游戏杆吗?如果你是我可以帮你设置,很简单。
-
对不起,如果我的问题有点混乱,但我想做一个操纵杆。span>
-
假设您使用 spritekit,我的回答将提供您设置操纵杆所需的一切?
-
我现在正在测试它。谢谢
-
有什么问题吗?我回答你的问题了吗?如果是这样,您需要将我的答案标记为正确,以免以后的观众感到困惑。
标签: swift xcode sprite-kit swift4