本人在使用精灵表单创建动画的过程中突然遇到了一些个问题,下面进行一下分析总结。

根据在Cocos2d-iphone中的经验,我写出了如下的代码:

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("my.plist");
CCSpriteBatchNode *batchNode = CCSpriteBatchNode::create("my.png");
this->addChild(batchNode); CCArray *frameArray = CCArray::create(); for (int i=1; i<=4; i++) {
CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("switch_off_1_%d.png",i)->getCString());
frameArray->addObject(frame);
} CCAnimation *animation = CCAnimation::create(frameArray, 0.4); CCAnimate *animate = CCAnimate::create(animation); CCSprite *iconSprite = CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("switch_off_1_4.png")); iconSprite->setPosition(ccp(100,100)); this->addChild(iconSprite); iconSprite->runAction(CCRepeatForever::create(animate));

但是程序出错:

Cocos2d: Assert failed: element type is wrong!
Assertion failed: (dynamic_cast<CCAnimationFrame*>(*__arr__)), function initWithAnimationFrames, file /Users/ios/Desktop/demo/demo/libs/cocos2dx/sprite_nodes/CCAnimation.cpp, line 144.

原来是

CCAnimation *animation = CCAnimation::create(frameArray, 0.4); 出了问题!

直接用create方法,会进入initWithAnimationFrames方法,里面会检查传入的CCArray是否是CCAnimationFrame*类型,而显然,我代码中CCArray中的元素是CCSpriteFrame*,所以出现了问题。

那么解决方法是:改用

CCAnimation *animation =CCAnimation::createWithSpriteFrames(frameArray,0.4); 这个方法就可以了。

注意:在xcode中代码提示 createWithSpriteFrames 这个方法只有一个参数,

但是,实际上进入这个函数就回发现,它是有两个参数的。

/* Creates an animation with an array of CCSpriteFrame and a delay between frames in seconds.
The frames will be added with one "delay unit".
@since v0.99.5
*/
static CCAnimation* createWithSpriteFrames(CCArray* arrayOfSpriteFrameNames, float delay = 0.0f);

注意其中的注释:
Creates an animation with an array of CCSpriteFrame and a delay between frames in seconds

对比create方法中的注释:

Creates an animation with an array of CCAnimationFrame, the delay per units in seconds and and how many times it should be executed.

 /* Creates an animation with an array of CCAnimationFrame, the delay per units in seconds and and how many times it should be executed.
@since v2.0
*/
static CCAnimation* create(CCArray *arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops);
static CCAnimation* create(CCArray *arrayOfAnimationFrameNames, float delayPerUnit) {
return CCAnimation::create(arrayOfAnimationFrameNames, delayPerUnit, 1);
}

这样就知道出错的原因了吧!

下面给出另一种方法创建动画的方式:

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("my.plist");
CCSpriteBatchNode *batchNode = CCSpriteBatchNode::create("my.png");
this->addChild(batchNode); CCAnimation *animation = CCAnimation::create(); for (int i=1; i<=4; i++) {
CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("switch_off_1_%d.png",i)->getCString());
animation->addSpriteFrame(frame);
} animation->setDelayPerUnit(0.4); CCAnimate *animate = CCAnimate::create(animation); CCSprite *iconSprite = CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("switch_off_1_4.png")); // CCSize size = CCDirector::sharedDirector()->getWinSize();
iconSprite->setPosition(ccp(100,100)); this->addChild(iconSprite); iconSprite->runAction(CCRepeatForever::create(animate));

参考文章:http://blog.csdn.net/rabbitlbj/article/details/8765575

Cocos2d-x 创建(create)动画对象CCAnimation报错分析的更多相关文章

  1. 【原创】MySQL Can't create a new thread报错分析

    今天有两台服务器都出现了Can't create a new thread报错. [故障处理过程] 故障发生后登录服务器,检查mysql进程正常,但登录mysql报下面错误 ERROR 1135 (H ...

  2. const变量赋值报错分析

    const变量赋值报错分析 const变量赋值报错 从变量到常量的赋值是合法C++的语法约定的, 如从char 到const char顺畅: 但从char **到 const char **编译器就会 ...

  3. std::unique_ptr使用incomplete type的报错分析和解决

    Pimpl(Pointer to implementation)很多同学都不陌生,但是从原始指针升级到C++11的独占指针std::unique_ptr时,会遇到一个incomplete type的报 ...

  4. mysql5.7密码修改与报错分析

    1.修改密码 修改密码: vim /etc/my.cnf 的mysqld字段加入skip-grant-tables 重启MySQL,service mysqld restart 终端输入 mysql ...

  5. MySQL创建外键约束的报错Error : Can't create table '#sql-534_185' (errno: 150)

    总得来说是因为两个表的字段类型不一致,例如: 两个字段的类型或大小不严格匹配,一个为tinyint,另一个为char:或一个为int(10)另一个为int(9)也是不行的,即使都为int(10),但一 ...

  6. PostgreSQL 大对象导出报错问题分析

    1.前言 在处理用户问题过程遇到一个问题.用户通过pg_dump导出 bytea 对象时,当行的大小超过 1G时,会报错: [v8r6c5b41@dbhost01 ~]$ sys_dump -t t1 ...

  7. 创建small表空间size32G报错ORA-01144

    #报错现象 #创建Oracle数据库11g默认标准块大小8K,直接分配32g报错 create tablespace ceshi datafile '+data' size 32g * ERROR a ...

  8. Anaconda Error opening file for writing , failed to create anacoda menu等报错问题解决方案

    安装anaconda的时候可能会遇到这个报错, 原因可能是:路径不允许有空格 此外发生报错failed to create anacoda menu, 解决方案 进入 cmd,找到你安装的位置(我的是 ...

  9. Eclipse/MyEclipse向HDFS中如创建文件夹等操作报错permission denied解决办法

    不多说,直接上干货! 问题现象 当执行创建文件的的时候, 即: String Path = "hdfs://host2:9000"; FileSystem fileSystem = ...

随机推荐

  1. C语言拾遗--static

    C程序一直由下列部分组成: 正文段——CPU执行的机器指令部分:一个程序只有一个副本:只读,防止程序由于意外事故而修改自身指令: 初始化数据段(数据段)——在程序中所有赋了初值的全局变量,存放在这里. ...

  2. 转:c语言EOF是什么?(及getchar()和putchar用法)

    我学习C语言的时候,遇到的一个问题就是EOF. 它是end of file的缩写,表示"文字流"(stream)的结尾.这里的"文字流",可以是文件(file) ...

  3. 求 n! (大数问题)----------HDOJ1042

    以下只YY算法,c++实现,java大神绕道..... (1)位数的估计     显然对于大数(比如1000的阶乘)的阶乘我们找不到一个数据类型存放这个数,是在太大啦....     那么现在来估计一 ...

  4. HDU 4720Naive and Silly Muggles热身赛2 1005题(分锐角钝角三角形讨论)

    Naive and Silly Muggles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  5. Netty In Action中文版 - 第五章:Buffers(缓冲)

    本章介绍 ByteBuf ByteBufHolder ByteBufAllocator 使用这些接口分配缓冲和运行操作 每当你须要数据传输时,它必须包括一个缓冲区.Java NIO API自带的缓冲区 ...

  6. Ubuntu中查看硬盘分区UUID的方法(所有Linux目录的解释)

    在Ubuntu中UUID的两种获取方法,至于UUID是什么,你可以大概理解为分区的标识符,像条形码那样. 在终端中输入下面的命令就可心查看到分区UUID了.命令1.sudo blkid 命令2.ls ...

  7. 图示CCScrollView的相关概念

    (转载请注明原文地址:http://blog.csdn.net/while0/article/details/11527899) 见下图: 1)设置ScrollView的视口大小的函数是:setVie ...

  8. 基于visual Studio2013解决C语言竞赛题之1021九九乘法表

         题目 解决代码及点评 /* 功能:请编程序按如下格式打印一个直角三角形的九九乘法表: 1 2 3 4 5 6 7 8 9 4 6 8 10 12 14 16 18 9 1 ...

  9. GAE+bottle+jinja2+beaker快速开发demo - Python,GAE - language - ITeye论坛

    GAE+bottle+jinja2+beaker快速开发demo - Python,GAE - language - ITeye论坛     :GAE+bottle+jinja2+beaker快速开发 ...

  10. ASP.NET、WinForm、C# - 配置文件信息读取 [ Web.config || Appconfig ]

    <configuration> <appSettings> <add key="name" value="HF_Ultrastrong&qu ...