-1

我一直在开发我的第一个 SpriteKit 游戏,该游戏完美地在 iPhone 6 上运行,但在 iPhone 5 上却不能运行(两者都是iOS 8.2,用Swift编写)。我解释了我的问题:有一个简单的球(SkShapeNode)在点击屏幕时会弹跳。这一举动在 iPhone 6 上非常顺利,在 iPhone 5 上有些滞后,不是第一次。FPS一直是60 。我已经最大限度地简化了我的项目并且我已经尝试了一切,我真的非常需要你的帮助,我有点绝望。我准备尝试任何事情,任何帮助将不胜感激。非常感谢您!

这是视频第一次平滑然后滞后):

https://www.youtube.com/watch?v=xNxp2uGIJew

如果您想测试它,这里是存储库:

https://github.com/jonmars8/SpriteKit-Lag-iPhone5

这是GameScene中的代码:(空但它是简化前的菜单)

import SpriteKit

class GameScene: SKScene {

var viewController:GameViewController?

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    viewController?.presentPlayScene()
}
}

这是PlayScene中的代码:

import SpriteKit

let PlayerCategory: UInt32 = 1 << 2
let ObstacleCategory: UInt32 = 1 << 3

class PlayScene: SKScene, SKPhysicsContactDelegate {
let world = SKNode()
var player : SKShapeNode!
var firsttap = true
var viewController : GameViewController?

override func didMoveToView(view: SKView) {
    physicsWorld.gravity = CGVectorMake(0.0, -9.8)
    physicsWorld.contactDelegate = self

    self.createPlayer()

    let obstacle = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(100.0, 38.0))
    obstacle.position = CGPointMake(frame.size.width / 2.0, CGRectGetMidY(frame) * 0.75 + 400)
    let body = SKPhysicsBody(rectangleOfSize: obstacle.size)
    body.categoryBitMask = ObstacleCategory
    body.contactTestBitMask = PlayerCategory
    body.collisionBitMask = PlayerCategory
    body.linearDamping = 0.0
    body.angularDamping = 0.0
    body.dynamic = false
    obstacle.physicsBody = body

    self.addChild(world)
    self.world.addChild(obstacle)
    self.world.addChild(player)
}

func createPlayer() {
    self.player = SKShapeNode(circleOfRadius: 13)
    player.fillColor = SKColor.greenColor()
    player.strokeColor = SKColor.blackColor()
    player.lineWidth = 1
    let body = SKPhysicsBody(circleOfRadius:13)
    body.dynamic = false
    body.linearDamping = 0.0
    body.angularDamping = 0.0
    body.allowsRotation = false
    body.affectedByGravity = true
    body.categoryBitMask = PlayerCategory
    body.contactTestBitMask = ObstacleCategory
    body.collisionBitMask = ObstacleCategory
    player.physicsBody = body
    player.position = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame) * 0.75)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        if firsttap {
            player.physicsBody?.dynamic = true
            firsttap = false
        }
        let touchLoc = touch.locationInNode(self)
        var right = true
        if touchLoc.x > CGRectGetMidX(frame) {
            right = false
        }
        player.physicsBody?.velocity = right ?
            CGVectorMake(CGFloat(-65), CGFloat(650)) :
            CGVectorMake(CGFloat(65), CGFloat(650))
    }
}

override func update(currentTime: NSTimeInterval) {
    var pos = -player.position.y + CGRectGetMidY(frame)
    if pos < world.position.y {
        world.position = CGPointMake(world.position.x, pos)
    }
}

func didBeginContact(contact: SKPhysicsContact) {
    viewController?.presentGameScene()
}
}

这是gameViewController中的代码:

extension SKNode {
    class func unarchiveFromFile(file : NSString) -> SKNode? {
    if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
        var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)
        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData!)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")

        if file == "GameScene"{
            let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
            archiver.finishDecoding()
            return scene
        }
        else {
            let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as PlayScene
            archiver.finishDecoding()
            return scene
        }
        } else {
            return nil
        }
    }
}

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    self.presentGameScene()
}

func presentPlayScene() {
    if let scene = PlayScene.unarchiveFromFile("PlayScene") as? PlayScene {

        // Configure the view.
        let skView = self.view as SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        //reference to self
        scene.viewController = self
        skView.presentScene(scene, transition: SKTransition.crossFadeWithDuration(0.5))
    }
}

func presentGameScene() {
    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {

        // Configure the view.
        let skView = self.view as SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.showsQuadCount = true
        skView.showsDrawCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        //reference to self
        scene.viewController = self

        skView.presentScene(scene, transition: SKTransition.crossFadeWithDuration(0.5))
    }
}

override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    } else {
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}

override func prefersStatusBarHidden() -> Bool {
    return true
}
}

注意:在 iPhone 6 上,在游戏刚开始时快速点击屏幕会有非常小的延迟。

我已经用 SKSpriteNode 替换了 SKShapeNode 并且它没有改变任何东西。

请帮助我,非常感谢您!

4

2 回答 2

0

也许你可以尝试应用脉冲而不是直接改变速度:

 if(right){
    player.physicsBody?.velocity = CGVectorMake(0,0)
    player.physicsBody?.applyImpulse(CGVectorMake(-3, 15))
    }else{
    player.physicsBody?.velocity = CGVectorMake(0,0)
    player.physicsBody?.applyImpulse(CGVectorMake(3, 15))
    }

此外,我无法使用您的代码产生这种滞后(但仅在 iPhone 6 上测试过)......尽管尝试使用 update 与 didSimulatePhysics 进行试验。并尝试隔离导致滞后的部分。从 update 方法中删除代码以查看导致延迟的原因(球弹跳或移动世界节点或其他东西)。

于 2015-04-09T09:57:26.853 回答
0

我在这里发布了我的视图控制器代码。我没有观察到 iPhone 5 和 iPhone 6 之间的性能差异。你能试试我的代码,让我知道延迟是否仍然存在吗?

import UIKit
import SpriteKit

class GameViewController: UIViewController
{

    var playScene: PlayScene?

    override func viewDidLoad() {

    }

    override func viewDidAppear(animated: Bool) {
        let view = self.view as SKView
        playScene = PlayScene(size: view.bounds.size)
        playScene!.viewController = self
        view.showsFPS = true
        view.showsNodeCount = true
        view.presentScene(playScene!)
    }

    func presentGameScene()
    {

        let view = self.view as SKView
        playScene = PlayScene(size: view.bounds.size)
        playScene!.viewController = self
        view.presentScene(playScene!)
    }
}
于 2015-04-09T00:55:43.783 回答