我有一个由操纵杆控制的节点,当我将操纵杆向左移动时,我想更改该节点的图像,并且当我向右移动操纵杆时,要更改相同的图像。我该怎么办?这是我的代码:
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if stickActive == true {
//EDIT Code to change texture when moving joystick to left or right.
if joyStick.position.x < base.position.x {
plane.texture = SKTexture(imageNamed: "planeleft")
} else {
plane.texture = SKTexture(imageNamed: "planeright")
}
plane.removeActionForKey("stopaction")
plane.physicsBody?.affectedByGravity = false
xJoystickDelta = location.x - base.position.x
yJoystickDelta = location.y - base.position.y
let v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.x)
let angle = atan2(v.dy, v.dx)
let length: CGFloat = base.frame.size.height / 2
let xDist: CGFloat = sin(angle - 1.57079633) * length
let yDist: CGFloat = cos(angle - 1.57079633) * length
if (CGRectContainsPoint(base.frame, location)) {
joyStick.position = location
}else {
base.alpha = 0.5 //sets the opacity to 0.5 when the joystick is touched
joyStick.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)
}
}
}
}
override func update(currentTime: CFTimeInterval) {
let xScale = 0.08
let yScale = 0.08
let xAdd = self.xJoystickDelta * CGFloat(xScale)
let yAdd = self.yJoystickDelta * CGFloat(yScale)
self.plane.position.x += xAdd
self.plane.position.y += yAdd
}
最佳答案
我对此并不熟悉,但是我知道您应该为此使用逻辑。
复杂:
要检测操纵杆是否在左侧,您应该使用最左边的范围,当操纵杆位于中间时,操纵杆可以移至操纵杆的中心。右侧将是从中心到最右侧。
简单:
如果操纵杆的X值小于默认的中心位置,请在左侧将其设置为X。右边的X位置将大于中间位置。
这些是update函数内部的if语句,因此它将不断进行检查。在语句中将是这样的:
plane.texture = SKTexture(imageNamed: "TextureName")
注意:这全部假设操纵杆处于固定位置并具有设定的中心
希望这可以帮助!
关于ios - 我将操纵杆向左或向右移动时,如何更改节点的纹理?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39060207/