1

我有一个名为 Textures 的类,它使用保存一些这样的数据

//Textures.h
#import <Foundation/Foundation.h>

@interface Textures
{
    CCTexture2D *Balloon_RED;
    CCTexture2D *Balloon_POP;
}
@property (nonatomic, retain) CCTexture2D* Balloon_RED;
@property (nonatomic, retain) CCTexture2D* Balloon_POP;

-(void)setTextures;
+(CCTexture2D*) cacheImg: (NSString*) image;
@end

//Textures.m
#import "Textures.h"

@implementation Textures
@synthesize Balloon_RED;
@synthesize Balloon_POP;
-(void)setTextures
{
    Balloon_RED = [Textures cacheImg:@"red.png"];
    Balloon_POP = [Textures cacheImg:@"pop.png"];
}
+(CCTexture2D*)cacheImg: (NSString*)image
{
    return [[CCTextureCache sharedTextureCache] addImage:image];
}
@end

我在我的主类中使用它,如下所示:(SpriteTextures是“ Textures ”类型,BalloonSprite是“ Balloon ”类型,这是我的CCSprite子类)

[SpriteTextures setTextures];
BalloonSprite = [Balloon spriteWithTexture: [SpriteTextures Balloon_RED]];

启动画面加载后,我收到一条错误消息,指出我正在使用的纹理无效:

Assertion failure in -[Balloon initWithTexture:], /Users/Mark/Kobold2D/Kobold2D-1.0.5/__Kobold2D__/libs/cocos2d-iphone/cocos2d/CCSprite.m:192
2012-03-02 20:00:18.011 Game-iOS[1341:1ca03] ERROR: Uncaught exception Invalid texture for sprite
2012-03-02 20:00:18.011 Game-iOS[1341:1ca03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid texture for sprite'

附加信息是我正在使用 Kobold,正如您在错误日志中看到的那样,但这不应该真正有所作为我确定我可能在某处做错了什么。

任何帮助表示赞赏,谢谢!

编辑 资源文件夹的样子,它们也在物理驱动器的文件夹中。 资源文件夹

此外,这很好用(将 texture2ds 添加到主类)

Texture_RED = [[CCTextureCache sharedTextureCache] addImage:@"red.png"];
Texture_POP = [[CCTextureCache sharedTextureCache] addImage:@"pop.png"];

BalloonSprite = [Balloon spriteWithTexture: Texture_RED];

或者说真的,总的来说,有没有更好的方法来组织一个拥有近 100 个不同图像的游戏的大量精灵(许多精灵需要改变纹理)?

编辑

主类 (*.h)

#import "Textures.h"

@interface MainClass : CCLayer
{
    Textures *SpriteTextures;
}
-(void)loop;
@property (retain) Textures *SpriteTextures;

执行

-(id) init
{
    if ((self = [super init]))
    {
        self.isTouchEnabled = true;
        [SpriteTextures setTextures];

        BalloonSprite = [Balloon spriteWithTexture: [SpriteTextures Balloon_RED]];
        .......
4

2 回答 2

1

NSAsserts 是一个真正的痛苦,但是当在它之上消息是无用的:) ...好的,看看 cocos2d (CCSprite) 中的第 192 行,纹理是 nil (导致断言失败)。在将纹理添加到 sharedTextureCache 时,检查是否收到与纹理类似的消息。

2012-03-02 21:19:43.497 我的游戏 [55224:12203] cocos2d: CCTexture2D。无法创建纹理。UIImage 为零

2012-03-02 21:19:43.498 MyGame[55224:12203] cocos2d:无法在 CCTextureCache 中添加图像:a.png

如果您这样做了,则尚未找到纹理。确保它们在您的资源文件夹中,并且它们是您正在构建的目标的成员。

第 2 部分:只需重新阅读您的代码,如果 SpriteTextures 在语句中为 nil,您将得到相同的错误

[SpriteTextures setTextures];
BalloonSprite = [Balloon spriteWithTexture: [SpriteTextures Balloon_RED]];

这可以解释为什么它在一个地方有效,而在另一个地方无效。

第 3 部分:您没有在类 init 中初始化或设置 SpritTextures。尝试:

    self.isTouchEnabled = true;
    self.SpriteTextures = [[Textures alloc] init];
    [SpriteTextures setTextures];
于 2012-03-03T02:25:47.727 回答
1

我相信答案比你想象的要简单..!

Texture_RED = [[CCTextureCache sharedTextureCache] addImage:@"red.png"];
Texture_POP = [[CCTextureCache sharedTextureCache] addImage:@"pop.png"];

纹理未保留。尝试:

self.Texture_RED = [[CCTextureCache sharedTextureCache] addImage:@"red.png"];
self.Texture_POP = [[CCTextureCache sharedTextureCache] addImage:@"pop.png"];

如果你需要一个理由,这是我的理由:

        if( tex )
            [textures_ setObject: tex forKey:path];
        else
            CCLOG(@"cocos2d: Couldn't add image:%@ in CCTextureCache", path);

        // autorelease prevents possible crash in multithreaded environments
        [tex autorelease];  

之前的代码在

CCTextureCache addImage:

这意味着,你应该保留你的纹理。

此外,即使 CCTextureCache 以某种方式将其保留在其他地方,缓存也会在收到来自操作系统的内存警告时被清除,这可能会导致您的游戏在发生这种情况时崩溃。

于 2012-03-03T09:01:49.817 回答