run another animation after running an animation with ccbanimationmanager - cocos2d-iphone

I'm running cocos2d with cocosbuilder 2.1 and I use the cocosbuilder animation delegate (CCBAnimationManagerDelegate::completedAnimationSequenceNamed) to get notified when an animation has completed and take actions like firing another cocosbuilder animation.
It runs fine the first time the foodfactoryshow animation is run from the delegate and after the animation is completed it also runs restoration animation correctly. However, when restoration animation is completed, the parameter name for -(void) completedAnimationSequenceNamed method is NULL!?
-(void) completedAnimationSequenceNamed:(NSString*)name
{
if ([name isEqualToString:#"foodfactoryshow"])
{
[manager runAnimationsForSequenceNamed:#"restoration"];
}
if ([name isEqualToString:#"restoration"])
{
[self colorLayerChanged];
self.gameLayer.isLock = true;
}
}
Is this a bug or am I not supposed to run animations from the CCBAnimationManagerDelegate::completedAnimationSequenceNamed method!?
Thanks in advance for your help.

I believe it is a CCBReader bug. There is an open issue about it in the CocosBuilder github page (https://github.com/cocos2d/CocosBuilder/issues/121). It should be fixed in the latest version of CocosBuilder + CCBReader, however, if you want to use the 2.1 version you can change the "sequenceCompleted" method of CCBAnimationManager to the following:
- (void) sequenceCompleted
{
NSString *completedSequenceName = [runningSequence.name copy];
int nextSeqId = runningSequence.chainedSequenceId;
runningSequence = NULL;
if (nextSeqId != -1)
{
[self runAnimationsForSequenceId:nextSeqId tweenDuration:0];
}
[delegate completedAnimationSequenceNamed:completedSequenceName];
[completedSequenceName release];
}

Related

Pause/Resume Action/Animation on Sprite in Cocos2d

Using Cocos2d-x and C++, I'm trying to play and pause an animation for a sprite.
I'm using version 3.15.1 of Cocos2dx.
I have a class called PlayerSprite which is derrived from the cocos2d::Sprite class. Inside PlayerSprite initialization, I've setup my animation with the following code:
SpriteBatchNode* playerSpriteBatch = SpriteBatchNode::create("player.png");
SpriteFrameCache* spriteFrameCache = SpriteFrameCache::getInstance();
spriteFrameCache->addSpriteFramesWithFile("player.plist");
Vector<SpriteFrame*> animFrames(2);
char str[18] = { 0 };
for (int i = 1; i < 3; i++) {
sprintf(str, "player_idle_%d.png", i);
SpriteFrame* frame = spriteFrameCache->getSpriteFrameByName(str);
animFrames.pushBack(frame);
}
Animation* idleAnim = Animation::createWithSpriteFrames(animFrames, 0.8f);
self->idleAction = self->runAction(RepeatForever::create(Animate::create(idleAnim)));
self->idleAction->setTag(0);
When I run the code, it works fine and the animation loops correctly.
In my void update() method, I am trying to pause/play the action/animation based of weather the player is moving or idle.
I do this with the following code:
const bool isIdleActionRunning = this->getNumberOfRunningActionsByTag(0) > 0 ? true : false;
const bool isMoving = !vel.isZero();
if (!isMoving && !isIdleActionRunning) {
// Player is idle AND animation is not running
// Run animation
this->runAction(idleAction);
} else if (isMoving && isIdleActionRunning) {
// Player is moving but animation is running
// Pause animation
this->stopActionByTag(0);
}
When I run this code now, my character falls, and as soon as he hits the gound, I get an error at this->runAction(idleAction); saying:
Access violation reading location 0xDDDDDDE5
I believe this is caused due to this->stopActionByTag(0) deleting the action pointer. I've tried to clone the action to avoid this but have had no success.
I know this is a bit late and you might already have solved this but here goes...
Your problem is that you cannot use one instance of Action (idleAction) multiple times. So, once you have run it and removed it, it is released and cannot be used. So, you have 2 options now,
Either create a new idleAction Action every time before running the action.
Or, have an idleAction retained and don't run it ever. Instead, create a clone of this idleAction and run a new clone each time. i.e.
idleAction->retain();
const bool isIdleActionRunning = this->getNumberOfRunningActionsByTag(0) > 0 ? true : false;
const bool isMoving = !vel.isZero();
if (!isMoving && !isIdleActionRunning) {
// Player is idle AND animation is not running
// Run animation
Action idleActionClone = idleAction->clone();
this->runAction(idleActionClone);
} else if (isMoving && isIdleActionRunning) {
// Player is moving but animation is running
// Pause animation
this->stopActionByTag(0);
}
Solution: call retain() to keep your action.
It's a matter of memory management of cocos2d-x.
In create() function of your RepeatForever class (derived from Ref), the reference count is set to 1 and there is a line of code ret->autorelease() before returning the object, which means this object will be released automatically at the end of this frame.
You called runAction() function the same frame you created it, the action is retained by ActionManager, it's reference count set to 2, and then 1 at the end of the frame (autorelease).
After your stopping it, it's released by ActionManager, reference count set to 0 and it's deleted. After this you use your action, it will be an access violation method.
*Edit: don't forget to release the action manually when PlayerSprite is deleted, or it's a leak.
When you stop action it's being recycled from memory. In order to play action once more, you have to recreate it. So you may just make a creator function, which returns your animation. The downside is you're recreating animation each time and it'll also play from the beginning (technically you can rewind it).
But I've developed a simpler technique to pause/resume animations:
Let's say you have an action:
action = MoveBy::create(5.0f, Vec2(50, 100));
Now, you can embed this action into Speed action like this:
action = Speed::create(MoveBy::create(5.0f, Vec2(50, 100)), 1.0f);
1.0f - is speed, so it's normal action rate. Now to pause just call:
action->setSpeed(0.0f);
and to resume:
action->setSpeed(1.0f);
you can also use different speed if you need it for some reason or another ;)

Cocos2d-x 2.2.5 scene replacing

Recently i started to study Cocos2d-x (C++). Problem is following, i do not know what is my problem, when i try to change scenes(when push the but).
void HelloWorld::menuCloseCallback(cocos2d::CCObject* pSender)
{
CCScene *pScene = UzdevumsScene::scene();
CCDirector::sharedDirector()->replaceScene(pScene);
}
Error:
error: undefined reference to 'UzdevumsScene::scene()'
When i use this line, i get nothing more than black screen(when i push the button).
CCScene *UzdevumsScene = CCScene::create();
CCTransitionPageTurn *HelloWorld = CCTransitionPageTurn::create(1, UzdevumsScene, true);
CCDirector::sharedDirector()->replaceScene(UzdevumsScene);

Cocos2D how to remove/release/clean up CCMenu

How can I add/remove CCMenu when the same button is clicked? I have added some code..
Thanks in advance..
CCMenu *menu;
if (!isMenuVisible) {
CCMenuItemSprite *item = [CCMenuItemSprite itemFromNormalSprite: .......];
menu = [CCMenu menuWithItems:item, nil];
[self addChild:menu];
} else {
// [menu cleanup];/// didn't work
// [menu removeFromParentAndCleanup:YES]; //// didnt work
// [menu removeAllChildrenWithCleanup:YES]; //// didn't work
}
isMenuVisible = !isMenuVisible;
}
you probably want to have the top line in your .h file, making the menu an iVar, to that the reference is kept in between successive executions of this code. Set menu to nil after you remove it.
One way - to create two menus. One for show/hide button, another for all buttons that have to be shown/hidden. It is not good way.
Another way is just to add/remove menuitems to the menu. I mean something like that:
- (void) removeItems
{
for(CCNode* item in _addedItems)
{
[item removeFromParentAndCleanup: YES];
}
[_addedItems removeAllObjects];
}
- (void) addItems
{
// create needed items and add them as children
// to your menu and add them to _addedItems array
// to be able to remove added objects
}
Also before using methods like cleanup, check it's code or at least cocos2d documentation. In your case it was comletely useless.

cocos2d-iphone: init method does not work properly

I'm studying Strougo/Wenderlich tutorial (space Viking project). I got troubles with chapter 4.
In RadarDish.m:
-(void)initAnimations
{
[self setTransmittingAnim: [self loadPlistForAnimationWithName:#"transmittingAnim" andClassName:NSStringFromClass([self class])]];
}
-(void)changeState:(CharacterStates)newState {
[self stopAllActions];
id action = nil;
[self setCharacterState:newState];
switch (newState) {
.
.
case kStateIdle:
action = [CCAnimate actionWithAnimation:transmittingAnim
restoreOriginalFrame:NO];
break; }
if (action != nil) {
[self runAction:action];
}
}
-(id)init
{
self=[super init];
if (self!=nil) {
.
.
[self initAnimations];
.
.
}
return self;
}
Exact the same code as in the tutorial. Failure:
*** Assertion failure in -[CCAnimate initWithAnimation:], /Users/macowner/Documents/examples/SpaceViking/SpaceViking/libs/cocos2d/CCActionInterval.
Using debugger with breakpoints, i noticed that value of transmittingAnim = nil.
So, if i put line with
[self setTransmittingAnim:
[self loadPlistForAnimationWithName:#"transmittingAnim" andClassName:NSStringFromClass([self class])]];
into case of
-(void)changeState then animation works correctly.
Why [self initAnimations] from (id)init is not called?
Im using cocos2d v.2 templates.
Great thanks in advance.
I had problems because I have been building project using cocos 2d v.2.0, while tutorial is based on cocos 2d templates v.1.x.x If you are going to follow the book "Learning Cocos2D", I strongly recommend you loading cocos2d-iphone version 1.0.1. Here is the link download cocos2d 1.x.x branch
if you still want to use latest cocos2d templates, I can give you some advice:
Follow the instructions in this link cocos2d v2.0 migration guide
You are going to have a lot of deprecations and changes to fix, so use this link to understand how to fix those deprecations and changes.
Now few words about the solution of the problem I mentioned here. In each of the GameObjects, EnemyObjects, and PowerUps, I added a method to override initWithFrameName.
-(id) initWithSpriteFrameName:(NSString*)frameName{
if ((self=[super init])) {
if ((self = [super initWithSpriteFrameName:frameName])) {
CCLOG(#"### RadarDish initialized");
[self initAnimations]; // 1
characterHealth = 100.0f; // 2
gameObjectType = kEnemyTypeRadarDish; // 3
[self changeState:kStateSpawning]; // 4
}
}
return self;
}
    This allows the GameObject and GameCharacter init methods to run before the CCSprite's initWithSpriteFrameName method to run.
    
    The Viking GameObject had to have a slightly different solution because it is initialized with initWithSpriteFrame rather than initWithSpriteFrameName. However, the override implementation is basically the same as the example of RadarDish above.

Callback when Cocos2d CCParticleSystem has finished?

I'd like to run a callback/selector when a Cocos2d CCParticleExplosion is completely finished. How do I do this?
I tried using scheduleOnce with the same duration as the emitter, but that finish too soon since I guess the duration of the emitter controls for how long it will emit new particles, but not how long the complete animation lasts.
Try sequencing the action (using CCSequence) with a CCCAllFunc Action. After one action runs, the other runs, the CCCAllFunc can be assigned to the selector/method of your choice.
Not sure if this is acceptable, but I tested and "it works on my mac".
in CCParticleSystem.h
// experimental
typedef void (^onCompletedBlock)();
#property (readwrite, copy) onCompletedBlock onComplete;
in CCParticleSystem.m
#synthesize onComplete;
in the same file update method,
_particleCount--;
if( _particleCount == 0 && self.onComplete)
{
[self onComplete]();
[[self onComplete] release];
}
if( _particleCount == 0 && _autoRemoveOnFinish ) {
[self unscheduleUpdate];
[_parent removeChild:self cleanup:YES];
return;
}
In your code
particleSystem.autoRemoveOnFinish = YES;
particleSystem.position = ccp(screenSize.width/2, screenSize.height/4);
particleSystem.onComplete = ^
{
NSLog(#"completed ..");
};
again, quite experimental..