【发布时间】:2017-11-30 23:32:03
【问题描述】:
我的问题是如何将我的精灵与操纵杆一起进行连续运动?我希望玩家一直以一定的速度移动,然后每当用户将操纵杆移动一定角度时,玩家就会跟随它。
ex:Sprite 自动移动一定的速度,例如; speed = CGFloat(100),然后它将继续以这个速度,通过速度精灵会自动向一个方向移动,我不知道如何实现它,以便我的玩家以恒定的速度移动,以及如何改变方向是用操纵杆移动。
目前使用我的代码我可以旋转精灵。
感谢你们提供的所有帮助,在此先感谢您。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
touchLocation = touch.location(in: self)
if (ball.frame.contains(touchLocation)) {
stickActive = true
} else {
stickActive = false
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
touchLocation = touch.location(in: self)
if isAlive == true{
joyStickMoved()
}
if isAlive == false{
player.position.x = -300
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if (stickActive == true) {
let move: SKAction = SKAction.move(to: base.position, duration: 0.2)
move.timingMode = .easeOut
ball.run(move)
}
}
func joyStickMoved() {
if (stickActive == true) {
let v = CGVector(dx: touchLocation.x - base.position.x, dy: touchLocation.y - base.position.y)
let angle = atan2(v.dy, v.dx)
// let degree = angle * CGFloat( 180 / Double.pi)
let length: CGFloat = base.frame.size.height / 2
let xDist: CGFloat = sin(angle - 1.57079633) * length
let yDist: CGFloat = cos(angle - 1.57079633) * length
// TODO // xJoystick = touchLocation.x - base.position.x
// yJoystick = touchLocation.y - base.position.y
if (base.frame.contains(touchLocation)) {
ball.position = touchLocation
} else {
ball.position = CGPoint(x: base.position.x - xDist, y: base.position.y + yDist)
}
player.zRotation = angle - 1.57079633
}
}
override func update(_ currentTime: TimeInterval) {
if isAlive == false && stickActive == false {
lblMain.text = "Game Over"
player.position.x = -300
waitThenResetTheGame()
}
if stickActive == true {
moveNodeToLocation()
}
}
func spawnPlayer(){
player = SKSpriteNode(color: offWhiteColor, size: playerSize)
player.position = CGPoint(x: self.frame.midX, y: 130)
player.physicsBody = SKPhysicsBody(rectangleOf: (player.size))
player.physicsBody?.affectedByGravity = false
player.physicsBody?.categoryBitMask = physicsCategory.player
player.physicsBody?.contactTestBitMask = physicsCategory.fallingBlock
player.physicsBody?.isDynamic = false
player.physicsBody?.allowsRotation = false
player.physicsBody?.angularVelocity = 5
player.physicsBody?.angularDamping = 0
player.name = "player"
self.addChild(player)
setupFollower()
}
【问题讨论】:
-
信息太少,无法确定如何解决这个问题,并将您的来源发布到问题中,不要使用 png。 SO 用于在格式正确时处理代码。
-
@Knight0fDragon 嘿,感谢骑士的快速响应,我按照你的建议格式化了文本,现在看起来更干净了,谢谢。
标签: swift3 sprite-kit sprite joystick