xcode - 一旦我最终使用它,我是否必须删除一个 sprite 节点?

标签 xcode swift xcode6

我正在我的场景中创建一系列移动的管道。但它总是在生成约 30 个管道后崩溃。是不是因为场景中的节点太多,没有新的内存?代码是这样的:

import SpriteKit

class GameScene: SKScene {

var mainPipe: SKSpriteNode = SKSpriteNode()
var space:Float = 1000
var pipeCount:Int = 0


override func didMoveToView(view: SKView) {
    self.backgroundColor = SKColor.blackColor()
    self.size.width = 640
    self.size.height = 1136


}

func randomOffset() -> Float{


    var rNum:Float = Float(arc4random()%181)    // 0-180

    return rNum
}

var durations: CFloat = 5.0
var colorPipes:UIColor = UIColor.grayColor()


func spawnPipeRow(offs:Float){

    self.pipeCount = self.pipeCount + 1
    println("\(self.pipeCount)")

    //offs is the random number
    //let offset = offs + (space/2) - 105
    let offset = offs + Float(self.size.height/100) - 180

    // mainPipe = SKSpriteNode(color:colorPipes, size:CGSize(width: view.bounds.size.width/3, height:700))
    mainPipe = SKSpriteNode(color:colorPipes, size:CGSize(width: self.size.width/5, height:self.size.height/1.5))



    let pipeBottom = (mainPipe as SKSpriteNode).copy() as SKSpriteNode
    let pipeTop    = (mainPipe as SKSpriteNode).copy() as SKSpriteNode

    let xx = self.size.width * 2.0
    self.setPositionRelativeBot(pipeBottom, x:Float(xx), y: offset )
    self.setPositionRelativeTop(pipeTop, x:Float(xx), y: offset + space)

    pipeBottom.physicsBody = SKPhysicsBody(rectangleOfSize: pipeBottom.size)
    pipeTop.physicsBody = SKPhysicsBody(rectangleOfSize: pipeTop.size)

    pipeBottom.physicsBody?.dynamic = false
    pipeTop.physicsBody?.dynamic = false

    //pipeTop.physicsBody?.contactTestBitMask = birdCategory
    //pipeBottom.physicsBody?.contactTestBitMask = birdCategory


    self.addChild(pipeBottom)
    self.addChild(pipeTop)

    var actionArray1:NSMutableArray = NSMutableArray()

    actionArray1.addObject(SKAction.moveTo(CGPointMake(-1000, pipeBottom.size.height - 200), duration: NSTimeInterval(durations)))
    var actionArray2:NSMutableArray = NSMutableArray()

    actionArray2.addObject(SKAction.moveTo(CGPointMake(-1000, pipeTop.size.height - 200), duration: NSTimeInterval(durations)))

    actionArray1.addObject(SKAction.removeFromParent())
    actionArray2.addObject(SKAction.removeFromParent())

    pipeBottom.runAction(SKAction.sequence(actionArray1))
    pipeTop.runAction(SKAction.sequence(actionArray2))

}




override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {

    }




}

override func update(currentTime: CFTimeInterval) {
    var timeSinceLastUpdate = currentTime - lastUpdateTimerInterval
    lastUpdateTimerInterval = currentTime

    if(timeSinceLastUpdate > 1){
        timeSinceLastUpdate = 1/60
        lastUpdateTimerInterval=currentTime


    }
    updateWithTimeSinceLastUpdate(timeSinceLastUpdate)



    /* Called before each frame is rendered */
}

func setPositionRelativeBot(node:SKSpriteNode, x: Float, y: Float){

    let xx = (Float(node.size.width)/2) + x
    let yy = (Float(self.size.height)/2) - (Float(node.size.height)/2) + y
    node.position.x = CGFloat(xx)
    node.position.y = CGFloat(yy)

}
func setPositionRelativeTop(node:SKSpriteNode, x:Float, y:Float){
    let xx = (Float(node.size.width)/2) + x
    let yy = (Float(self.size.height)/2) + (Float(node.size.height)/2) + y
    node.position.x = CGFloat(xx)
    node.position.y = CGFloat(yy)

}

var lastUpdateTimerInterval:NSTimeInterval = NSTimeInterval()
var lastYieldTimeInterval:NSTimeInterval = NSTimeInterval()
var speedOfBird: CDouble = 1.8
func updateWithTimeSinceLastUpdate(timeSinceLastUpdate:CFTimeInterval){
    lastYieldTimeInterval += timeSinceLastUpdate
    if(lastYieldTimeInterval > speedOfBird ){
        lastYieldTimeInterval=0
        self.spawnPipeRow(self.randomOffset())
        if speedOfBird > 0.8{
            speedOfBird -= 0.1}
    }
}

}

最佳答案

当您不再需要 Sprite 时,您应该将它们从场景中移除。但是,您的问题可能与纹理占用的内存无关:

SpriteKit Programming Guide

An SKTexture object is created and attached to the sprite. This texture object automatically loads the texture data whenever the sprite node is in the scene, is visible, and is necessary for rendering the scene. Later, if the sprite is removed from the scene or is no longer visible, Sprite Kit can delete the texture data if it needs that memory for other purposes. This automatic memory management simplifies but does not eliminate the work you need to do to manage art assets in your game.

The texture object itself is just a placeholder for the actual texture data. The texture data is more resource intensive, so Sprite Kit loads it into memory only when needed.

If you already have an SKTexture object, you can create new textures that reference a portion of it. This approach is efficient because the new texture objects reference the same texture data in memory.

关于xcode - 一旦我最终使用它,我是否必须删除一个 sprite 节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26203487/

相关文章:

objective-c - 适用于 iPhone 6 plus 的 Xcode 6 启动图像

submit - 错误 ITMS-4088 - 无权修改应用程序

objective-c - iPad 应用程序仅在设备上而非模拟器上加载 Nib 时崩溃

ios - 如何在 iOS8 键盘扩展中检测 iPad 方向

ios - IBDesignable - IBOutlet 在属性 didSet 方法中为 nil

ios - 按下时如何隐藏底部栏但保留在主视图 Controller 中

ios7 - NSLayoutConstraints 在 ios7 上崩溃但在 ios8 上没有

ios - 如何使用后缀中的 CFBundleShortVersionString 动态获取 "Archive Name"(在编辑方案/存档/存档名称中)?

ios - 构建依赖于 GoogleAnalytics-iOS-SDK 的 Cocoapod 验证失败

iPhone - 如何在进入前景后恢复帧大小?