Category Archives: Objective-C

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

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 »

Working with files in Objective C

By | December 22, 2010

Basic file operations Assumes the existence of a file called “testfile” in the current working directory #import #import #import #import #import int main (int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString *fName = @”testfile”; NSFileManager *fm; NSDictionary *attr; // Need to create an instance of the file manager fm =… Read More »

How to use picker in iphone, A simple example.

By | December 20, 2010

#import “ViewController.h” @implementation ViewController @synthesize mlabel; – (void)viewDidLoad { [super viewDidLoad]; arrayNo = [[NSMutableArray alloc] init]; [arrayNo addObject:@” Hello “]; [arrayNo addObject:@” Hai “]; [arrayNo addObject:@” Picker “]; [arrayNo addObject:@” Simple “]; [arrayNo addObject:@” example “]; [pickerView selectRow:1 inComponent:0 animated:NO]; mlabel.text= [arrayNo objectAtIndex:[pickerView selectedRowInComponent:0]]; } – (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; { return 1; } – (void)pickerView:(UIPickerView *)pickerView… Read More »