New to Cocos2D: Show same sprite multiple times?

Forums Programming cocos2d support (graphics engine) New to Cocos2D: Show same sprite multiple times?

This topic contains 7 replies, has 6 voices, and was last updated by  ipodfreak0313 2 years, 10 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
Author Posts
Author Posts
May 9, 2010 at 8:07 pm #221628

Krumelur
@krumelur

Hi,

what is the best practice if I have a sprite and want to show it multiple times on the screen?

Each instance should be an individual sprite but they all share the same texture(s).

I think I should use CCSpriteSheed but I can’t seem to get a starting point.

I’ve alredy gone through the forum searching for “reuse sprite”, “same sprite multiple times”, etc. but cannot seem to find an answer, so there must be acompletely different approach I think.

To make it even more complex: the sprites created from this one texture should not only appear multiples times on the screen but be scaled differently too. If I understand correctly, this is not possible with CCSpriteSheet?

Thanks.

René

May 9, 2010 at 11:26 pm #282486

Codemattic
Moderator
@codemattic

Use multiple sprites that use the same texture. Cocos will only load the texture once even though its used many times. For instance when you make a CCSprite with -spriteWithFile:@”someImage.png” it will load “someImage.png” into memory. The next time you create a new CCSprite and call -spriteWithFile:@”someImage.png” it wont reload that image, instead it will reference the texture which already exists in memory.

May 9, 2010 at 11:32 pm #282487

ddeaco
Participant
@ddeaco

While were on the subject,

codemattic if you add the same sprite multiple times, do all of these identical sprites get draw like if you were using an AtlasSprite, in one batch?

May 10, 2010 at 9:04 am #282488

Krumelur
@krumelur

Thanks. That’s a good optimization.

What I’m still wondering: if I use spriteWithFile and my texture is under “resources/gfx/texture.png” I will still use “texture.png” and not “gfx/texture.png”.

It seems the groups I create in xcode are purely virtual. Is there a way to have a real folder structure like thing so I can have multiple files named “texture.png” without naming conflicts?

René

May 10, 2010 at 2:32 pm #282489

Verisutha
Participant
@verisutha

You really should name your texture what it is. Have 50 texture.png files would be quite confusing if anyone other than you had to work on the code.

May 10, 2010 at 2:39 pm #282490

Blue Ether
Participant
@blue-ether

Krumelur: Groups are virtual. Folder references are not. When you add a folder to your project, it depends on whether you select “recursively create groups” or “create folder references”. You can tell a group from a folder reference in the tree view because folder references are blue. This means everything in groups has to have a unique file name. You can’t have two of the same file name if they are in groups. However, in a folder reference you can, and you have to use the folder path to access it.

May 10, 2010 at 9:54 pm #282491

Codemattic
Moderator
@codemattic

@ddeaco no, I dont think so

June 13, 2011 at 12:49 am #282492

ipodfreak0313
@ipodfreak0313

@Codemattic @Blue Ether @Krumelur okay, if i wanted to have an app where the user would drag their finger across the screen, and a dot appeared every 10 pixels the user’s finger traveled, how would i accomplish that?? I know you cant just keep repeating:[self addChild:dot]; every 10 pixels, because that would take FOREVER to code. Please help me out, guys! Thanks!

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

You must be logged in to reply to this topic.