I have a class called Textures that use holds some data like this
//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
And I use it in my main class like this: (SpriteTextures is of type "Textures" and BalloonSprite is of type "Balloon" which is my subclass of CCSprite)
[SpriteTextures setTextures];
BalloonSprite = [Balloon spriteWithTexture: [SpriteTextures Balloon_RED]];
After the splash screen loads I get an error saying its an invalid texture I'm using:
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'
Additional Info is that I'm using Kobold as you can see in the error log but that shouldn't really make a difference I'm sure I'm probably doing something wrong somewhere.
Any help is appreciated, thanks!
Edit
What the resources folder looks like, they are also in folders of the physical drive too.
Also, this works just fine (adding the texture2ds to main class)
Texture_RED = [[CCTextureCache sharedTextureCache] addImage:@"red.png"];
Texture_POP = [[CCTextureCache sharedTextureCache] addImage:@"pop.png"];
BalloonSprite = [Balloon spriteWithTexture: Texture_RED];
Or really, overall, is there a better way to organize a lot of sprites (that many need to change texture) for a game with close to 100 different images?
Edit
Main Class (*.h)
#import "Textures.h"
@interface MainClass : CCLayer
{
Textures *SpriteTextures;
}
-(void)loop;
@property (retain) Textures *SpriteTextures;
Implementation
-(id) init
{
if ((self = [super init]))
{
self.isTouchEnabled = true;
[SpriteTextures setTextures];
BalloonSprite = [Balloon spriteWithTexture: [SpriteTextures Balloon_RED]];
.......
NSAsserts are a real pain , but when on top of it the message is useless :) ... ok, looking at line 192 in cocos2d (CCSprite), the texture is nil (causing the assert failure). Check to see if you got messages similar to these for your textures when adding them to the sharedTextureCache.
2012-03-02 21:19:43.497 MyGame[55224:12203] cocos2d: CCTexture2D. Can't create Texture. UIImage is nil
2012-03-02 21:19:43.498 MyGame[55224:12203] cocos2d: Couldn't add image:a.png in CCTextureCache
If you did, the textures have not been found. Make certain they are in your resources folder, and also that they are members of the target you are building.
part 2 : just re-reading your code, you would get the same error if SpriteTextures was nil in the statement
[SpriteTextures setTextures];
BalloonSprite = [Balloon spriteWithTexture: [SpriteTextures Balloon_RED]];
That could explain why it works in one place but not the other.
part 3 : you are not initializing nor setting SpritTextures in your class init. Try:
self.isTouchEnabled = true;
self.SpriteTextures = [[Textures alloc] init];
[SpriteTextures setTextures];