精灵动画表

发布于 2025-01-05 05:37:31 字数 870 浏览 0 评论 0原文

我正在使用 cocos2d 和 box2d phisycs 开发一个应用程序。我想让我的精灵在移动时有动画效果。我在 Zwoptex 中制作了 *.plist 和 *.png 文件,并将它们添加到我的项目中。现在,我正在尝试创建一个精灵:

        [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"SquirrelAnimation.plist"];

        node = [CCSpriteBatchNode batchNodeWithFile:@"SquirrelAnimation.png" capacity:100];
        spriteTexture = [node texture];

        b2BodyDef bodyDef;
        bodyDef.type = bodyType;
        CGSize size = [CCDirector sharedDirector].winSize;
        CGPoint point = ccp(size.width / 2, size.height / 2);
        bodyDef.position.Set(point.x / PTM_RATIO, point.y / PTM_RATIO);
        body = world->CreateBody(&bodyDef);

        sprite = [PhysicsSprite spriteWithTexture:spriteTexture];
        [sprite setPhysicsBody:body];
        [node addChild:sprite];

但是此代码创建一个精灵,其中所有帧都为节点。我做错了什么?

I'm developing an app using cocos2d and box2d phisycs. I whant to make my sprite animate on moving. I made a *.plist and *.png files in Zwoptex, and added them to my project. Now, I'm trying to create a sprite:

        [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"SquirrelAnimation.plist"];

        node = [CCSpriteBatchNode batchNodeWithFile:@"SquirrelAnimation.png" capacity:100];
        spriteTexture = [node texture];

        b2BodyDef bodyDef;
        bodyDef.type = bodyType;
        CGSize size = [CCDirector sharedDirector].winSize;
        CGPoint point = ccp(size.width / 2, size.height / 2);
        bodyDef.position.Set(point.x / PTM_RATIO, point.y / PTM_RATIO);
        body = world->CreateBody(&bodyDef);

        sprite = [PhysicsSprite spriteWithTexture:spriteTexture];
        [sprite setPhysicsBody:body];
        [node addChild:sprite];

but this code makes one sprite with all frames to node. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长不大的小祸害 2025-01-12 05:37:31

像这样提取帧...

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"spritesheet.plist"];
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"spritesheet.png"];


    [self addChild:spriteSheet];

     NSMutableArray *frames = [[[NSMutableArray alloc]init]retain];


     for(int i = 1; i <= numberFrames; i++) {
          [frames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%@0%d.png", file_name, i]]];
        }


     // Animation object with 0.04 seconds between each frame (~30fps)
        CCAnimation *anim = [CCAnimation animationWithFrames:frames delay:0.04f];

        if(self.sprite){
          // Animate the sprite
          [self.sprite runAction:[CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO]];
         }

Extract frame like this...

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"spritesheet.plist"];
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"spritesheet.png"];


    [self addChild:spriteSheet];

     NSMutableArray *frames = [[[NSMutableArray alloc]init]retain];


     for(int i = 1; i <= numberFrames; i++) {
          [frames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%@0%d.png", file_name, i]]];
        }


     // Animation object with 0.04 seconds between each frame (~30fps)
        CCAnimation *anim = [CCAnimation animationWithFrames:frames delay:0.04f];

        if(self.sprite){
          // Animate the sprite
          [self.sprite runAction:[CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO]];
         }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文