1
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – PlayDeezGames Aug 10 '12 at 17:28
  • \$\begingroup\$ @PlayDeezGames where can I read on separating game object and rendering? \$\endgroup\$ – Dvole Aug 10 '12 at 17:32
  • \$\begingroup\$ @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. \$\endgroup\$ – bummzack Aug 10 '12 at 19:05
1
\$\begingroup\$
CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:@"new_image_name"];
[spriteWant2Change setTexture: tex];
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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