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

时间:2013-07-23 16:30:04

标签: c++ animation sprite cocos2d-x

我目前正在使用cocos2d-x为blackberry / android / iOS构建游戏。

我有png和plist用于使用texturepacker创建的角色的动画。我用CCSpriteBatchNodeCCSpriteFrameCache加载它们然后我使用我创建的函数将所有帧加载到一个帧数组中,然后创建一个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 -- ..\..\cocoa\CCObject.cpp:92 m_uReference > 0 -- assertion failed

1 个答案:

答案 0 :(得分:3)

这是因为您创建的CCAnimate对象是 autorelease 对象而您保留对象。 Autorelease 对象如果不是保留,则会自动删除,无论是显式还是其他对象。

添加到阵列时可以执行

CCAnimate *animate = CCAnimate::create(CCAnimation::createWithSpriteFrames(animframes, delay));
animate->retain();
_charanimation[a] = animate;

当一切都结束时,不要忘记释放数组中的所有对象

_charanimation[index]->release();
  • 注意:

    除了使用简单的C或C ++数组,您可以使用Cocos2d的CCArray 保留对象一旦添加到数组中。

例如:(内存处理类似于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);

You can refer this link for more on CCArray