Swift:接收内存警告然后崩溃

时间:2016-02-09 20:43:40

标签: xcode swift

我在收到应用程序几分钟后收到内存警告,然后崩溃(来自调试器的消息:由于内存问题而终止

我已经在互联网上阅读了一些答案,我认为可能会发生这种情况,因为我将精灵动画存储在字典数组中,如下所示:

class CharacterCreate {

    var player = SKSpriteNode()
    var playerAtlas = SKTextureAtlas()
    var playerAnimation = [String : [SKTexture]]()
    let playerAnimationTypes = ["run", "static", "jump", "armed_static", "armed_run", "armed_jump"] // Player's animations
    var playerAnimationsArray = [String]() // Array with all player Sprites
    let playerAnimationSpritesPrefix = "Player_"
    let runAnimationTime = 0.1
    init(initPosition: CGPoint) {

        playerAtlas = SKTextureAtlas(named: "Player_anim")

        for i in 0 ..< playerAtlas.textureNames.count {
            playerAnimationsArray.append(playerAtlas.textureNames[i]) // Gets all the animations image names
        }

        for i in 0 ..< playerAnimationTypes.count {

            let animations = playerAnimationsArray.filter{$0.hasPrefix(playerAnimationSpritesPrefix + playerAnimationTypes[i])}.sort() // Gets all image names for a specific type of animation (eg: Player_run0.png, Player_run1.png, etc)

            for j in 0 ..< animations.count {

                if playerAnimation[playerAnimationTypes[i]] == nil {
                    playerAnimation[playerAnimationTypes[i]] = [] // Creates array if doesn't exist
                }

                playerAnimation[playerAnimationTypes[i]]!.append(SKTexture(imageNamed: animations[j])) // Add texture to make the animation

            }

        }

我不知道问题是否发生是因为我存储动画的方式,或者是否因为其他事情而发生,但它总是发生在开启后15分钟应用程序。如果你们需要任何其他信息,我很乐意提供它

@edit:我也不知道问题是否因为这个问题而发生,但我在更新功能上有一些不断变化的值,因为它在每一帧被调用,我认为它也可能超载使用的记忆力,我不知道

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

    if (Player.playerOldY != Player.player.position.y && !Player.playerOnGround) {
        Player.playerOnAir = true
        if (Player.playerRunningJumpAnimation == false) {
            Player.playerRunningJumpAnimation = true
            spriteAnimation(Player.player, action: Player.playerAnimation["jump"]!, time: Player.runAnimationTime)
        }
    }

    Player.playerOldY = Player.player.position.y
    Player.playerOldX = Player.player.position.x

    if pressedButtons.indexOf(directionButtons["left"]!) != nil {
        Player.player.position.x -= Player.playerWalkingSpeed
        Player.playerDirection = (pressedButtons.count > 1 ? Player.playerDirection : -1)
        Player.player.xScale = Player.playerScale * Player.playerDirection
    }
    if pressedButtons.indexOf(directionButtons["right"]!) != nil {
        Player.player.position.x += Player.playerWalkingSpeed
        Player.playerDirection = (pressedButtons.count > 1 ? Player.playerDirection : 1)
        Player.player.xScale = Player.playerScale * Player.playerDirection
    }

}

@ edit2:所以,我发现,如果我移除地面和玩家,内存就会停止增长。但如果我只添加其中一个,则内存开始不断增长

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    // Ground Create
    ground = SKSpriteNode(imageNamed: "grassSprite")
    ground.setScale(3)
    ground.position = CGPoint(x: self.frame.width / 2, y: 100 + ground.frame.height)

    // Ground Physics
    ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size)
    ground.physicsBody?.categoryBitMask = PhysicsCategory.Ground
    ground.physicsBody?.collisionBitMask = PhysicsCategory.Player
    ground.physicsBody?.contactTestBitMask = PhysicsCategory.Player
    ground.physicsBody?.affectedByGravity = false
    ground.physicsBody?.dynamic = false
    ground.physicsBody?.restitution = 0
    self.addChild(ground)


    self.addChild(Player.player)

1 个答案:

答案 0 :(得分:0)

我做了很多调试,发现如果我拿出所有对象的物理特性,内存泄漏就会消失。所以似乎问题出在游戏物理上,这是非常奇怪的事情。

然后我做了很多研究,发现显示游戏物理需要大量的记忆。所以,我只需要从GameViewController中取消skView.showsPhysics = true行,问题就解决了。