0
hello:D我有下面这段代码。用快捷键在游戏杆中移动精灵Xcode
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 */
}
}
上面的代码创建了一个操纵杆和一个船。通过移动操纵杆,我可以用操纵杆来旋转“船”。但是,我想按照操纵杆的方向移动船。我如何解决这个问题?谢谢。
对于那些未来的读者,如果你想在'touches ended'方法中使用'xJoystickDelta = 0'和'yJoystickDelta = 0'来让你的角色或者你正在移动的任何东西停止移动操纵杆 –