我正在使用Swift在iOS Spritekit中开发游戏。我创建了一个菜单,然后我称GameScene出现了混乱的滞后
但!如果我只加载游戏场景一切正常,如果我加载游戏,并改为另一个场景,则会造成滞后。
我在iPhone 5(iOS 8.1.x / 8.2 beta)Xcode(6.1.x / 6.2 beta)上进行了测试。在模拟器中一切都好
你可以在这个简单的代码上看到:
import SpriteKit
class GameScene: SKScene {
let kMovementSpeed:CGFloat = 3.8
var firstX:CGFloat = 0
override func didMoveToView(view: SKView) {
self.backgroundColor = SKColor.whiteColor()
self.anchorPoint = CGPointMake (0.5,0.5)
self.physicsBody = SKPhysicsBody (edgeLoopFromRect: self.frame)
addBall()
}
func addBall(){
var ball = SKShapeNode(circleOfRadius: CGFloat(30))
ball.name = "ball"
ball.fillColor = .redColor()
ball.position = CGPointMake (-200, 0)
ball.physicsBody = SKPhysicsBody (circleOfRadius: 30)
self.addChild(ball)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
var touch:UITouch!
touch = touches.anyObject() as UITouch
let location = touch.locationInNode(self)
if ((location.x <= 0) & (location.y <= 0)) {
firstX = location.x
}
else if ((location.x >= 0) & (location.y <= 0)) {
var ball = self.childNodeWithName("ball") as SKShapeNode
ball.physicsBody?.applyImpulse(CGVector(dx: 0,dy: 113))
}
else if ((location.x <= 0) & (location.y >= 0)) {
var ball = self.childNodeWithName("ball") as SKShapeNode
ball.removeAllActions()
var game:GameScene!
self.removeAllActions()
var moveSequence = SKAction.runBlock({
game = GameScene(size:(self.view?.frame.size)!)
game.scaleMode = .AspectFill
})
self.runAction(moveSequence, completion: {
self.view?.presentScene(game)
return
})
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if ((location.x <= 0) & (location.y <= 0)) {
var ball = self.childNodeWithName("ball") as SKShapeNode
let ySpeed = ball.physicsBody?.velocity.dy
ball.physicsBody?.velocity.dx = kMovementSpeed * (location.x - firstX)
}
}
}
}
左上角 - 代表场景
左下角 - 简单的操纵杆。
右下角 - 跳跃。
我做错了什么?