简体   繁体   English

CCSpriteFrameCache 还是 CCSprite? - Cocos2d

[英]CCSpriteFrameCache or CCSprite? - Cocos2d

I am making my first game and so far my only problem is memory usage (I haven't payed the developer fee so I am running it in the simulator).我正在制作我的第一个游戏,到目前为止我唯一的问题是 memory 的使用(我没有支付开发人员费用,所以我在模拟器中运行它)。 So far it is taking up about 23MB, from what I've read that is way to much.到目前为止,它占用了大约 23MB,从我读过的内容来看,这已经很多了。 Is this true?这是真的? First, in my main menu my biggest problem is my background, it is taking up 2MB+ of memory.首先,在我的主菜单中,我最大的问题是我的背景,它占用了 2MB+ 的 memory。 Is that ok?那样行吗? Second, the background in my gamescene is also taking up 2MB+.其次,我的游戏场景中的背景也占用了 2MB+。 Is that ok?那样行吗? Third, I have 2 CCSpriteFrameCaches's, one has 55.PNG files and the other has 12. The one with the 55 images takes up about 9MB of memory.第三,我有 2 个 CCSpriteFrameCache,一个有 55.PNG 文件,另一个有 12 个。有 55 个图像的那个占用了大约 9MB 的 memory。 Does that sound normal?这听起来正常吗? The second one takes up about 3MB.第二个占用大约 3MB。 Does that sound normal?这听起来正常吗?

The 55 images aren't added to the layer until a button is pressed (which then takes up even more memory about 7MB, not sure why).在按下按钮之前,55 张图像不会添加到图层中(然后占用更多的 memory 大约 7MB,不知道为什么)。 Then when the second button is pressed the first image is removed and the second image is added.然后当第二个按钮被按下时,第一张图像被删除,第二张图像被添加。 Then, when the first button is pressed again the second image is removed and the third image is added (so on and so fourth).然后,当再次按下第一个按钮时,第二个图像被删除并添加第三个图像(依此类推,第四个)。 My question is would it be better to use a CCSpriteFrameCache or CCSprite?我的问题是使用 CCSpriteFrameCache 或 CCSprite 会更好吗? Or is there some kind of alternative.或者是否有某种替代方案。

If anyone has a better way of doing this please let me know.如果有人有更好的方法,请告诉我。 ANY help is greatly appreciated!任何帮助是极大的赞赏!

Sample Code:示例代码:

-(void)buttons:(CGPoint)touchLocation {
if (CGRectContainsPoint(leftB.boundingBox, touchLocation) && tapP == YES && paused == NO) {
    if (count == 1)
    {
        isBOP = YES;
        count = 2;
    } else if (count == 3)
    {
        [bspriteSheet removeChild:_b cleanup:YES];
        [pspriteSheet removeChild:_p cleanup:YES];

        _p = [CCSprite spriteWithSpriteFrameName:@"p3.png"];
        [pspriteSheet addChild:_p];
        count = 4;
        _p.position = ccp(240, 215);

        _b = [CCSprite spriteWithSpriteFrameName:@"b2.png"];
        _b.position = ccp(240, 158);
        [bspriteSheet addChild:_b];
    }
if (CGRectContainsPoint(rightB.boundingBox, touchLocation) && tapP == YES && paused == NO) {
    if (count == 2)
    {
        [self removeChild:gb cleanup:YES];
        [pspriteSheet removeChild:_p cleanup:YES];

        _p = [CCSprite spriteWithSpriteFrameName:@"p2.png"];
        _p.position = ccp(240, 215);
        [pspriteSheet addChild:_p];

        _b = [CCSprite spriteWithSpriteFrameName:@"b1.png"];
        _b.position = ccp(240, 158);
        [bspriteSheet addChild:_b];

        count = 3;
    } else if (count == 4)
    {
        [bspriteSheet removeChild:_b cleanup:YES];
        [pspriteSheet removeChild:_p cleanup:YES];

        _p = [CCSprite spriteWithSpriteFrameName:@"p4.png"];
        [pspriteSheet addChild:_p];
        count = 5;
        _p.position = ccp(240, 215);

        _b = [CCSprite spriteWithSpriteFrameName:@"b3.png"];
        _b.position = ccp(240, 158);
        [bspriteSheet addChild:_b];
    }

Check out the best practices document... they have a lot of details about the issue that you have.查看最佳实践文档......他们有很多关于您遇到的问题的详细信息。

http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:best_practiceshttp://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:best_practices

Consider using other image formats for your large images.考虑为大图像使用其他图像格式。 Using a PVRTC file would greatly cut down on your memory usage.使用 PVRTC 文件将大大减少 memory 的使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM