1

my problem is: I'm making a game for iOS using cocos2d and this game has lots of levels, so I'll have to create a loading scene to load my sprites for each level. ( like new backgrounds, monsters and other stuff ) But I have no idea about this, I'm adding all the Texture Packer Files (.plist and .pvr.ccz) on the sharedSpriteFrameCache in the GameData.m. Does anyone knows a good tutorial for this or can help me solve this? Thanks!

user1284366
  • 23
  • 1
  • 3

2 Answers2

1

So basically you want to know how to load and unload images as you see fit. How about

@implementation Level1

- (void) loadLevel
{
    CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
    CCTextureCache* textureCache = [CCTextureCache sharedTextureCache];

    // Add the sprite frames. This will load the texture as well
    [frameCache addSpriteFramesWithFile:@"monkey.plist"];
    [frameCache addSpriteFramesWithFile:@"player.plist"];
    [frameCache addSpriteFramesWithFile:@"enemy.plist"];

    // Load other textures that are going to be used
    _myBackgroundTexture = [textureCache addImage:@"background.png"];
}

- (void) unloadLevel
{
    CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
    CCTextureCache* textureCache = [CCTextureCache sharedTextureCache];

    // Remove textures
    [textureCache removeTexture:_myBackgroundTexture];

    // Remove sprite frames. This will load the texture as well
    [frameCache removeSpriteFramesFromFile:@"monkey.plist"];
    [frameCache removeSpriteFramesFromFile:@"player.plist"];
    [frameCache removeSpriteFramesFromFile:@"enemy.plist"];

    // Though normally, id use frameCache removeUnusedSpriteFrames and
    // textureCache removeUnusedTextures
}

... @end

Andres C
  • 919
  • 7
  • 14
-1

You can use the sprite by CCMenuItem and also by the Menu as you would require to click and move to that particular level.. The below is the code for adding the menu item image

CCMenuItem *m4 =[CCMenuItemImage itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>] 
                                           selectedSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>] 
                                           disabledSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
                                                   target:self selector:@selector(MoveLeft)];

The above code gives the information you can use to display the sprite for the particular state of the menu. And the "MoveLeft" is the method selector which I have used to call the particular method.

At last you can add the m4 object to the CCMenu and get the desired output...

Hope it works for you.

Marine
  • 1,097
  • 7
  • 19
  • I'm sorry for my english, but my problem is that my game has lots os spritesheets and I want to remove or add the spritesheets and free the memory. Example: In stage 1 of my game, it has a background Image stage1BG.png in the spritesheet called stage1.pvr.ccz . How can I remove the stage1.pvr.ccz from the sharedSpriteFrameCache and load another one, freeing the memory of the device. Again, sorry for my english (: – user1284366 Mar 22 '12 at 18:05
  • You can use the code for making unused spritesheet to free the memory. [CCSpriteFrameCache purgeSharedSpriteFrameCache]; Also it would show the number of spritesheet made free from the Cache. The List would be displayed on the console – Marine Mar 23 '12 at 05:02