Category Archives: Cocos2D

Changing the image of the sprite in cocos2D ?

By | March 17, 2011

Hi, You all know how to add an Image to a sprite. But what if we need to change the image of the same sprite? How to do that? Use the following line of code to change the image of a sprite which already initialized with a sprite. [sampleSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@”newImage.png”]]; where sampleSprite is… Read More »

How to stop the sprite sheet animation in cocos2D ?

By | March 17, 2011

Hi, In one of our previous post we have discussed how to animate sprite sheet in cocos2D. In some cases you may need to stop the sprite sheet animation abruptly. How to do that? Here the way to do that. Use the following. [spriteName stopAction:spriteNameAction]; where spriteName is the name of your sprite & spriteNameAction… Read More »

Sprite Sheet Animation in Cocos2D – An Example

By | March 14, 2011

Hi, Suppose you have a sprite sheet of the desired animation you want. But how will you use that sprite sheet animation in your program using cocos2D? That’s simple. See the following code first. //Initializing a CCSpriteBatchNode with our sprite sheet image CCSpriteBatchNode *newSpriteSheet = [CCSpriteBatchNode batchNodeWithFile:@”jumpingOver.png”]; //Adding the spriteSheet to the layer & setting… Read More »

Copying a Sprite Image to another Sprite in Cocos2D

By | March 11, 2011

Hi, Suppose you want to copy a sprite image to another new sprite while using cocos2D. What you do? See the following code… Let oldSprite be your first sprite CCSprite *oldSprite = [CCSprite spriteWithFile:@”ImgOne.png”]; And newSprite be your second sprite CCSprite *newSprite = [CCSprite spriteWithTexture:[oldSprite texture]]; That’s it! Done! Now newSprite too contain ImgOne as… Read More »

Delete a substring in Objective C, Iphone , Mac………….

By | March 10, 2011

This code snippet deletes a substring from a original string.. /** initialize two strings **/ NSString *string = @”This is heaven”; NSString *part = @”heaven”; /** Get the range of the substring in the original string **/ NSRange range = [string rangeOfString:part]; /** Delete the substring from the original string **/ [string deleteCharactersInRange:range]; Please leave… Read More »

How to make gear joint in Box2D, iPhone?

By | March 7, 2011

Gear joints are one of the real specialities of Box2D. By creating gear joints between two bodies you can actually simulate a real world gear. For creating a gear joint you need two bodies connected by a revolute or prismatic joint. The below code creates two bodies in the shape of a circle which is… Read More »

Read a plist from your resources folder in Iphone Objective C

By | March 6, 2011

Plist or property list are usually used for storing data in iPhone. They are normal XML files, you can open it in a texteditor to view it’s data. This sample code shows how to read data from the plist. Reading a plist will return an array.You can print out the array to view the results.… Read More »

Remove unwanted memory from iPhone….

By | March 5, 2011

Hi all …… You know iPhone doesnot have garbage collection like ANDROID. So it becomes the responsibility of the developer or programmer to release the resources and remove the unwanted textures from your memory. If you don’t remove the unused textures and other variables from your memory your application will exit after a while. You… Read More »

Collision Detection In Box2D – Using With Cocos2D

By | March 5, 2011

For an interactive game building using Box2D, collision detection of Box2D bodies is necessary. There is an easy way to implement collision detection in Box2D. For checking whether two bodies have collided with each other in Box2D while using it with cocos2D, then use the following lines of code where ever you want to check… Read More »

Removing Unused Textures in Cocos2D

By | March 5, 2011

Sometimes unused textures may cause you memory leak! So you need to remove the unused textures. For removing unused textures, use the following line of code, where ever you want to remove unused textures. [[TextureMgr sharedTextureMgr] removeUnusedTextures]; 🙂

Removing Forces From Your World in Box2D Cocos2D

By | March 5, 2011

Hi, In certain situations you may need to remove all the forces you have applied on different bodies in your box2D world. Then what to do? Removing all the forces from the bodies in the box2D world is simple. Just use the single line of code. yourWorld->ClearForces(); Here yourWorld is your Box2D world. Simple, isn’t… Read More »

How to load a URL in Objective C or Cocoa in iPhone?/ Open Safari in Objective C

By | March 1, 2011

This code snippet helps you to open a webpage in Safari. Code uses openURL to load the webpage in the web browser. Write this function as an action to a button……… – ( IBAction ) loadWebPageInSafari : ( id ) sender { NSURL *my_URL = [NSURL URLWithString:[Your_URL stringValue]]; if ( [ [NSWorkspace sharedWorkspace] openURL :… Read More »

How to remove unwanted frames from memory in iPhone?

By | February 13, 2011

In Cocos2D in iPhone often you may be using spritesheets which amy cause memory leaks because they are unused textures in memory even when you go from one scene to other. So make sure to remove all those unwanted textures from memory to save memory. Copy this following code to dealloc function in your class… Read More »

CCMenu Animation in Cocos2d iphone

By | February 8, 2011

You cal also animate CCMenu like other sprites to have some effect on your Menu. You can also place spritesheets in each MenuItem. -(void) setUpMenu { CCSprite *Home1 = [CCSprite spriteWithFile:@”Home1.png”]; CCSprite *Home2 = [CCSprite spriteWithFile:@”Home1.png”]; Home2.opacity = 100; CCSprite *Levels1 = [CCSprite spriteWithFile:@”levels2.png”]; CCSprite *Levels2 = [CCSprite spriteWithFile:@”levels2.png”]; Levels2.opacity = 100; CCSprite *Refresh1 =… Read More »

How to add infinite backgrounds in Cocos2D, iphone?

By | December 24, 2010

You often may need scrolling backgrounds in your game or any other application. The following code helps you to add continuous backgrounds. Each sprite is added to the right of the current sprite which make them continuos. //Add more stripes, flip them, and position them next to their neighbour stripe. for (int i = 0;… Read More »

How to dynamically change image in a sprite in cocos2D iphone?

By | December 24, 2010

//Dynamically change the sprite image…… //my_sprite is a CCSprite already initialized with spriteWithFile. [[CCTextureCache sharedTextureCache] removeTexture:[self.my_sprite texture]; CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addImage:@”new_image.jpg”]; [my_sprite setTexture:tex];