根据度数移动 Sprite
Posted
技术标签:
【中文标题】根据度数移动 Sprite【英文标题】:Move Sprite Based Off of Degrees 【发布时间】: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,我的回答将提供您设置操纵杆所需的一切? 我现在正在测试它。谢谢 有什么问题吗?我回答你的问题了吗?如果是这样,您需要将我的答案标记为正确,以免以后的观众感到困惑。 【参考方案1】:这非常简单,虽然我花了很长时间才弄清楚如何去做。
TouchesBegan 方法:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
for touch in touches
let location = touch.location(in: self)
if isTracking == false && DPad.contains(location)
isTracking = true
TouchesMoved 方法:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
for touch in touches
let location: CGPoint = touch.location(in: self)
if isTracking == true
v = CGVector(dx: location.x - DPad.position.x, dy: location.y - DPad.position.y)
let angle = atan2(v.dy, v.dx)
let deg = angle * CGFloat(180 / Double.pi)
let Length:CGFloat = DPad.frame.size.height / 2
let xDist: CGFloat = sin(angle - 1.57079633) * Length
let yDist: CGFloat = cos(angle - 1.57079633) * Length
xJoystickDelta = location.x - DPad.position.x
yJoystickDelta = location.y - DPad.position.y
if DPad.contains(location)
thumbNode.position = location
else
thumbNode.position = CGPoint(x: DPad.position.x - xDist, y: DPad.position.y + yDist)
TouchesEnded 方法:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
isTracking = false
thumbNode.run(SKAction.move(to: DPad.position, duration: 0.01))
xJoystickDelta = 0
yJoystickDelta = 0
update(_ currentTime:)
方法:
if v.dx > abs(v.dy)
yourPlayer.texture = SKTexture(imageNamed: "rightTexture")
else if v.dx < -abs(v.dy)
player.texture = SKTexture(imageNamed: "leftTexture")
else if v.dy < 0
yourPlayer.texture = SKTexture(imageNamed: "frontTexture")
else if v.dy > 0
yourPlayer.texture = SKTexture(imageNamed: "backTexture")
//This code moves your character where-ever you want it too
let xScale = CGFloat(4) //adjust to your preference. Higher means slower, lower means faster
let yScale = CGFloat(4) //adjust to your preference. Higher means slower, lower means faster
let xAdd = xScale * self.xJoystickDelta
let yAdd = yScale * self.yJoystickDelta
yourPlayerNode.position.x += xAdd
yourPlayerNode.position.y += yAdd
这些东西需要在你的didMove(toView:)
方法之外:
var xJoystickDelta:CGFloat = 0
var yJoystickDelta:CGFloat = 0
var v = CGVector()
var isTracking:Bool = false
var DPad = SKSpriteNode()
var thumbNode = SKSpriteNode()
-解释-
在touchesBegan
方法中,if 语句正在测试您是否没有控制 thumbNode 以及您的触摸是否在 DPad 节点内。然后它开始跟踪。
在touchesMoved
一次isTracking == true
中,它开始计算必要的数学,然后调整所需的各种东西。 (它很复杂,最重要的是它的工作原理。)
在touchesEnded
方法中,它会测试您何时将手指从屏幕上移开,然后它会重置所有内容以供下次使用。
在update(_ current:)
方法中,代码计算CGVector
的角度,然后在各种情况下设置纹理(或您想要做的任何事情)。然后它计算拇指节点在 DPad 内的位置,并在场景中移动你的播放器(或任何你需要移动的东西)。调整 xScale
和 yScale
浮动更高以减慢运动,降低以增加您试图移动的任何物体的移动。
-额外的必需品-
您需要在didMove(toView:)
方法中设置DPad
和thumbNode
:
thumbNode.size = CGSize(width: 50, height: 50)
DPad.size = CGSize(width: 150, height: 150)
DPad.position = CGPoint(x: 0, y: 0)
thumbNode.position = DPad.position
DPad.zPosition = 3
thumbNode.zPosition = 4
DPad.texture = SKTexture(imageNamed: "yourBaseTexture")
thumbNode.texture = SKTexture(imageNamed: "yourStickTexture")
self.addChild(thumbNode)
self.addChild(DPad)
您只需将DPad.position
移动到您想要的任何位置。 thumbNode 将随之移动。另外,如果您有任何问题,请务必问我,以便我为您提供帮助。
【讨论】:
以上是关于根据度数移动 Sprite的主要内容,如果未能解决你的问题,请参考以下文章
如何在伺服电机上使用 pwm 将度数转换为占空比以实现平稳移动?