如何正确加载 cocos2d 中的精灵? [英] How to load sprites in cocos2d correctly?

查看:15
本文介绍了如何正确加载 cocos2d 中的精灵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:我正在使用 cocos2d 为 iOS 制作一个游戏,这个游戏有很多关卡,所以我必须创建一个加载场景来为每个关卡加载我的精灵.(比如新背景、怪物和其他东西)但我对此一无所知,我正在添加所有纹理打包程序文件(.plist 和 .pvr.ccz)在 GameData.m 中的 sharedSpriteFrameCache 上.有谁知道这方面的好教程或可以帮助我解决这个问题?谢谢!

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!

推荐答案

所以基本上你想知道如何加载和卸载你认为合适的图像.怎么样

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
}

...@结束

这篇关于如何正确加载 cocos2d 中的精灵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆