Cocos2d Archives

Particle Effect in cocos 2d

Here is the code to give particle effect in cocos 2d using a plist:

CCParticleSystemQuad *effect = [CCParticleSystemQuad particleWithFile:@"bubbles.plist"];
[self addChild:effect];

Wave Effect in cocos 2d

To give wave effect to a particular sprite, use the code as given below:

CCWaves *waves = [CCWaves actionWithWaves:-1 amplitude:3 horizontal:YES vertical:NO grid:ccg(15,10) duration:4];
[bgImage runAction: [CCRepeatForever actionWithAction:waves]];

How to select random numbers within a range?

In cocos 2d, for selecting a random number between a given range, use the code as given below:

arc4random()%(endValue-startValue+1) + startValue;

For example, for selecting a random number between 50 and 149, use the code given below:

arc4random()%100 + 50;

How to select random numbers without repetition?

The below function in cocos 2d is used for selecting a specified number of random numbers from a particular range without repetition.

//Function to select random numbers without repetition

-(void) randomarrayInitialisation:(int)totalRandomNumbers:(int)minNumber:(int)maxNumber
    int randCount=totalRandomNumbers;
    int maxRandom=maxNumber;
    for(int i=0;i<100;i++)
    for(int i=0;i<randCount;i++)
        int rand=minNumber+arc4random()%(maxRandom-minNumber);
        int flag=0;
        for(int j=0;j<randCount;j++)

We can call the function as given below:

[self randomarrayInitialisation:3:0:10];

The above code will select 3 random numbers from the range 0 to 9 and stores it in the variables randomArray[0],randomArray[1],randomArray[2].

Don’t forget to declare randomArray in .h class as follows:

int randomArray[100];

Add the sprite to a separate layer. Give required anchor points for the object and the layer.

Then give  rotations to the object and layer.

Updating labels in cocos2d

[labelname setString:@"string"];

How to convert a UIView into a UIImage

To convert uiview into a uiimage ..You can use the following code.



[certificateView.layer renderInContext:UIGraphicsGetCurrentContext()];

uiimage collageImage = UIGraphicsGetImageFromCurrentImageContext();


[collageImage retain];




How to add email facility in Objective C

To include email facility in your cocos program,you have to add the MessageUI framework in your program.

Then you have to include the following code.



MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];





mailController.mailComposeDelegate = self;

[mailController setSubject:@"Certificate"]; ///for setting the message subject.

[mailController setMessageBody:@"Rate this app if you like it.Find similar products at ." isHTML:YES];///for setting the message body.

[mailController addAttachmentData:UIImageJPEGRepresentation (collageImage,0.5) mimeType:@"image/jpeg" fileName:@"image.jpg"];//for adding an attachmet.

//[mailController setCcRecipients:[NSArray arrayWithObjects:@"",nil]];//to set a ccrecipient.

UIWindow *topWindow;

NSArray *windows = [[UIApplication sharedApplication] windows];

for(topWindow in windows)


if (topWindow.windowLevel == UIWindowLevelNormal)



UIView *rootView = [[topWindow subviews] objectAtIndex:0];

id nextResponder = [rootView nextResponder];

UIViewController *rootViewController = nextResponder;

while (rootViewController.modalViewController != nil)

rootViewController = rootViewController.modalViewController;

UIViewController *topViewController = rootViewController;

[topViewController presentModalViewController:mailController animated:YES];


//Also you have to add the mail composer control delegate..

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error


switch (result)


case MFMailComposeResultSent:


case MFMailComposeResultSaved:


case MFMailComposeResultCancelled:



case MFMailComposeResultFailed:




[controller dismissModalViewControllerAnimated:YES];

[controller release];




Multi Touch in Cocos2d

To recieve multi-touch events, you have to activate them. You can do this by adding the following code in your AppDelegate’s applicationDidFinishLaunching:

[glView setMultipleTouchEnabled:YES];

To reverse all actions

Almost all actions have the reverse method implemented. Basically it creates a new action with the reverse behavior.


id move = [CCMoveBy actionWithDuration:2  position: ccp(80,80)];

id move_reverse = [move reverse];

The move_reverse action will be a CCMoveBy action of duration 2, but with the position value of ccp(-80,-80).

Page 20 of 44« First...10...1819202122...3040...Last »
We Can Build Your App for You. Lets Get Started !