Change Sprite Frame

Forums Programming cocos2d support (graphics engine) Change Sprite Frame

This topic contains 9 replies, has 3 voices, and was last updated by  Eko Mirhard 3 years, 5 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
Author Posts
Author Posts
October 18, 2010 at 1:59 pm #225563

napierzaza
Participant
@napierzaza

Is there a method to change an existing SpriteNode’s Texture?

For example I create the sprite like this:

Projectile *projectile = [Projectile spriteWithSpriteFrameName:string];

But once it’s created I want to recycle the sprite, so I need to be able to change it’s frame (texture). I used to use rect’s that referenced the sprite sheet. But since I’m not using plist’s I can not longer [sprite setTextureRect:rect];

Is there a way to do this in the current version of cocos2d?

October 18, 2010 at 4:43 pm #301719

Eko Mirhard
Participant
@whateva

EDIT : Oopss.. looks like i misunderstood your problem. I thought you were creating a simple CCSprite using spriteWithFile and want to change the texture without creating another instance… =.= For your problem, i guess you can use the setDisplayFrame method to change the current displayed frame…

In the past, i used something like this :

-(void) setFile:(NSString*)filename
{
NSAssert(filename!=nil, @"Invalid filename for sprite");

CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage: filename];
if( texture ) {
CGRect rect = CGRectZero;
rect.size = texture.contentSize;
[self setTexture:texture];
[self setTextureRect:rect];
}
}

Basically, i added a new method in a category for CCSprite, in which can be used to update the texture by specifying the new filename :)

Btw, use CCSpriteSheet if you are going to change the texture of a sprite frequently as it will boost the performance…

Regards,

Eko Mirhard

October 18, 2010 at 8:46 pm #301720

napierzaza
Participant
@napierzaza

My mistake, I forgot to mention I was using a SpriteNode but maybe only alluded to it when I mentioned using a plist. Essentially I have the texture preloaded and should not have to add anything else to the sharedTextureCache. I just need to make the sprite change from one rect to another on the same sheet.

So previously with rects I might be at rect 0, 0, 64, 64 and then change the offset to 64, 64, 64, 64 without having to re-init the sprite. This was somewhat of a memory optimization in previous versions because removing/adding sprites was costly. I assume it’s still the case but who knows.

October 19, 2010 at 1:30 pm #301721

napierzaza
Participant
@napierzaza

So does this method not exist in the current version of cocos?

October 19, 2010 at 1:35 pm #301722

goki
@goki

I believe setTextureRect is what you’re looking for

October 19, 2010 at 2:45 pm #301723

napierzaza
Participant
@napierzaza

Noooo, because it’s not rect based anymore. It’s frame (NSString of framename) based now. I don’t want to have to do a work around and go back to rect-based references now since it doesn’t make sense. I have to suss out what the rect of the Frame is and setTextureRect?

October 19, 2010 at 3:10 pm #301724

goki
@goki

yes, take rect of frame and set it via setTextureRect

October 19, 2010 at 3:23 pm #301725

Eko Mirhard
Participant
@whateva

how about setDisplayFrame ? You can get the CCSpriteFrame using the frame name method (spriteFrameByName), and send it as a parameter to setDisplayFrame method of the sprite… You are using CCSpriteFrameCache to store all the frames needed right? Then you can use this approach…

October 19, 2010 at 7:59 pm #301726

napierzaza
Participant
@napierzaza

This is how it’s created.

+(id)spriteWithSpriteFrameName:(NSString*)spriteFrameName

{

CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:spriteFrameName];

return [self spriteWithSpriteFrame:frame];

}

Why not have something like

-(void) setDisplayWithFrameName:(NSString*)spriteFrameName

{

CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:spriteFrameName];

[self setDisplayFrame:frame];

}

October 20, 2010 at 3:36 pm #301727

Eko Mirhard
Participant
@whateva

well, than you can just add this method (setDisplayWithFrameName) in a category, or in a sublass of CCSprite :)

the point is that you will have to make use of the setDIsplayFrame method ;)

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.