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 8822295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:04:16+00:00 2026-06-14T06:04:16+00:00

Is there a clean and efficient way to create an arbitrary number of identical

  • 0

Is there a clean and efficient way to create an arbitrary number of identical CCSprites?

I really just need a tag to reference them for later removal.

For example in my game I am displaying the number of lives in a HUD:

- (void)displayOneLife
{
    CGPoint positionOne = ccp(90, 450);

    CCSprite *life1 = [CCSprite spriteWithFile:@"life.png"];
    [life1 setPosition:positionOne];
    [life1 setScale:0.5f];
    [self addChild:life1 z:5 tag:1];
}

- (void)displayTwoLives
{
    CGPoint positionOne = ccp(90, 450);
    CGPoint positionTwo = ccp(105, 450);

    CCSprite *life1 = [CCSprite spriteWithFile:@"life.png"];
    CCSprite *life2 = [CCSprite spriteWithFile:@"life.png"];

    [life1 setScale:0.5f];
    [life2 setScale:0.5f];

    [life1 setPosition:positionOne];
    [life2 setPosition:positionTwo];

    [self addChild:life1 z:5 tag:1];
    [self addChild:life2 z:5 tag:2];
}

- (void)displayThreeLives
{
    CGPoint positionOne = ccp(90, 450);
    CGPoint positionTwo = ccp(105, 450);
    CGPoint positionThree = ccp(120, 450);

    CCSprite *life1 = [CCSprite spriteWithFile:@"life.png"];
    CCSprite *life2 = [CCSprite spriteWithFile:@"life.png"];
    CCSprite *life3 = [CCSprite spriteWithFile:@"life.png"];

    [life1 setPosition:positionOne];
    [life2 setPosition:positionTwo];
    [life3 setPosition:positionThree];

    [life1 setScale:0.5f];
    [life2 setScale:0.5f];
    [life3 setScale:0.5f];

    [self addChild:life1 z:5 tag:1];
    [self addChild:life2 z:5 tag:2];
    [self addChild:life3 z:5 tag:3];
}
  • 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-06-14T06:04:18+00:00Added an answer on June 14, 2026 at 6:04 am

    Create a CCTexture2D using your image and then init all of the sprites using that texture.
    This way you only load the image once.

    Hope this helps.

    EDIT:

    Also , you can add them dynamically. Like this:

    - (void)displayLifes:(int) nrOfLifes
    {
        CGPoint position = ccp(90, 450);
        CCTexture2D *texture = [[[CCTexture2D alloc] initWithImage:[UIImage imageNamed:@"life.png"]]autorelease];
    
        for(int i = 1 ; i <= nrOfLifes ; i++)
        {
             CCSprite *life = [CCSprite spriteWithTexture:texture];
             [life setPosition:position];
             [life setScale:0.5f];
             [self addChild:life z:5 tag:i];
    
             position.x += 15;
        }
    }
    

    Cheers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there some clean and elegant way to execute my code right after a
Is there a (clean) way to manipulate some text from std::cin before inserting it
Is there an easy/clean way to do this in Linux/ a Linux-like environment? Purpose
I know there is a clean way to map a function f:A => B
I'm sure there's a clean way to do this, but I'm probably not using
I would like to know if there is a clean way to do git-svn
I'm developing my first EclipseLink J2SE project, and wondering if there's an efficient way
Is this the most efficient and clean way to check the sessions for a
I just want to ask if: The code below is efficient? Is there a
Is there a clean Java method for merging the points of two given overlapping

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.