如何在 cocos2d-x 中设置多个动画

How to set multiple animations in cocos2d-x

本文关键字:动画 设置 cocos2d-x      更新时间:2023-10-16

我目前从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的更多信息