Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

How do I correctly change sprite to show another image? Or to have another texture? This is the way I do it now. When I need to change sprite image I increase state variable. So i have one picture for 0, another for 1 and another for 2. First i tried just saying mysprite = [CCSpriteWithFile:@"sprite.png"] but that didn't end up too well since I was making new objects every frame and it got messy pretty fast. So I added a check. There is another variable that is stateChanged and it starts at zero. So I compare state to stateChanged before actually making any sprites and if it differs I do the magic. But here comes the problem. Somehow this sprite won't flip. And I believe the problem is in this method.

- (void) updateState
    {
        if (stateChanged != state) {

        if (state == 0)
            {
                currentSprite = [CCSprite spriteWithFile:@"eggclosed.png"];
                [self addChild:currentSprite];
            }

        if (state == 1)

        {
            currentSprite = nil;
            [self removeAllChildrenWithCleanup:YES];
            currentSprite = [CCSprite spriteWithFile:@"eggopen.png"];
            [self addChild:currentSprite];
        }

        if (state == 2)
        {
            currentSprite = nil;
            [self removeAllChildrenWithCleanup:YES];
            currentSprite = [CCSprite spriteWithFile:@"yoshi.png"];
            [self addChild:currentSprite];
        }

            stateChanged = state;
        }
            }

TLDR: How to correctly change image of sprite. I have a Pet object that has a CCSprite property. And I sometimes need to change sprte to look another way. How do?

share|improve this question
    
First, I don't think this code illustrates the problem you are having. We'd need likely a lot more code on how you are doing things in order to make a specific recommendation for this problem. However, one of the things I'm spotting here is that you are not separating code between the representation of the game object and the rendering logic itself. Also, you are not using a sprite cache, which would probably help with the problem of loading sprites all the time and loading them multiple times. Of course, that is a long tale, and will be told another time. –  PlayDeezGames Aug 10 '12 at 17:28
    
@PlayDeezGames where can I read on separating game object and rendering? –  Dvole Aug 10 '12 at 17:32
    
@PlayDeezGames Just FYI: Loading sprites like above (using spriteWithFile will cache the file. All subsequent loads (of an already loaded filename) will hit the cache instead of loading from the filesystem. –  bummzack Aug 10 '12 at 19:05
add comment

1 Answer

up vote 1 down vote accepted
CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:@"new_image_name"];
[spriteWant2Change setTexture: tex];
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.