【问题标题】:count how much images in a sprite sheet-cocos2d计算 sprite sheet-cocos2d 中有多少图像
【发布时间】:2011-07-21 23:19:13
【问题描述】:

当我在循环中为精灵表设置动画时,我不知道有多少精灵,所以我必须先计算它们,因为当动画完成后,我必须运行其他东西。

那么我如何计算开始循环之前的图像数量? 还是有另一种方法? (这个函数可以获得各种精灵表。 我现在更喜欢不使用 Plist

非常感谢。

-(void)animation
{   

    WeInAction=1;   




    //animation
    CCSpriteBatchNode *spriteSheet = [ CCSpriteBatchNode batchNodeWithFile:animation];  
    [self addChild:spriteSheet];

    CCSprite *dollSprite = [CCSprite spriteWithTexture:spriteSheet.texture rect:CGRectMake(0, 0, 320, 350)];
    [spriteSheet addChild:dollSprite];
    spriteSheet.anchorPoint=CGPointMake(0, 30);

    CGSize s = [[CCDirector sharedDirector] winSize];
    dollSprite.position = ccp(s.width/2,s.height/2);

    CCAnimation *dollAnimation = [CCAnimation animation];
    [dollAnimation setDelay:0.1f];

    int frameCount = 0;
    for (int y = 0; y < 5; y++)      //cocos add all this frames to memery and then he starts down the action, 

    {

        for (int x = 0; x < 6; x++) 
        {

            CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:spriteSheet.texture rect:CGRectMake(x*320,y*350,320,350)];
            [dollAnimation addFrame:frame];
            frameCount++;

            if (frameCount == 25)
            break;

        }
    }




    CCAnimate *Action = [CCAnimate actionWithAnimation:dollAnimation];
    id call=[CCCallFunc actionWithTarget:self selector:@selector(finishAnimation)];
    id sequence=[CCSequence actions:Action,[CCHide action],call,nil];
    [dollSprite runAction:sequence];
    NSLog(@"%@",basic_pic); 

}

【问题讨论】:

    标签: objective-c cocos2d-iphone


    【解决方案1】:

    这与您制作精灵表的程序有关。除非你告诉它存在,否则 Cocos2D 不知道存在多个图像。

    编辑:

    Cocos2D 只有告诉它有图像才知道有图像。 Cocos2D 只有在告诉它图像是 spritesheet 时才知道图像是 spritesheet。这就是 PLIST 文件的用途。 PLIST 告诉 Cocos2D 每个 sprite 在 spritesheet 中的位置和大小。

    您的问题的简短回答是否定的。最简单的方法是只计算精灵表中的精灵。这样做并不难。如果你真的需要从代码中知道有多少,那么你需要编写一两个方法来根据图像的所有参数计算它们。这是相当基本的数学。

    (widthOfSpritesheet/widthOfIndividualSprite)*(heightOfSpritesheet/heightOfIndividualSprite)
    

    这是基本的数学运算,但您还必须考虑到您可能没有完全完整的 spritesheet,因此您需要一种方法来了解最后一行是否已满。

    真的,这听起来比它的价值要多得多。您应该只计算 spritesheet 中的 sprite,或者向 spritesheet 软件的开发人员提出请求,以引入一项新功能,以在 spritesheet 中提供完整的 sprite 计数。我使用 TexturePacker,它没有提供我知道的这个功能,但我也从来不需要它。

    【讨论】:

    • 我听不懂你的意思。你可以解释吗 ?我正在用 x 和 y 运行一个循环,我以前不知道它们的值。有办法知道吗?我需要运行一个适合它获得的任何精灵表的通用循环..
    • Cocos2D 不检查图像。 Cocos2D 只知道 spritesheet 中有多个图像,如果你告诉它有的话。事实上,除非你告诉它,否则它不知道普通图像和 spritesheet 之间的区别。如果所有图像的尺寸始终相同,那么您可以自己计算,或者您可以创建一种方法来根据您插入的值进行计算。
    • 我找不到如何知道的方法...你能给我一个大致的方向吗? cocos 是“盲目的”,它运行在我给它的循环号上......
    • 我已经修改了我的答案。我真的不能给你更多的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2013-11-19
    • 2011-05-06
    • 2014-02-02
    • 2019-01-05
    • 2012-09-21
    • 2014-01-16
    相关资源
    最近更新 更多