Posts Tagged ‘objective C’


Posted in code on May 11th, 2012 by admin – Be the first to comment

Right so I get this one a lot, especially when I’m coding late at night and tired…which is most of the time. So, my main culprits are objects that I haven’t released in my dealloc method.

Especially look for anything that you created with ALLOC INIT, for example an array

[[NSMutableArray alloc] init]

You must, must, must release it.

Second most common is not removing the class from observing a Notification. So if you have something like

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(classMethod:) name:@"notificationName" object:nil];

Then again in your dealloc method, you must put this

[[NSNotificationCenter defaultCenter] removeObserver:self];

I’m sure there are more things, and I’ll add notes as I discover them.

xcode persistent data

Posted in code on May 8th, 2012 by admin – Be the first to comment

So you want to save some data to the users device for use in your app. The way to do it is thusly,

// declare your app data with whatever name you like
NSUserDefaults *yourAppData = [NSUserDefaults standardUserDefaults];

// retrieve your data with
NSString *myString = [yourAppData objectForKey:@"StringVariableName"];
int myInt = [yourAppData integerForKey:@"IntVariableName"];

// now if this is the first time the app has been run, these won't have any value,
// so it's probably best just to check they aren't null before you try to use them
    // then this is the first time it's been accessed
    // otherwise it has been created and has got a value so do whatever you need to with it here

// now to set data use the following

[yourAppData setObject:@"someStringData" forKey:@"StringVariableName"];
[yourAppData setInteger:2 forKey:@"IntVariableName"];

Load a class from String

Posted in code on May 3rd, 2012 by admin – Be the first to comment

I often need to dynamically load a class from a concatinated combination of strings and integers. So here is how you do it. In a game for example, you may have a LevelID stored in your appdelegate that changes at the game progresses, so I’ve made the example grabbing such a levelID from the delegate class.

YourAppDelegate *theDelegate = (YourAppDelegate*)[[UIApplication sharedApplication] delegate];
int LevelID = [theDelegate getLevelID];
NSString *levelToLoad = [NSString stringWithFormat:@"Level%i",LevelID];
Class newLevel = NSClassFromString(levelToLoad);

BAM, and it’s done.

Change Cocos2D template to portrait

Posted in code on April 17th, 2012 by admin – Be the first to comment

The template that comes with Cocos2D 0.99.5 is set in landscape mode. Sometimes you want portrait though, so I did the following and it seems to work, and reorient without fail.

In GameConfig.m

// REPLACE THIS LINE #define GAME_AUTOROTATION kGameAutorotationNone
#define GAME_AUTOROTATION kGameAutorotationUIViewController

In RootviewController.m in the section of

#elif GAME_AUTOROTATION == kGameAutorotationUIViewController

    // EAGLView will be rotated by the UIViewController
    // Sample: Autorotate only in landscpe mode
    // return YES for the supported orientations
    // REPLACE THIS LINE return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );
        return ( UIInterfaceOrientationIsPortrait( interfaceOrientation ) );

In your appdelegate.m class file

        [director setAnimationInterval:1.0/60];
    [director setDisplayFPS:YES];
    [director setDeviceOrientation:kCCDeviceOrientationPortrait];

Code for multitouch

Posted in code on June 30th, 2011 by admin – Be the first to comment

In my latest app Loopy Tunes, I needed to add multitouch functionality to a keyboard. I use Cocos2D, so if you’re like me, then just do the following.

In your delegate.m class, add this line

[glView setMultipleTouchEnabled:YES];

right after this one.

[director setOpenGLView:glView];

Then in your class.m where you are testing for multitouch, up in your init method, add the following line

self.isTouchEnabled = YES;

And then add your touch method somewhere down under the init method, just update the values of the areaToCheck

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
    NSSet *allTouches = [event allTouches];
    for (UITouch* touch in allTouches) {
        CGPoint location = [touch locationInView:[touch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];
        CGRect areaToCheck = CGRectMake(100,100,100,100);
        if(CGRectContainsPoint(areaToCheck, location)) {
            // whatever you want to happen here


Posted in code on July 15th, 2010 by lucy – Be the first to comment

To add a schedule

[self schedule:@selector(methodName:) interval:0.5];

Method called

- (void)methodName:(ccTime) dt {

To remove one:

[self unschedule:@selector(methodName:)];

Adding Accelerometer Data

Posted in Uncategorized on June 10th, 2010 by lucy – Be the first to comment

So easy.

Add to the init method

self.isAccelerometerEnabled = YES;

Then somewhere in your class, stick the following

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
 NSLog(@"tilting %f", acceleration.z);
    //accelerator x
    //So tilting to the left goes towards -1, tilting to the right goes towards 1;
    //accelerator y
    // tiling forward goes towards -1, tilting backwards goes towards 1;
    //accelerator z
    // flicking it upwards it goes towards and past 2, flicking it downwards it goes towards and past  -2;

NSLog CCLOG Format Specifiers

Posted in Uncategorized on June 10th, 2010 by lucy – Be the first to comment

I can never remember which one to use, so here’s the list

%@     Object
%d, %i signed int
%u     unsigned int
%f     float/double

%x, %X hexadecimal int
%o     octal int
%zu    size_t
%p     pointer
%e     float/double (in scientific notation)
%g     float/double (as %f or %e, depending on value)
%s     C string (bytes)
%S     C string (unichar)
%.*s   Pascal string (requires two arguments, pass pstr[0] as the first, pstr+1 as the second)
%c     character
%C     unichar

%lld   long long
%llu   unsigned long long
%Lf    long double