Category Archives: Box2D

How to detect shake in iPhone ? An example

By | April 13, 2011

Hi, Sometimes you may want to detect shake gesture in iPhone using your programs. But how can you detect shake gesture in iPhone? Shake gesture in iPhone can be detected by using the accelerometer. The following lines of code can be used for detecting the shake gesture in iPhone. You can use these lines of… Read More »

How to remove box2D body from world ?

By | April 10, 2011

Hi, If you are a game developer and you are using box2D for real world simulations, you would probably need a particular body to be removed from the box2D world! But how can you remove box2D body from world ? See the following sample code which does the same. – (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {… Read More »

How to empty an NSMutableArray ?

By | April 6, 2011

Hi, Sometimes you may need to empty an NSMutableArray which is already initialized with some objects. You don’t need to release it and again alloc it for future use! You can simply empty an NSMutableArray using the following line of code. [yourArray removeAllObjects]; Here ‘yourArray’ is your NSMutableArray. 🙂

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 »

How to play music in cocos2D or Box2D, iPhone?

By | March 7, 2011

This simple code plays a music…….. [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@”my_music.mp3″]; For stopping the music use this……. [[SimpleAudioEngine sharedEngine]stopBackgroundMusic]; To pause the music use this line of code….. [[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];

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 »

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 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 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 »

How to Implement Box2D in Adobe AIR?

By | February 4, 2011

Everyone will be fascinated how flash games are built on the web that implements the real world physics. Well for your information there are a lot of physics engines available. One of them is the Box2D. Box2D was written in C++. Then it was converted to flash. So now I am going to show you… Read More »