gpt4 book ai didi

ios - 当我向左或向右移动操纵杆时,如何更改节点的纹理?

转载 作者:行者123 更新时间:2023-11-28 06:36:08 25 4
gpt4 key购买 nike

我有一个由操纵杆控制的节点,当我将操纵杆向左移动时,我想更改节点的图像,当我将操纵杆向右移动时,我想更改相同的内容。我该怎么做?这是我的代码:

    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")

注意:这一切都假设操纵杆处于固定位置并具有设定的中心

希望这对您有所帮助!

关于ios - 当我向左或向右移动操纵杆时,如何更改节点的纹理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39060207/

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