Search code examples
cocos2d-iphonetetrisccaction

CCMoveBy behaviour


I'm getting stuck to implement some Cocos2D animations for my Tetris clone(that works perfectly, no logic bugs, i just want to perform some smooth animation when deleting rows).

The current code(no animation) just drops the block position, like this:

   block.position = ccp(block.position.x, block.position.y - kBlockSize);

This happens in a for loop for, classic tetris programming. But when i try to animate, like this:

id move = [CCMoveBy actionWithDuration:0.5f position:(0, -kBlockSize)];
[block runAction:move];

Some blocks just moves down once, even tough the action may be called multiple times for the same block(when breaking more than one row for example)...

Why that happens ? I know it's a little bit confusing, but the point is that i'm doing the same stuff and getting different results...i could post more code to help clarify!

Thanks!


Solution

  • I'm quite sure actions are parallel operations so you could be calling a CCMoveBy action before a previous one is completed. Some alternatives I have used are...

    1. Monitor for when the action completes by using a CCSequence finishing with a CCCallFunc action that sets a flag. Something like...

      id myAction = [[CCSequence runWithActions:[CCMoveBy actionWithDuration:0.5f position:(0, -kBlockSize)], [CCCallFunc actionWithTarget:self selector:@selector(myFunc)], nil]

    2. Roll your own solution using a velocity variable in a tick or update function where you can get a hold of delta time/# of ticks since the last update

    Hope some of that helps.