Search code examples
iphoneobjective-ccocos2d-iphone

How to change sprite image with array of images cocos2d?


I am learning Cocos2d and building an application where i have sprite at one end and I need to through it on the other side and with the same I am removing the sprite from the screen and after some time, I am displaying the same.

Now I have a folder of images in my application and I need to load different images each time in random order from the same folder and maintain a log that these images do not repeat again and again. I am able to load images from the folder with:

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];
NSArray *onlyJPGs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"]];

Now how would I call this array and display different images each time and also maintain a log that images don't get repeat. I have already went through links like this and this but to no avail.


Solution

  • The best way to do this is by creating a spriteSheet. first of all you can get http://zwoptexapp.com/ , its free and you can create your spritesheet for using with cocos (on the exporter make sure you select cocos2d to create the proper plist)

    You want to pack all your image in 1 big texture so you can add it to your project with the plist (zwoptex will create both for you)

    then you can load your texture with

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"your_plist"];
    

    switching textures is slow operation, so having all the images in the same texture will boost the openGL performance, after you've done that changing the texture for a sprite is very easy

    [yourSprite setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"FRAME_NAME"]];
    

    where FRAME_NAME is the name of the frame in the plist (you can see it by selecting the plist inside xcode.

    to cycle in a random way without repeating images... (i'll write some pseudocode directly in here, let me do inits inside class declaration and inline implementations :) )

    //WARNING THIS IS PSEUDO CODE :)
    
        @interface Randomizer {
            //an array of NSStrings containing all you images names    
            NSMutableArray *allImagesFrameNames = [NSMutableArray arrayWithCapacity:NUM_FRAMES];
    CCSprite *sprite = alloc init
        }
    
    -(void) resetAllFrames {
    [allImagesFrameNames removeAllobjects];
    
    [allImagesFrameName addObject:@"FIRST_IMAGE"];
    [allImagesFrameName addObject:@"SECOND_IMAGE"]; //add all your images
    }
    

    @end

    And to display a random frame:

    -(void) display a randomImage {
    //if the array is empty, all images are already been randomly displayed, so we reset the array
    if([allImagesFrameName count] == 0)
    [self resetAllFrames];
    
    //we choose a random index
    int randomIndex = arc4random %[allImagesFrameName count];
    //we get the frame name at that index
    NSString *imageFrameName = [allImagesFrameNames objectAtIndex:randomIndex];
    
    //and we display the frame
    [sprite setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:imageFrameName]];
    
    [allImagesFrameNames removeObjectAtIndex:randomIndex];
    
    }