Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7068329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:18:38+00:00 2026-05-28T05:18:38+00:00

I am learning Cocos2d and building an application where i have sprite at one

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:18:39+00:00Added an answer on May 28, 2026 at 5:18 am

    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];
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When learning new languages such as C++ from PHP, does reading other language snippets
I'm just learning Cocos2d and Objective-C, and I have some problems with touch detection.
learning Jquery and integrating with PHP - getting there, but have one last challenge
I am in the process of learning cocos2d-android. I have been following a tutorial
Learning django from learning website development using django. In chapter 3, building a bookmark
Learning C# here. I have a monthly process form whereby one can select to
Learning Javascript and have a question about global variables. From my reading, most recommend
Learning from my last question , most member names seem to get included in
For learning and demonstrating, I need a macro which prints its parameter and evaluates
Learning jquery, so please be kind :) Using PHP, I have a table with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.