如何在 cocos2d-x 中设置多个动画
How to set multiple animations in cocos2d-x
我目前从cocos2d-x开始为黑莓/安卓/iOS开发游戏。
我有 png 和 plist 用于使用 texturepacker 创建的角色的动画。我用CCSpriteBatchNode
加载它们,然后CCSpriteFrameCache
然后我使用我创建的函数将所有帧加载到帧数组中,然后创建一个CCAnimation
对象并存储使用动画创建CCAnimate
对象(代码更清晰(问题是我有一个检测触摸的功能,它应该循环浏览所有动画, 但它总是崩溃。
这是一些代码(这在init()
中(:
_batchNode = CCSpriteBatchNode::create("Character/girl.png");
_cache = CCSpriteFrameCache::sharedSpriteFrameCache();
_cache->addSpriteFramesWithFile("Personajes/girl.plist");
_character = CCSprite::createWithSpriteFrameName("girlneutral1.png");
_character->setPosition(ccp(winSize.width * 0.1, winSize.height * 0.5));
_batchNode->addChild(_character, 1);
this->addChild(_batchNode);
createAnimation(0, "girlpush", 8, 0.15f);
createAnimation(1, "girlneutral", 4, 0.3f);
createAnimation(2, "girlcrash", 12, 0.3f);
createAnimation(3, "girljump", 12, 0.2f);
createAnimation(4, "girltrick", 12, 0.3f);
_character->runAction(CCRepeatForever::create( _charanimation[3]));
this->setTouchEnabled(true);
加载动画的函数(_charanimation[] 只是一个 CCAnimate 数组(:
void HelloWorld::createAnimation(int a, CCString animation_name, int frames, float delay)
{
CCArray* animframes = CCArray::createWithCapacity(frames);
char str[100] = {0};
for(int i = 1; i <= frames; i++)
{
sprintf(str, "%s%d.png", animation_name.getCString(), i);
CCSpriteFrame* frame = _cache->spriteFrameByName( str );
animframes->addObject(frame);
}
_charanimation[a] = CCAnimate::create(CCAnimation::createWithSpriteFrames(animframes, delay));
//_charanimation[a]->getAnimation()->setLoops(-1);
}
我让动画工作(我用runAction()
设置的动画(,但是如果我尝试更改动画,例如,当我触摸屏幕时:
void HelloWorld::ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event)
{
action++;
action%=5;
_character->stopAllActions();
_character->runAction( CCRepeatForever::create(_charanimation[action]));
char str[100];
sprintf(str, "Animation: %d", action);
pLabel->setString(str);
}
它崩溃了...我不知道我是否做错了,如果有人能帮忙,我会感激的。
如果我在runAction()
中更改动画,它会正确显示动画,但我无法通过触摸在游戏中更改动画。
顺便说一句,这是我在控制台中遇到的错误:
cocos2d-x debug info Assert failed: reference count should greater than 0
In function retain -- ....cocoaCCObject.cpp:92 m_uReference > 0 -- assertion failed
这是因为您创建的CCAnimate
对象是自动释放对象,并且您没有保留该对象。如果未保留自动释放对象,则会自动删除这些对象(显式保留或由其他对象保留(。
添加到数组时 你可以做
CCAnimate *animate = CCAnimate::create(CCAnimation::createWithSpriteFrames(animframes, delay));
animate->retain();
_charanimation[a] = animate;
当一切都结束时,不要忘记释放数组中的所有对象
_charanimation[index]->release();
注意:
您可以使用 Cocos2d 的
CCArray
,而不是使用简单的 C 或 C++ 数组,一旦对象添加到数组中,它就会保留对象。
例如:(内存处理类似于Objective-C(
_charanimation = new CCArray();
//To add object to array
_charanimation->addObject(object); //This will retain the object
//To get an object
_charanimation->objectAtIndex(index);
_charanimation->lastObject();
_charanimation->randomObject();
//To remove object
_charanimation->removeObjectAtIndex(index); //This will release object
_charanimation->removeObject(object); //This will release object
//Dont forget delete array later
delete (_charanimation);
您可以参考此链接以获取有关CCArray的更多信息
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 在C++/Linux中设置单调时钟的一些技巧
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何在Qt中设置围绕固定点的线的动画
- QML:设置动画图像源,而不加载资源中的文件
- 无法在openGL和GLUT中为我想要的确切对象设置动画
- 要设置动画的QPropertyAnimation中支持什么属性
- 在不重绘整个背景图的情况下为模型设置动画--OPENGL
- 单击按钮后对QML矩形的颜色设置动画
- Cocos2dx.如何在动画序列之后设置回调(传递参数)
- 设置QGraphicsPixmapItem动画的最简单方法
- 在Qt中设置大量对象动画的最简单方法
- 设置QGraphicsLineItem动画时出现问题
- C++使用MoveWindow()为按钮设置动画
- 如何在 cocos2d-x 中设置多个动画
- SDL如何在按键时设置精灵的动画
- 如何在曲线上为实心球体设置动画
- 在Qt中设置QGraphicsObject/QGraphicsItem的动画
- 我如何设置一个if语句来动画一个精灵