Hello , I just started using Cocos2dx and I am having a lot of fun learning about it.
I am currently attempting to load a saved animation from my sprite sheet. I have something like this
void HelloWorld::initiateFlyingAnimation()
{
//This is the plist
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(“msprite.plist”);
//This is the real Texture file name this->flyinganimation=Animation::create(); for(int i=0;i<=79;i++) { char szName[100]={0}; sprintf(szName,"tmp-%d.gif",i); //This is the frame names this->flyinganimation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(szName)); } this->flyinganimation->setDelayPerUnit(0.1f); this->flyingAction = CCRepeatForever::create( CCAnimate::create(this->flyinganimation)); flyingAction->setTag(12);
}
as you can see the variable flyingAction
has been declared inside my class. Now in another method of mine I have something like this
Caode Sample A:
void HelloWorld::UpdateEagleMovement(float dt)
{
//Check if the eagle is suppose to be moving
if(eglmotion.motionDirection == HelloWorld::EagleMotion::EnumRight)
{
sprite_eagle->runAction(this->flyingAction); //Crashes on this
}
}
Now when I run the above method sprite_eagle->runAction(this->flyingAction);
causes a crash. Why is that ? since initiateFlyingAnimation
method runs before this code. However if I do this the code works
code Sample : B
void HelloWorld::UpdateEagleMovement(float dt)
{
//Check if the eagle is suppose to be moving
if(eglmotion.motionDirection == HelloWorld::EagleMotion::EnumRight)
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("msprite.plist");
Animation* animation=Animation::create();
for(int i=0;i<=79;i++)
{
char szName[100]={0};
sprintf(szName,"tmp-%d.gif",i);
animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(szName));
}
animation->setDelayPerUnit(0.1f);
this->flyingAction = CCRepeatForever::create( CCAnimate::create(animation));
this->flyingAction->setTag(12);
//this->addChild(sprite_eagle, 0, 23);
sprite_eagle->runAction(this->flyingAction);
}
}
Why doesnt code Sample A work if it is being called after the void HelloWorld::initiateFlyingAnimation()
?