Category Archives: iPhone

Splitting a string using symbols in Objective C

By | March 20, 2011

Hi, If you want to split a string between a symbol like, ‘.’ or ‘-‘ or ‘,’ in Objective C, what to do? See the code for how to split string in Objective C. NSString *myString = @”song.mp3”; NSArray *splitArray = [myString componentsSeparatedByString:@”.”]; This code will split the string ‘myString’ into ‘song’ & ‘mp3’ (separated… Read More »

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 »

Adding Image to a UIImageView using Image URL

By | March 14, 2011

Hi, Sometimes you need to add Image to a UIImageView from the URL of the image you got. In that case, this is how you can add image to the UIImageView. NSString *picURL= @”http://photobucket.com/yourImage.gif”; NSData *picData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mapURL]]; UIImage* picture = [[UIImage alloc] initWithData:picData]; [pictureView setImage:picture]; [picData release]; [picture release]; Here, pictureView is… 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 »

Playing sound on the Mac Desktop in Objective C.

By | March 11, 2011

There are different ways for playing sound on the Mac in Objective C. Let’s look at some of the methods.. 1. Play by filename. NSSound *mySound = [NSSound soundNamed:@”sound”]; if you have no file named “sound” in your application’s main bundle, then it will return nil. 2. Play by pathName sound = [[NSSound alloc] initWithContentsOfFile:@”/Volumes/Audio/sound.aiff”… 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 »

AlertView with textbox in iPhone, Objective C.

By | March 9, 2011

Often we see alerts with text message or title in it. But actually we can customize alerts. We can place any views inside it. We can place a textView(TextField) , imageView etc in it. Take a look at this simple example that places a textbox inside an alertView. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Title goes… 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 detect shake Gesture in your iPhone Cocos2D?

By | February 26, 2011

For detecting shake in a cocos2D program copy these lines to your layer class bool shaked_once; //default false self.isAccelerometerEnabled = YES; [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1/60]; shaked_once = false; Then copy this function to the same file…. and you are done…….. -(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { float THRESHOLD = 2; if (acceleration.x > THRESHOLD || acceleration.x… Read More »