During my hero init, i call the method below to setup the animations (declared in header’s public) between the player states. When a player state changes to say, baseballbat, it triggers a change in the sprite animation. I’m doing that in the change state method (also below). When it changes action manager fires a bad access request. I attempted a ->retain(), which runs, but isn’t referencing the animation correctly and the program crashes shortly after. Please help.
void Player::createWalkingAnimation()
{
// Switching Weapons
char str[100] = {};
Animation* aniFrames = Animation::create();
aniFrames->setDelayPerUnit(0.5f);
//switch to bat
for(int i = 16; i <= 21 ; i ++)
{
sprintf(str,"ZF-hero-%02d.png",i);
aniFrames->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(str));
}
switchToBat = Animate::create(aniFrames);
switchToTorch = switchToBat->reverse();
aniFrames = Animation::create();
aniFrames->setDelayPerUnit(0.5f);
// walk with Bat
for(int i = 6; i < 9 ; i ++)
{
sprintf(str,"ZF-hero-%02d.png",i);
aniFrames->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(str));
}
walkWithBat = Animate::create(aniFrames);
// walk with torch
aniFrames = Animation::create();
aniFrames->setDelayPerUnit(0.5f);
for(int i = 4; i <= 5 ; i ++)
{
sprintf(str,"ZF-hero-%02d.png",i);
aniFrames->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(str));
}
walkWithTorch = Animate::create(aniFrames);
// Attack with torch
aniFrames = Animation::create();
aniFrames->setDelayPerUnit(0.5f);
for(int i = 1; i <= 3 ; i ++)
{
sprintf(str,"ZF-hero-%02d.png",i);
aniFrames->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(str));
}
attackWithTorch = Animate::create(aniFrames);
// attack with bat
aniFrames = Animation::create();
aniFrames->setDelayPerUnit(0.5f);
for(int i = 1; i <= 3 ; i ++)
{
sprintf(str,"ZF-hero-%02d.png",i);
aniFrames->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(str));
}
attackWithBat = Animate::create(aniFrames);
// needs to be in a switch statement that checks states at update
animation = switchToBat;
}
void Player::handleState()
{
CCLOG("Hangling hero state");
switch(characterState){
case kStateIdleBaseballBat:
animation = walkWithBat;
break;
case kStateIdleFlameThrower:
animation = walkWithTorch;
break;
case kStateSwitchingToBat:
animation = switchToBat;
currentWeapon = kBat;
break;
case kStateSwitchingToFire:
animation = switchToTorch;
currentWeapon = kTorch;
break;
case kStateAttackingBat:
animation = attackWithBat;
break;
case kStartAttackingFire:
animation = attackWithTorch;
break;
case kStateTakingDamage:
//animation = walkWithBat;
break;
case kStateWalkingFlameThrower:
animation = walkWithTorch;
break;
case kStateWalkingBasballBat:
animation = walkWithBat;
break;
default:
CCLOG("Unhandled new state %d",characterState);
break;
}
action = Speed::create(animation,0.0f);
pSprite->stopAllActions();
pSprite->runAction(action);
}