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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:16:50+00:00 2026-05-23T12:16:50+00:00

I have a ball sprite that changes it’s texture when a certain scenario occurs.

  • 0

I have a “ball” sprite that changes it’s texture when a certain scenario occurs. I change it’s texture like this:

[ball setTexture:[[CCTextureCache sharedTextureCache] addImage:@"red.png"]];

This does work; it changes the ball sprite to use the red.png image. How would I handle this if I have about 20 balls that need to switch to using this sprite? Would I run through each ball and “addImage”?

If I could, I would like to load the texture once, save it in a variable (called like “redTexture”), and then be able to assign it to any of the ball objects.

Any advice on how to approach this would be a huge help, thank you!

  • 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-23T12:16:51+00:00Added an answer on May 23, 2026 at 12:16 pm

    If you were to put that statement inside, say, a for loop, you’d be adding the red.png image to the shared texture cache over and over again, which I doubt is what you want.

    Let’s back up and re-write things a bit, starting by adding the red.png image to the shared texture cache, on a line by itself:

    [[CCTextureCache sharedTextureCache] addImage:@"red.png"];
    

    You’d subsequently get the same texture again simply by calling [CCTextureCache sharedTextureCache]. Until you add another image to the shared texture cache, that is.

    CCTextureCache is a singleton, and its docs don’t suggest there’s a way to make a copy of the shared texture cache (which would be ideal for preserving your redTexture). That being the case, just create a variable and point it at [CCTextureCache sharedTextureCache]; just be careful not to add any other images to it before you’re done with it:

    CCTextureCache *redTexture = [CCTextureCache sharedTextureCache];
    

    Now let’s assume you already have an array (or mutable array) called ballArray, that contains 20 ball objects. You could loop through them like this:

    for (YourBallObject *ball in ballArray)
    {
        [ball setTexture:redTexture];
    }
    

    Or even better, you could do this:

    [ballArray makeObjectsPerformSelector:@selector(setTexture:) withObject:redTexture];
    

    Good luck in your endeavors.

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

Sidebar

Related Questions

I have an object tree that looks something like Ball / \ LegalBall IllegalBall
I have this code: package graphics { import flash.display.Sprite; import flash.events.*; public class Ball
I have a ball that triggers an action when it collides with a sprite.
I have a sprite(image of a ball) I can move it using touch and
I'm having a problem with a class like this: class Sprite { ... bool
I have this shaded circle: \shade [ball color=black!80!white] (0,0) circle (1); Is it possible
I'm writing a pong game and I have a ball class that has velocity
I have an issue for this line ball* balle = [[ball alloc] initWithPNGFileName:ballPath andGame:game
I have code that will paint a ball when I click the mouse in
Have you managed to get Aptana Studio debugging to work? I tried following this,

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.