开发者

count how much images in a sprite sheet-cocos2d

when i animate a sprite sheet in a loop , i dont know how many sprites are there, so i have to count them before, because when animation is done , i have to run something else.

so how can i count the number of images befor starting loop? or there is another way to do so ? (this function can get various of sprite sheets. i prefer now to not usi开发者_运维问答ng a Plist.

thanks a lot .

-(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); 

}


This has to do with the program you make your spritesheets with. Cocos2D does not know that there is more than one image unless you tell it there is.

Edit:

Cocos2D only knows there is an image if you tell it there is an image. Cocos2D only knows an image is a spritesheet if you tell it the image is a spritesheet. That is what the PLIST files are for. The PLIST tells Cocos2D the location and size of every sprite in the spritesheet.

The short answer to your question is no. The easiest thing to do is to just count the sprites in your spritesheet. It isn't exactly hard to do so. If you really need to know how many there are from code then you will need to code up a method or two to count them based on all the parameters of the image. It is fairly basic math.

(widthOfSpritesheet/widthOfIndividualSprite)*(heightOfSpritesheet/heightOfIndividualSprite)

That is the basic math but you also have to consider that you may not have a completely full spritesheet so you will need a way to know if the last row is full or not.

Really that sounds like way more work than it is worth. You should either just count your sprites in the spritesheet or make a request to the developer of your spritesheet software to introduce a new feature providing a full count of sprites in your spritesheet. I use TexturePacker and it does not provide this feature that I know of, but I have never needed it either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜