[RESOLVED] Create Animation Method pointer problem

I’m having trouble pointing to my spriteframecache variable from one method to another.

bool LocalMapScene::init() {
/…
SpriteBatchNode* spriteSheet = SpriteBatchNode::create(“Sprites/Characters/C1_SpriteSheet.png”);
SpriteFrameCache frameCache = SpriteFrameCache::sharedSpriteFrameCache();*
frameCache->addSpriteFramesWithFile(“Sprites/Characters/C1_SpriteSheet.plist”);
_player = Sprite::createWithSpriteFrameName(“C1_Front_Walk_0.png”);
_player->setPosition(x + tileMap->getTileSize().width / 2, y + tileMap->getTileSize().height / 2);
player->setScale(1);
spriteSheet->addChild(player);
addChild(spriteSheet);
createAnimation(frameCache, 0, "C1_Front_Walk
%d.png", 4, 0.2f);
createAnimation(frameCache, 1, "C1_Left_Walk
%d.png", 4, 0.2f);
createAnimation(frameCache, 2, "C1_Right_Walk
%d.png", 4, 0.2f);
createAnimation(frameCache, 3, "C1_Back_Walk
%d.png", 4, 0.2f);
_player->runAction(RepeatForever::create(animation[1]));
/…
}

void LocalMapScene::createAnimation(cocos2d::SpriteFrameCache passed_cache, int a, cocos2d::String animation_name, int numFrames, int delay)
{
frameCache = passed_cache;
Vector<SpriteFrame
> animFrames(numFrames);
for(int i = 0; i < numFrames; i++)
{
String fileName = StringUtils::format(animation_name.getCString(), i);
SpriteFrame* frame = frameCache->getSpriteFrameByName(fileName.getCString());
animFrames.pushBack(frame);
}
animation[a] = Animate::create(Animation::createWithSpriteFrames(animFrames, delay));
}

LocalMapScene.h
*private:
cocos2d::SpriteFrameCache frameCache;

*public:
void createAnimation(cocos2d::SpriteFrameCache passed_cache, int a, cocos2d::String animation_name, int numFrames, int delay);

My animations don’t play and I get a HEAP error after i close the program.

SpriteFrameCache is a singleton, so you don’t need to pass it around as a parameter.
init method looks fine.
I’m not sure where the variable animation[] exists, but that may be your issue. You either need to use the Animate::create action with some sprite, or retain it if you use it later.

animation[] is an array of the cocos Animate class.
i’ve got it in the header as a private variable.

re edited my createAnimation method.
no more Heap error which obviously was due to recreating the framecache over again.
but now my game crashes.

void LocalMapScene::createAnimation(int a, cocos2d::String animation_name, int numFrames, int delay)
{
	Vector<SpriteFrame*> animFrames(numFrames);
	for(int i = 0; i < numFrames; i++) 
	{
		String fileName = StringUtils::format(animation_name.getCString(), i);
		SpriteFrame* frame = frameCache->getSpriteFrameByName(fileName.getCString());
		animFrames.pushBack(frame);
	}
	animation[a] = Animate::create(Animation::createWithSpriteFrames(animFrames, delay));
	animation[a]->retain();
}

i also tried declaring the array in the

init(){
         Animate* animation[] = {0};
}

this is where it points to when it crashes,

size_type _Hashval(const key_type& _Keyval) const
	{	// return hash value, masked and wrapped to current table size
	**size_type _Num = ((_Traits&)*this)(_Keyval) & _Mask;**
	if (_Maxidx <= _Num)
		_Num -= (_Mask >> 1) + 1;
	return (_Num);
	}

thanks for the reply!

also when i run the animation method as part of the init method, (basically copy and paste the code from animation method and put it into the main method), it works perfectly fine and i get an animated sprite with no errors.

Make sure you’re declaring array size, Animate* animation[16], or use a cocos2d::Vector<Animate*> instead (in which case you won’t need to explicitly call retain). Otherwise look up how to new to initialize an array of pointers. In this case I’d recommend Vector though since you’re already using it with the spriteFrames.

thanks for the reply,

vector array is not being accepted as a compatible parameter of cocos2d::actionInterval.
not sure how to convert it.
here’s my code so far.

main init() {
...
Vector<Animate*>* animation[10];
...
_player->runAction(RepeatForever::create(animation[1]));
...
}

void LocalMapScene::createAnimation(int a, cocos2d::String animation_name, int numFrames, int delay)
{
...
animation[a].pushBack(Animate::create(Animation::createWithSpriteFrames(animFrames, delay)));
}

the animation[1] is red underlined.

You’re trying to combine an array structure with Vector<> structure. You should use one or the other, not both, most of the time (unless you know that you want an array of Vector<>s).

// LocalMapScene.h
class LocalMapScene ... {
// ...
Animate* animateActions[10];
// ...
};

// LocalMapScene.cpp
main init() {
// ...
// Don't need this - it's a local and will go out of scope after init() returns
// Vector<Animate*>* animation[10];
// ...
// note: I've split these into separate lines to prevent wrapping
auto animateAction = animateActions[1];
_player->runAction(RepeatForever::create(animateAction));
// ...
}

void LocalMapScene::createAnimation(int a, cocos2d::String animation_name, int numFrames, int delay)
{
// ...
// note: I've split these into separate lines to prevent wrapping
auto animation = Animation::createWithSpriteFrames(animFrames, delay);
auto animateAction = Animate::create(animation);
animateActions[a].pushBack(animateAction);
}

Redline under animateActions.
“Expression must have class type.”

Animate class doesn’t have member pushBack. nor any member to store a value actually.

The only thing I can think of is some way to convert Vector<Animate*> to Animate but I don’t know how.

Oh my bad, I was modifying your code, didn’t actually test it. That line with error should’ve been:

animateActions[a] = animateAction;

You may find logical issues if you were trying to do something different than what I understood your code to be trying to do. Also, it sounds like you may want to go study c++ if you’re going to continue to use cocos2d-x (c++ version).

thanks for the help and for replying.

i figured out the problem.

it was because i re-declared 2 variables from the header.

removed the second declarations and used my original code.
apparently it was correct the first time but i just had duplicates of framecache and spritesheet.
now it is working perfectly and i can create and call as many animations as i want.

thank you!