Domanda

After I completed my testing, I have moved my images over to a spritesheet.

I loaded up the batchnode with the appropriate files and my images load just fine.

But I am running into an issue of swapping the textures out. When the images were individual files, there was no problem. Now it seems the CCTexture2d doesn’t like my sprite sheet.

I have the objects stored in a multidimensional array, so I can run through them quickly and update their image.


Here is what I did when it worked:

CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:@"alt-image.png"];
[((MyFunObject*)[[myFunObject2DArr objectAtIndex:j]objectAtIndex:i])->img setTexture: tex];

Here is what I am doing now:

CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:[CCSprite spriteWithSpriteFrameName:@"alt-image.png"]];
[((MyFunObject*)[[myFunObject2DArr objectAtIndex:j]objectAtIndex:i])->img setTexture: tex];

MyFunObject is a subclass of CCSprite and has an CCSprite img property that get set. I run through the array and find like objects and replace their image with a new image “alt-image.png”.

Seems simple, but outside of a sprite sheet this worked flawlessly.

Can someone tell me what I am doing wrong?


È stato utile?

Soluzione

UPDATE: HERE IS THE SOLUTION FOR ANYONE IN THE FUTURE WHO HAS THIS ISSUE

You can't change the sprites 'texture' when the texture in question is spritesheet. (Slapped my head on that one) You can only change the rectangle that is viewed.

Here is how you do it:

[((MyFunObject*)[[myFunObject2DArr objectAtIndex:j]objectAtIndex:i])->img setTextureRect:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"alt-image.png" ].rect];

Notice I changed setTexture to setTextureRect, then I called the SpriteFrame's rectangle as an argument.

Enjoy.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top