1

我必须运行一个 19 秒的动画,每帧 466 帧是“1024x768”我正在使用 zwoptex 制作一个精灵表,但一张表是“4096 X 4096”,它只能容纳 15 帧。

我制作了多张纸,准确地说是 30 张,并用它们运行动画,但它有抖动,当我只使用 2 或 3 张纸时,它非常光滑。

这是我的代码

    cache=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache addSpriteFramesWithFile:@"Set_1.plist"];

    cache1=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache1 addSpriteFramesWithFile:@"Set_2.plist"];

    cache2=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache2 addSpriteFramesWithFile:@"Set_3.plist"];

    cache3=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache3 addSpriteFramesWithFile:@"Set_4.plist"];

    cache4=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache4 addSpriteFramesWithFile:@"Set_5.plist"];

    cache5=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache5 addSpriteFramesWithFile:@"Set_6.plist"];

    cache6=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache6 addSpriteFramesWithFile:@"Set_7.plist"];

    cache7=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache7 addSpriteFramesWithFile:@"Set_8.plist"];

    cache8=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache8 addSpriteFramesWithFile:@"Set_9.plist"];

    cache9=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache9 addSpriteFramesWithFile:@"Set_10.plist"];

    cache10=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache10 addSpriteFramesWithFile:@"Set_11.plist"];

    cache11=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache11 addSpriteFramesWithFile:@"Set_12.plist"];

    cache12=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache12 addSpriteFramesWithFile:@"Set_13.plist"];

    cache13=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache13 addSpriteFramesWithFile:@"Set_14.plist"];

    cache14=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache14 addSpriteFramesWithFile:@"Set_15.plist"];

    cache15=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache15 addSpriteFramesWithFile:@"Set_16.plist"];




    equ=[CCSprite spriteWithFile:@"m1.png"];
    [self addChild:equ z:5 tag:90 ];
    equ.anchorPoint=ccp(0, 0);
    equ.position=ccp(0, 0);

    NSMutableArray *framesArray=[NSMutableArray array];
     NSMutableArray *framesArray1=[NSMutableArray array];
    NSMutableArray *framesArray2=[NSMutableArray array];
    NSMutableArray *framesArray3=[NSMutableArray array];
    NSMutableArray *framesArray4=[NSMutableArray array];
    NSMutableArray *framesArray5=[NSMutableArray array];
    NSMutableArray *framesArray6=[NSMutableArray array];
    NSMutableArray *framesArray7=[NSMutableArray array];
    NSMutableArray *framesArray8=[NSMutableArray array];
    NSMutableArray *framesArray9=[NSMutableArray array];
    NSMutableArray *framesArray10=[NSMutableArray array];
    NSMutableArray *framesArray11=[NSMutableArray array];
    NSMutableArray *framesArray12=[NSMutableArray array];
    NSMutableArray *framesArray13=[NSMutableArray array];
    NSMutableArray *framesArray14=[NSMutableArray array];
   // NSMutableArray *framesArray15=[NSMutableArray array];

    for (int i=1; i<=15; i++) {
        NSString *frameName=[NSString stringWithFormat:@"m%d.png", i];
        NSString *frameName1=[NSString stringWithFormat:@"m%d.png", i+(15*1)];
        NSString *frameName2=[NSString stringWithFormat:@"m%d.png", i+(15*2)];
        NSString *frameName3=[NSString stringWithFormat:@"m%d.png", i+(15*3)];
        NSString *frameName4=[NSString stringWithFormat:@"m%d.png", i+(15*4)];
        NSString *frameName5=[NSString stringWithFormat:@"m%d.png", i+(15*5)];
        NSString *frameName6=[NSString stringWithFormat:@"m%d.png", i+(15*6)];
        NSString *frameName7=[NSString stringWithFormat:@"m%d.png", i+(15*7)];
        NSString *frameName8=[NSString stringWithFormat:@"m%d.png", i+(15*8)];
        NSString *frameName9=[NSString stringWithFormat:@"m%d.png", i+(15*9)];
        NSString *frameName10=[NSString stringWithFormat:@"m%d.png", i+(15*10)];
        NSString *frameName11=[NSString stringWithFormat:@"m%d.png", i+(15*11)];
        NSString *frameName12=[NSString stringWithFormat:@"m%d.png", i+(15*12)];
        NSString *frameName13=[NSString stringWithFormat:@"m%d.png", i+(15*13)];
        NSString *frameName14=[NSString stringWithFormat:@"m%d.png", i+(15*14)];


        id frameObject=[cache spriteFrameByName:frameName];
        id frameObject1=[cache1 spriteFrameByName:frameName1];
        id frameObject2=[cache2 spriteFrameByName:frameName2];
        id frameObject3=[cache3 spriteFrameByName:frameName3];
        id frameObject4=[cache4 spriteFrameByName:frameName4];
        id frameObject5=[cache5 spriteFrameByName:frameName5];
        id frameObject6=[cache6 spriteFrameByName:frameName6];
        id frameObject7=[cache7 spriteFrameByName:frameName7];
        id frameObject8=[cache8 spriteFrameByName:frameName8];
        id frameObject9=[cache9 spriteFrameByName:frameName9];
        id frameObject10=[cache10 spriteFrameByName:frameName10];
        id frameObject11=[cache11 spriteFrameByName:frameName11];
        id frameObject12=[cache12 spriteFrameByName:frameName12];
        id frameObject13=[cache13 spriteFrameByName:frameName13];
        id frameObject14=[cache14 spriteFrameByName:frameName14];



        [framesArray addObject:frameObject];
        [framesArray1 addObject:frameObject1];
        [framesArray2 addObject:frameObject2];
        [framesArray3 addObject:frameObject3];
        [framesArray4 addObject:frameObject4];
        [framesArray5 addObject:frameObject5];
        [framesArray6 addObject:frameObject6];
        [framesArray7 addObject:frameObject7];
        [framesArray8 addObject:frameObject8];
        [framesArray9 addObject:frameObject9];
        [framesArray10 addObject:frameObject10];
        [framesArray11 addObject:frameObject11];
        [framesArray12 addObject:frameObject12];
        [framesArray13 addObject:frameObject13];
        [framesArray14 addObject:frameObject14];


    }

    id animObject=[CCAnimation animationWithSpriteFrames:framesArray delay:0.0407];
    id animObject1=[CCAnimation animationWithSpriteFrames:framesArray1 delay:0.0407];
    id animObject2=[CCAnimation animationWithSpriteFrames:framesArray2 delay:0.0407];
    id animObject3=[CCAnimation animationWithSpriteFrames:framesArray3 delay:0.0407];
    id animObject4=[CCAnimation animationWithSpriteFrames:framesArray4 delay:0.0407];
    id animObject5=[CCAnimation animationWithSpriteFrames:framesArray5 delay:0.0407];
    id animObject6=[CCAnimation animationWithSpriteFrames:framesArray6 delay:0.0407];
    id animObject7=[CCAnimation animationWithSpriteFrames:framesArray7 delay:0.0407];
    id animObject8=[CCAnimation animationWithSpriteFrames:framesArray8 delay:0.0407];
    id animObject9=[CCAnimation animationWithSpriteFrames:framesArray9 delay:0.0407];
    id animObject10=[CCAnimation animationWithSpriteFrames:framesArray10 delay:0.0407];
    id animObject11=[CCAnimation animationWithSpriteFrames:framesArray11 delay:0.0407];
    id animObject12=[CCAnimation animationWithSpriteFrames:framesArray12 delay:0.0407];
    id animObject13=[CCAnimation animationWithSpriteFrames:framesArray13 delay:0.0407];
    id animObject14=[CCAnimation animationWithSpriteFrames:framesArray14 delay:0.0407];

    id animAction=[CCAnimate actionWithAnimation:animObject];
    id animAction1=[CCAnimate actionWithAnimation:animObject1];
    id animAction2=[CCAnimate actionWithAnimation:animObject2];
    id animAction3=[CCAnimate actionWithAnimation:animObject3];
    id animAction4=[CCAnimate actionWithAnimation:animObject4];
    id animAction5=[CCAnimate actionWithAnimation:animObject5];
    id animAction6=[CCAnimate actionWithAnimation:animObject6];
    id animAction7=[CCAnimate actionWithAnimation:animObject7];
    id animAction8=[CCAnimate actionWithAnimation:animObject8];
    id animAction9=[CCAnimate actionWithAnimation:animObject9];
    id animAction10=[CCAnimate actionWithAnimation:animObject10];
    id animAction11=[CCAnimate actionWithAnimation:animObject11];
    id animAction12=[CCAnimate actionWithAnimation:animObject12];
    id animAction13=[CCAnimate actionWithAnimation:animObject13];
    id animAction14=[CCAnimate actionWithAnimation:animObject14];

    //animAction=[CCRepeatForever actionWithAction:animAction];
    CCSequence *moveAction = [CCSequence actions:animAction,animAction1,animAction2,animAction3,animAction4,animAction5,animAction6,animAction7,animAction8,animAction9,animAction10,animAction11,animAction12,animAction13,animAction14,nil];
    moveAction.tag=100;
    [equ runAction:moveAction];
4

1 回答 1

4

每帧 466 帧的动画是“1024x768”

停在那儿!

问题是每个 4096x4096 纹理图集(spritesheets)都使用 64 MB 内存。在任何给定时间,只有 2-4 个(取决于设备内存)适合内存。

由于您按顺序缓存它们,因此您额外增加了加载时间,因为到它应该缓存纹理编号 3 或 4 时 cocos2d 将收到内存警告,并清除未使用的纹理 - 前几个纹理。

然后,当您开始动画时,它会在使用每个纹理图集时将其加载到内存中,因为此时唯一缓存的纹理可能是 8 到 10。同时其他未使用的纹理会被缓存,因为您不断收到内存警告.

你有两个选择:

  • 停止这种疯狂(™)
  • 也许最终可能通过使用 TexturePacker 并将纹理颜色深度降低到 16 位(RGB565 或 RGBA5551 - 将内存使用量减少一半)并将它们导出为 NPOT(轻微内存减少)和 .pvr.ccz(更快的加载和内存减少)可以改善情况。

有关更多信息,请查看我的cocos2d 内存优化指南

于 2013-01-20T09:26:23.117 回答