gpt4 book ai didi

xcode - 在 swift Xcode 中用操纵杆移动 Sprite

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

你好 :D 我在下面有这段代码。

import SpriteKit

class GameScene: SKScene {

let base = SKSpriteNode(imageNamed: "yellowArt/Base")
let ball = SKSpriteNode(imageNamed: "yellowArt/Ball")
let ship = SKSpriteNode(imageNamed: "yellowArt/Ship")

var stickActive:Bool = false


override func didMoveToView(view: SKView) {





self.backgroundColor = SKColor.blackColor()
self.anchorPoint = CGPointMake(0.5, 0.5)

self.addChild(base)
base.position = CGPointMake(0, -200)

self.addChild(ball)
ball.position = base.position

self.addChild(ship)
ship.position = CGPointMake(0, 200)

ball.alpha = 0.4
base.alpha = 0.4
}




override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in (touches ) {
let location = touch.locationInNode(self)

if (CGRectContainsPoint(ball.frame, location)) {
stickActive = true
} else {
stickActive = false
}
}
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

for touch in (touches ) {

let location = touch.locationInNode(self)


if (stickActive == true) {

let v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y)
let angle = atan2(v.dy, v.dx)

let deg = angle * CGFloat( 180 / M_PI)
print( deg + 180 )

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

ball.position = location

} else {

ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)

ship.zRotation = angle - 1.57079633
ship.position = CGPointMake(angle, angle)



}
} // ends stick active test
}
}


override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (stickActive == true) {
let move: SKAction = SKAction.moveTo(base.position, duration: 0.2)
move.timingMode = .EaseOut
ball.runAction(move)
}
}


override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}

上面的代码创建了一个操纵杆和一艘船。通过移动操纵杆,我可以用操纵杆旋转“船”。但是,我想将船朝操纵杆所握的方向移动。我该如何解决这个问题?谢谢。

最佳答案

你的船不会去任何地方,因为你有 ship.position = CGPointMake(angle, angle)。 只要你移动操纵杆,船就会去那一点 - 删除那条线。

我会使用 update 方法来移动飞船。首先,您需要找到您想要在 x 和 y 方向上移动多少。在 var stickActive:Bool = false 语句下面为操纵杆移动创建类变量:

var xJoystickDelta = CGFloat()
var yJoystickDelta = CGFloat()

将以下代码放入您的 touchesMoved 方法中:

xJoystickDelta = location.x - base.position.x
yJoystickDelta = location.y - base.position.y

将以下代码放入您的update 方法中:

let xScale = 1.0 //adjust to your preference
let yScale = 1.0 //adjust to your preference

let xAdd = xScale * self.xJoytickDelta
let yAdd = yScale * self.yJoystickDelta

self.ship.position.x += xAdd
self.ship.position.y += yAdd

希望对您有所帮助。

关于xcode - 在 swift Xcode 中用操纵杆移动 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32761001/

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