Adding transition effects

If you think adding transitions was cool, you can add transition effects while transitioning between scenes in Cocos2d, and it is very easy!

Getting ready

All we really have to do is change the replaced code written in the previous code with the code specifying the transition effect we want. This is done using the CCTransition class.

How to do it…

Replace the code in the previous section with the code highlighted in the playBtnPressed function, as follows:

-(void)playBtnPressed:(id)sender{

  CCLOG(@"play button pressed");

  //[[CCDirector sharedDirector] replaceScene:[[GameplayScene alloc] initWithLevel:@"1"]];

  CCTransition *transition = [CCTransition transitionPushWithDirection:CCTransitionDirectionLeft duration:0.20];

  [[CCDirector sharedDirector]replaceScene:[[GameplayScene alloc]initWithLevel:@"1"] withTransition:transition];
}

When you run the application, you will see a nice crossfade effect while transitioning between scenes.

How it works…

The main class at work is the CCTransition class here. You can specify the kind of transition effect you want and the duration for which the transition should last. In the preceding example code, I added a 0.2-second duration. You can increase or decrease the value according your game's requirements.

How it works…

There's more…

Cocos2d also gives you a lot of choices while adding effects. You can go through the list and add them to see the kind of effects each has.

There's more…

Change the transition effect to the following and run it to see the new effect:

CCTransition *transition = [CCTransition transitionRevealWithDirection:CCTransitionDirectionLeft duration:0.2];
There's more…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.74.160