Search code examples
objective-ccocos2d-iphone

Invalid texture for sprite


I'm trying to load a texture, which is contained in a NSMutableArray, but I'm getting this error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid texture for sprite'

This is how I'm trying to refer to my texture in the mutable array:

background = [CCSprite spriteWithTexture:[loadedTextures objectAtIndex:0]];

This is how I'm generating my textures:

  - (void)loadAssets {

        NSArray *imageNames = @[
                                @"bg-001.png",
                                @"bg-002.png"
                                ];

        for (NSString *imageName in imageNames)
        {
            CCTexture * tex = [CCTexture textureWithFile:imageName];
            [loadedTextures addObject:tex];
        }
    }

Solution

  • The message indicates that the texture is nil. Which is weird because you can't store a nil value in an array. That leaves as only possible explanation that loadedTextures itself is nil.

    Check that this (or similar) line initializes the array:

    loadedTexture = [NSMutableArray array];
    

    This has to be before the for loop that fills the loadedTextures array.