ios - 重新启动场景时崩溃

标签 ios cocos2d-iphone dealloc sprite-sheet

所以,这是我的场景问题:我从菜单场景开始,然后进入游戏内场景,当角色死亡时,我再次进入菜单场景,所有这些都使用:

[[CCDirector sharedDirector] replaceScene:[MainMenu scene]];

[[CCDirector sharedDirector] replaceScene:[InGame scene]];

输掉游戏并尝试返回游戏后,我的 SpriteSheet 崩溃并出现错误:

'CCSprite is not using the same texture id'

这是我初始化动画的方法:

- (void) initSprite:(NSString *)plist andTexture:(NSString *)texture_ {

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:plist];

spriteSheet = [CCSpriteBatchNode batchNodeWithFile:texture_];

NSMutableArray *walkAnimFrames = [NSMutableArray array];

for (int i=1; i<=12; i++) {
    [walkAnimFrames addObject:
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
      [NSString stringWithFormat:@"%d.png",i]]];
}

CCAnimation *walkAnim = [CCAnimation animationWithSpriteFrames:walkAnimFrames delay:0.05f];

texture = [CCSprite spriteWithSpriteFrameName:@"1.png"];
walkAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:walkAnim]];
[texture runAction:walkAction];

texture.position = position;
texture.tag = HeroType;
[spriteSheet addChild:texture];

[self addChild:spriteSheet];
}

当我将纹理添加到 spriteSheet 时发生崩溃:

[spriteSheet addChild:texture];

我相信问题来自于纹理的释放..

我不使用 ARC。

最佳答案

您的缓存中可能有一个“1.png”,它对应于在退出重新启动序列之前创建的另一个动画。也许您想在重新启动之前清除 Sprite 帧缓存(可能还有很多其他东西)。

我完全避免通过“NNNN.png”使用“1.png”,因为很可能所有动画都会包含它们。相反,我使用类似的东西:

walk_classKey_upNNNN.png {上、下、左、右、空闲、跳跃...以及我需要的任何其他 map 姿态) fight_classKey_strikeNNNN.png {打击、伤害、死亡……例如)

classKey 是 {fighter,rogue, ... etc ...无论我有哪种独特的士兵类型)

我将我的 plists/textures 命名为相同

walk_fighter_up-hd.plist(使用纹理打包器,plist 嵌入了纹理名称)。 fight_rogue_cheapShot-hd.plist(cheapShot 是我当前游戏中盗贼的技能之一)。

关于ios - 重新启动场景时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18419088/

相关文章:

cocos2d-iphone - iads 与 cocos2d 的集成

ios - cocos2d/IOS中获取背景颜色

iphone - 如何在 xcode 中配置 Box2D

objective-c - 如何确保在应用程序终止时调用您的 dealloc 代码?

ios - 为什么将从反序列化的 WKWebView 中删除 url

ios - AudioKit:声像振荡器

ios - Xcode 添加多个字符串消息正文

ios - 使用强制性手机号码进行用户注册

Xcode 和 ARC 调试问题(跳过 dealloc)

ios - 内存管理。马洛克。 iOS