iphone - 如何在不使用TexturePacker的情况下在Cocos2D中添加动画图像?

标签 iphone ios objective-c cocoa-touch cocos2d-iphone

我是 iOS 开发新手。 我也开始学习Cocos2D了。

我已阅读本教程:http://www.raywenderlich.com/tutorials#cocos2d

对于初学者来说,这是一个极好的教程,但我也对图像动画感兴趣。我怎样才能完成动画?

所以我阅读了关于如何使用简单项目放置动画图像的教程(从上面的链接描述)

在本教程中,我使用了 TexturePacker 并且它正在工作...但我想了解更多关于如何使用 TexturePacker 为图像添加动画效果.

这可能吗?如果是这样,请解释如何操作或链接到如何使其工作的教程。

提前致谢。

最佳答案

您可以从文件运行动画。

    CCSprite *coin = [CCSprite spriteWithFile:@"MyImage_1.png"];
    coin.position  = ccp(mS.width*0.5f, mS.height*0.5f);
    [self addChild:coin z:2];

    {
        NSString *animationName = @"UNIQUE_ANIMATION_NAME";

        CCAnimation* animation = nil;
        animation = [[CCAnimationCache sharedAnimationCache]  animationByName:animationName];

        if(!animation)
        {
            NSMutableArray *animFrames = [NSMutableArray array];

            for( int i=1;i<=5;i++)
            {
                NSString* path = [NSString stringWithFormat:@"MyImage_%d.png", i];
                CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:path];
                CGSize texSize = tex.contentSize;
                CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
                CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:tex rect:texRect];
                [animFrames addObject:frame];
            }

            animation = [CCAnimation animationWithSpriteFrames:animFrames];

            animation.delayPerUnit = 0.175f;
            animation.restoreOriginalFrame = YES;

            [[CCAnimationCache sharedAnimationCache] addAnimation:animation name:animationName];
        }

        if(animation)
        {
            CCAnimate *animAction  = [CCAnimate actionWithAnimation:animation];
            [coin runAction:animAction];
        }
    }

关于iphone - 如何在不使用TexturePacker的情况下在Cocos2D中添加动画图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17609439/

相关文章:

ios - AdMob 原生高级广告单元 ID

ios - 如何快速制作多行表格 View 标题?

objective-c - 如何在objective-c中加入两个flv文件

iphone - 检查 NSNumber 是否有尾随 .00 数字

iphone - 为 iPad 和 iPhone 设计输入按钮

iphone - IOS5 应用程序/未加载网络视频后,如何在第一个视频完成后播放下一个视频

iphone - 何时何地放置@class 声明

iOS:如何在 Storyboard中实际没有单元格的情况下将可重用的 'default' 单元格出列

iphone - 在 “ipod”麦克风中播放iPhone音频

objective-c - 为 iOS 编写格式化输出