精灵套件:许多精灵(1000+)与Bit Blitting

时间:2015-08-14 13:46:44

标签: ios swift sprite-kit bitblt

我正在尝试使用SpriteKit创建一个具有数千个精灵(~500 - 2000)的场景。每个精灵只是一个白色像素1x1 - 甚至不需要使用纹理。

直接一次性地将这么多精灵添加到场景是不可能的(或者至少我认为是这样)。在iPhone 6上我最终添加了大约200个精灵,然后系统因内存而结束添加过程,并且不添加其余的精灵。

我找到了一个聪明的解决方案叫做Bit Blitting,其中所有精灵都被添加到一个节点,然后用override func didMoveToView(view: SKView) { self.generateRandomSprites() } func generateRandomSprites() { let texture = SKTexture(imageNamed: "whitePixel") self.canvasNode = SKNode() log("started generating all sprites") for var i = 0;i < 1000;i++ { let width = self.scene!.frame.size.width let height = self.scene!.frame.size.height let x: CGFloat = CGFloat(arc4random_uniform(UInt32(width))) let y: CGFloat = CGFloat(arc4random_uniform(UInt32(height))) let cell = SKSpriteNode(texture: texture) cell.position = CGPointMake(x, y) self.arrCells.append(cell) self.canvasNode.addChild(cell) } self.canvasTexture = self.view!.textureFromNode(self.canvasNode) self.canvasSprite = SKSpriteNode(texture: self.canvasTexture, size: self.frame.size) self.canvasSprite.anchorPoint = ccp(0,0) self.addChild(self.canvasSprite) } override func update(currentTime: CFTimeInterval) { for oneCell in self.arrCells { oneCell.position = CGPointMake(oneCell.position.x + 1, oneCell.position.y) } self.canvasTexture = self.view!.textureFromNode(self.canvasNode) self.canvasSprite.texture = self.canvasTexture } 方法“转换”为纹理,然后从这个纹理创建一个单个精灵,最终会添加到屏幕上。它效果很好,我可以用很棒的fps一次创建超过10 000个精灵。

我的问题是我之后无法移动这些精灵(=改变位置)。无论我做什么,纹理总是保持不变。我错过了什么?

我的代码:

00C6A290

app的截图(这是静态的,没有任何反应):

enter image description here

2 个答案:

答案 0 :(得分:2)

这不是答案,而是我尝试过的一系列事情。

我对这段代码很好奇。我尝试过它并没有用。我更改了更新方法,只是将点放在随机位置,它可以工作:

所以我有一个问题。为什么代码不能从它的当前位置更新,或者是红鲱鱼?

override func update(currentTime: CFTimeInterval) {

    let width = self.scene!.frame.size.width
    let height = self.scene!.frame.size.height

    for oneCell in self.canvasNode.children as! [SKSpriteNode] {

        let x: CGFloat = CGFloat(arc4random_uniform(UInt32(width)))
        let y: CGFloat = CGFloat(arc4random_uniform(UInt32(height)))

        oneCell.position = CGPointMake(x, y)
    }

    self.canvasTexture = self.view!.textureFromNode(self.canvasNode)
    self.canvasSprite.texture = self.canvasTexture

}
好吧,我一直在玩这个,这很有用,只是获得一个随机值并将其添加到该位置......

override func update(currentTime: CFTimeInterval) {

    let width = self.scene!.frame.size.width
    let height = self.scene!.frame.size.height

    for oneCell in self.canvasNode.children as! [SKSpriteNode] {

        let x2: CGFloat = CGFloat(arc4random_uniform(UInt32(10)))
        let y2: CGFloat = CGFloat(arc4random_uniform(UInt32(10)))
        let x:CGFloat = oneCell.position.x - x2
        let y:CGFloat = oneCell.position.y - y2
        let point = CGPointMake(x, y)

        oneCell.position = point
    }

    self.canvasTexture = self.view!.textureFromNode(self.canvasNode)
    self.canvasSprite.texture = self.canvasTexture

}

现在我认为这与编译器有关,因为它有效:

override func update(currentTime: CFTimeInterval) {

    let width = self.scene!.frame.size.width
    let height = self.scene!.frame.size.height


    var x3:CGFloat = 10.0
    var y3:CGFloat = 10.0
    for oneCell in self.canvasNode.children as! [SKSpriteNode] {

        let x:CGFloat = oneCell.position.x - x3
        let y:CGFloat = oneCell.position.y - y3
        let point = CGPointMake(x, y)

        oneCell.position = point
        x3 += 0.01
    }


    self.canvasTexture = self.view!.textureFromNode(self.canvasNode)
    self.canvasSprite.texture = self.canvasTexture

}

更多调查,有趣的是,如果你使用值1.3并运行代码,你可以看到点移一次。 (http://llvm.org/docs/LangRef.html#simple-constants

override func update(currentTime: CFTimeInterval) {

    let width = self.scene!.frame.size.width
    let height = self.scene!.frame.size.height

    for oneCell in self.canvasNode.children as! [SKSpriteNode] {

        let x:CGFloat = oneCell.position.x - 1.3
        let y:CGFloat = oneCell.position.y - 1.3
        let point = CGPointMake(x, y)

        oneCell.position = point
    }

    self.canvasTexture = self.view!.textureFromNode(self.canvasNode)
    self.canvasSprite.texture = self.canvasTexture

}

像50.3333333333这样的数字显示可见的震动,但似乎停止了。

答案 1 :(得分:0)

说实话,我认为你建议的方法不会表现得很好,而且看起来真的很复杂。例如,使用SpriteKit,您可以创建一个具有背景纹理颜色的节点,并使其成为1x1,而不会执行任何此类blit。像这样:

$(document).ready(function () {
        $('select[id^=ddl]').change(function () { // target all select element whose ID starts with ddl
            var selectedValue = $(this).find('option:selected').val();
            $("input[id*=tbHazRat]").val(selectedValue);                
        });
    });