我刚开始学习 Sprite Kit。我尝试制作一个简单的球弹跳游戏,有两个玩家,另一个玩家慢慢追踪球的位置。但是我发现了一个问题:当我把线移动到球(和边缘)时,球会从屏幕上消失。有时候球会反弹,就没有问题了。这是什么问题?
我只有一个 GameScene、sks 文件和 ViewController。我的精灵节点来自于 sks 文件。如果有人能解释一下这种情况,那就太好了。我在下面附上了我的代码。
我的 GameScene:
我尝试了一些更新功能中的消息,然后遇到了相同的情况,即球从设备左侧飞出(使用 iPhone 6S)。 2016年12月8日14:27:54.436485 Pong[14261:3102941] 致命错误:球出了左边界:文件
我只有一个 GameScene、sks 文件和 ViewController。我的精灵节点来自于 sks 文件。如果有人能解释一下这种情况,那就太好了。我在下面附上了我的代码。
我的 GameScene:
class GameScene: SKScene {
var ball = SKSpriteNode()
var enemy = SKSpriteNode()
var main = SKSpriteNode()
override func didMove(to view: SKView) {
ball = self.childNode(withName: "ball") as! SKSpriteNode
enemy = self.childNode(withName: "enemy") as! SKSpriteNode
main = self.childNode(withName: "main") as! SKSpriteNode
ball.physicsBody?.applyImpulse(CGVector(dx: -20, dy: -20))
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.angularDamping = 0
let border = SKPhysicsBody(edgeLoopFrom: self.frame)
border.friction = 0
border.restitution = 1
self.physicsBody = border
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
main.run(SKAction.moveTo(x: location.x, duration: 0.2))
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
main.run(SKAction.moveTo(x: location.x, duration: 0.2))
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.5))
}
视图控制器:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
}
}
override var prefersStatusBarHidden: Bool {
return true
}
Pad设置:
球设置: 一些更新:我尝试了一些更新功能中的消息,然后遇到了相同的情况,即球从设备左侧飞出(使用 iPhone 6S)。 2016年12月8日14:27:54.436485 Pong[14261:3102941] 致命错误:球出了左边界:文件