当我向左或向右移动操纵杆时,如何更改节点的纹理?
How do I change the texture of my node when Im moving the joystick to the left or right?
我有一个由操纵杆控制的节点,当我向左移动操纵杆时,我想更改节点的图像,当我向右移动操纵杆时,我想改变同样的事情。我该怎么做?这是我的代码:
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 位置。
这些将是更新函数中的 if 语句,因此它会不断检查这一点。在语句中将是这样的:
plane.texture = SKTexture(imageNamed: "TextureName")
注意:这一切都假设操纵杆处于固定位置并且有一个设定的中心
希望对您有所帮助!
我有一个由操纵杆控制的节点,当我向左移动操纵杆时,我想更改节点的图像,当我向右移动操纵杆时,我想改变同样的事情。我该怎么做?这是我的代码:
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 位置。
这些将是更新函数中的 if 语句,因此它会不断检查这一点。在语句中将是这样的:
plane.texture = SKTexture(imageNamed: "TextureName")
注意:这一切都假设操纵杆处于固定位置并且有一个设定的中心
希望对您有所帮助!