我有两个SKNodes。我把我的角色添加为第一个角色的子角色,把地图添加为第二个角色的子角色。我在人物接触的地图上有一些物理体。我在屏幕上有一个虚拟操纵杆,可以移动第二个SKNode。(表现出运动的样子。)
我移动第二个(map)SKNode的方式造成了这种奇怪的困境。我想不通。
需要
虚拟操纵杆代码:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if isTracking == true && location.x >= -(self.frame.width / 2) {
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 = thumbNode.position.x - DPad.position.x
yJoystickDelta = thumbNode.position.y - DPad.position.y
}
}
}
地图代码的移动:
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
let xScale = CGFloat(0.056) //adjust to your preference
let yScale = CGFloat(0.056) //adjust to your preference
let xAdd = xScale * self.xJoystickDelta
let yAdd = yScale * self.yJoystickDelta
map.position.x -= xAdd
map.position.y -= yAdd
}