Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何优化以获得更好的帧速率?_Ios_Swift_Optimization_Sprite Kit - Fatal编程技术网

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

Ios 如何优化以获得更好的帧速率?,ios,swift,optimization,sprite-kit,Ios,Swift,Optimization,Sprite Kit,这是一个益智游戏。从屏幕截图中可以看到,屏幕上有256个节点,帧速率徘徊在10 FPS左右 我看到很多益智游戏,我假设屏幕上有数百个独立的节点,但帧速率很高。。。我也希望实现同样的目标。根据我的代码,我可以考虑哪些优化点?(图纸代码如下) 我实际上只是创建了一堆SKShapeNodes,将它们放入一个NSArray,然后将它们作为孩子添加到didMoveToView的场景中 class GameScene: SKScene { var grid: Grid! = nil ov

这是一个益智游戏。从屏幕截图中可以看到,屏幕上有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

    }
}
class游戏场景:SKScene{
变量网格:网格!=nil
覆盖func didMoveToView(视图:SKView){
self.grid=grid()
self.grid.initWithParent(父:self,行:16,列:8,六边形大小:40.0)
self.grid.drawGrid()
}
覆盖函数更新(currentTime:CFTimeInterval)
{
//这里什么都没有!!为什么这么慢。。
}
}
//…
类网格:NSObject{
//这些都将在稍后初始化
变量六边形大小:CGFloat!=nil
var hexagonArray:NSMutableArray!=nil
var numRows:Int!=nil
变量numColumns:Int!=nil
var parentScene:SKScene?=nil
// …
func drawGrid(){
//…
if(self.parentScene!=nil){

对于0中的rowIdx…嗯…您正在屏幕上手动绘制…您应该让SpriteKit引擎负责在屏幕上绘制您的精灵(它进行了大量优化,可以比我们通常做的更好)

我的建议 首先,扔掉你的类
Grid
Hexagon

完成了吗?很好:)

形象
  • 得到一个像这样的软件
  • 画一个背景透明的六边形
  • 然后将其导出为
    hexagon.png
    (我为您准备了
  • 将其添加到
    Xcode资产目录
  • 六边形类 创建一个文件
    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
    并将它们放置在场景中

    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将负责绘制您添加到场景中的精灵

    按照这种方法,你可以在屏幕上添加大量的精灵,精灵将进行我们人类永远不知道的优化,以保持尽可能高的帧率

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


    希望这有帮助。

    使用
    CG…
    原语绘图相对来说比较昂贵。如果你所有的单元格看起来都一样,你可以在位图上下文中绘制它们一次,然后将该位图在屏幕上放置256次。该死的,这很有帮助!谢谢你的精彩回答,甚至还为我创建了一个精灵图像。我几乎说不出话来了ss!享受你的项目:-)