如何优化以获得更好的帧速率?

时间:2015-09-17 17:42:06

标签: ios swift optimization sprite-kit

这是一款益智游戏。从屏幕截图中可以看出,屏幕上有256个节点,帧速率大约为10 FPS。

我看到很多益智游戏,我假设在屏幕上有数百个单独的节点,但帧速率很高......我想达到同样的效果。根据我的代码,我可以考虑哪些优化点? (下面的绘图代码)

我真的只是创建了一堆SKShapeNodes,将它们放入NSArray中,然后将它们作为子项添加到didMoveToView上的场景

class GameScene: SKScene {

    var grid: Grid! = nil

    override func didMoveToView(view: SKView) {

        self.grid = Grid()
        self.grid.initWithParent(parent: self, rows:16, columns:8, hexagonSize:40.0)
        self.grid.drawGrid()

    }

    override func update(currentTime: CFTimeInterval)
    {
        // nothing here!! why so slow..
    }
}

//…

class Grid : NSObject {

    // this all gets initialized later
    var hexagonSize: CGFloat! = nil
    var hexagonArray: NSMutableArray! = nil
    var numRows: Int! = nil
    var numColumns: Int! = nil
    var parentScene: SKScene? = nil

    // …

    func drawGrid(){

        //…

        if(self.parentScene != nil){

            for rowIdx in 0..<self.numRows {

                for colIdx in 0..<self.numColumns {

                    //…
                    let hex = Hexagon()
                    hex.initWithParentNode(node: self.parentScene!, size: self.hexagonSize)
                    hex.drawAtPoint(positionPoint: hexCenter)
                    self.hexagonArray.addObject(hex) hex.drawAtPoint(:positionPoint)
                }
            }
        }
    }

    func initWithParent(#parent: SKScene, rows: Int, columns: Int, hexagonSize: CGFloat){

        self.parentScene = parent
        self.numRows = rows
        self.numColumns = columns
        self.hexagonSize = hexagonSize
    }
}


//…

class Hexagon: NSObject {

    //…

    var parentNode : SKNode? = nil

    var size : CGFloat! = nil

    var shape : SKShapeNode! = nil

    func drawAtPoint(#positionPoint: CGPoint){

        let diameter = self.size

        let radius = diameter/2

        let normal = radius * sqrt(3)/2

        var path = CGPathCreateMutable()

        self.shape = SKShapeNode(path: path)

        let point = CGPointZero

        CGPathMoveToPoint(path, nil, point.x, point.y+radius)

        CGPathAddLineToPoint(path, nil, point.x+normal, point.y+(radius/2))
        CGPathAddLineToPoint(path, nil, point.x+normal, point.y-(radius/2))

        CGPathAddLineToPoint(path, nil, point.x, point.y-(diameter/2))

        CGPathAddLineToPoint(path, nil, point.x-normal, point.y-(radius/2))
        CGPathAddLineToPoint(path, nil, point.x-normal, point.y+(radius/2))

        CGPathAddLineToPoint(path, nil, point.x, point.y+radius)

        self.shape?.path = path
        self.shape?.fillColor = SKColor.blueColor()

        if(self.shape != nil && self.parentNode != nil){

            self.shape.position = positionPoint
            self.parentNode!.addChild(self.shape!)

        }
    }

    func initWithParentNode(#node: SKNode, size: CGFloat){

        self.parentNode = node
        self.size = size

    }
}

simulator screenshot

1 个答案:

答案 0 :(得分:6)

嗯......你是在屏幕上手动绘图......你应该让SpriteKit引擎有责任在屏幕上绘制你的精灵(它有很多优化来做到这一点,我们通常可以这样做)。

我的建议

首先丢弃您的课程GridHexagon

完成?好的:))

图像

  1. 获取iDraw
  2. 等软件
  3. 绘制透明背景的六边形
  4. 然后将其导出为hexagon.png(我为您准备了image
  5. 将其添加到Xcode Asset catalog
  6. Hexagon类

    创建文件Hexagon.swift并将以下代码写入其中

    import SpriteKit
    
    class Hexagon : SKSpriteNode {
        init() {
            let texture = SKTexture(imageNamed: "hexagon")
            super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    

    填充场景

    现在只需创建一些Hexagon(s)并将它们放置在您的场景中即可。

    class GameScene: SKScene {
        override func didMoveToView(view: SKView) {
            let hexagon0 = Hexagon()
            hexagon0.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
            self.addChild(hexagon0)
    
            // add more Hexagon(s) at different positions...             
        }
    }
    

    结论

    那就是它。 SpriteKit将负责绘制您添加到场景中的精灵。

    按照这种方法,你可以在屏幕上添加大量的精灵,SpriteKit将做一些我们凡人永远不会知道的优化,以保持尽可能高的帧速率。

    (Xcode 7和iOS 9的优化效果会更好,因为SpriteKit现在建立在Metal之上。)

    希望这有帮助。