gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:23:38 25 4
gpt4 key购买 nike

我目前开始使用 cocos2d-x 为黑莓/安卓/iOS 构建游戏。

我有使用 texturepacker 创建的角色动画的 png 和 plist。我用 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

最佳答案

这是因为您创建的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

关于c++ - cocos2d-x中如何设置多个动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17815862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com