gpt4 book ai didi

swift - 根据度数移动 Sprite

转载 作者:行者123 更新时间:2023-11-28 06:04:49 24 4
gpt4 key购买 nike

我已经能够检测到度数,但我不确定如何在 360 度范围内移动 Sprite 。 drawing我不希望 Sprite 只能在某些部分移动,如上图所示,而是让它能够在一个完整的圆圈内移动。代码:

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)


}
}

最佳答案

这非常简单,尽管我花了很长时间才弄清楚如何去做。

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 的角度,然后在里面设置纹理(或任何你想做的)的各种情况。然后它计算 thumbNode 在 DPad 内的位置并在场景中移动你的播放器(或任何你需要移动的东西)。将 xScaleyScale 调整得更高以减慢移动速度,调整得更低以增加您要移动的物体的移动速度。

-额外必要的东西-

您需要在didMove(toView:) 方法中设置DPadthumbNode:

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 将随之移动。另外,如果您有任何问题,请务必询问我,以便我能为您提供帮助。

关于swift - 根据度数移动 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48554796/

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