gpt4 book ai didi

xcode - Swift:收到内存警告后崩溃

转载 作者:行者123 更新时间:2023-11-30 13:43:11 26 4
gpt4 key购买 nike

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

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

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)

最佳答案

我做了很多调试,发现如果我去掉所有对象的物理特性,内存泄漏就会消失。所以看来问题出在游戏物理上,这很奇怪。

然后我做了很多研究,发现显示游戏物理需要大量内存。因此,我只需从 GameViewController 中删除 skView.showsPhysics = true 行,问题就解决了。

关于xcode - Swift:收到内存警告后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35301800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com