我有一个名为 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]];
.......