Sunday, 2 June 2013

How to remove unused/all textures: cocos2d


Syntax for removing unused textures in cocos2d:



        [[CCTextureCache sharedTextureCache] removeUnusedTextures];


Syntax for removing all Texture:

        [[CCTextureCache sharedTextureCache] removeAllTextures];

How to change from one scene to another(scene transition): cocos2d

First import the next scene name in the implementation file:
    
     #import "newSceneName .m"


Then call the next scene using the following line:

    [[CCDirector sharedDirector] replaceScene:[newSceneName scene]];


There are also some special transitions available on cocos2d. some of the examples are give below:

        // For Transition radial clockwise
            [[CCDirector sharedDirector] replaceScene:
                    [CCTransitionRadialCW transitionWithDuration:1.0f scene:[newSceneName scene]]];

        // For Transition shrink grow
            [[CCDirector sharedDirector] replaceScene:
                    [CCTransitionShrinkGrow transitionWithDuration:1.0f scene:[newSceneName scene]]];

        // For Transition split rows
            [[CCDirector sharedDirector] replaceScene:
                    [CCTransitionSplitRows transitionWithDuration:1.0f scene:[newSceneName scene]]];

        // For Transition turn off tiles
            [[CCDirector sharedDirector] replaceScene:
                    [CCTransitionTurnOffTiles transitionWithDuration:1.0f scene:[newSceneName scene] ]];

        // For Transition zoom and flipping X with orientation
            [[CCDirector sharedDirector] replaceScene:
                    [CCTransitionZoomFlipX transitionWithDuration:1.0f
                    scene:[newSceneName scene] orientation:kOrientationRightOver]];

Saturday, 1 June 2013

How to detect shake using accelerometer: cocos2d/Xcode


    // in init //
        bool shake_once = false;
        self.isAccelerometerEnabled = YES;

    // accelerometer method //

    -(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
        {
            float threshold = 2;
   
            if(acceleration.x > threshold || acceleration.x < -threshold ||
               acceleration.y > threshold || acceleration.y < -threshold ||
               acceleration.z > threshold || acceleration.z < -threshold){
       
                if(!shake_once){
                    shake_once =true;
                    NSLog(@"shaked shaked...");
           
                }
            }
            else{
                shake_once = false;
            }
   
   
        }

How to open an url: cocos2d/Xcode


Syntax:

    [[UIApplication sharedApplication]
                  openURL:[NSURL URLWithString:@"http://devblogdk.blogspot.in/"]];

How to create UIAlert View: cocos2d/Xcode


Syntax:

    [self Alertfunction];   // Call Alertfunction


    -(void)Alertfunction {
       
        UIAlertView* dialog = [[UIAlertView alloc] init];
        [dialog setDelegate:self];
        [dialog setTitle:@"Title"];
        [dialog setMessage:@"Do you want to play again?"];
        [dialog addButtonWithTitle:@"Yes"];
        [dialog addButtonWithTitle:@"No"];
        [dialog show];
        [dialog release];
    }


    - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
        {
            if(buttonIndex==0) {
                //do stuff for first button click
            }
            else
                //do stuff for second button click
            }
        }

How to call simple particle effect: cocos2d

You can call several simple particle effects in cocos2d. A smoke particle effect example is shown here.

Syntax:

            CCParticleSmoke *smoke_ = [[CCParticleSmoke alloc]init];
            smoke_.texture = [[CCTextureCache sharedTextureCache]
                                                              addImage:@"smokeImage.png"];
            smoke_.life = 1.0;
            smoke_.duration = 2;  
            smoke_.position = ccp(240, 160);
            [self addChild:smoke_ z:2];

How to terminate the app programmatically: cocos2d

You can simply exit the app programmatically by calling the code line below:

Syntax:
      exit(0);