Posts Tagged ‘code’

Cocos2d V2 HD graphics labelling convention

Posted in code on June 12th, 2013 by lucy – Be the first to comment

GameBGiPad1.png          - For iPhone  
GameBGiPad1-hd.png       - For iPhone HD
GameBGiPad1-ipad.png     - For iPad  
GameBGiPad1-ipadhd.png   - For iPad HD

Porting Photoshop Blend Modes To Cocos2D

Posted in code on March 1st, 2013 by lucy – Be the first to comment

Want a nice, easy to read and implement description of blend modes in Cocos2D? Well you won’t get it here on Tokeru, but you will on Nial Giacomelli’s site. Check it out. http://nial.me/2012/06/porting-photoshop-blendmodes-to-cocos2d/

Orientation always in Portrait iOS6

Posted in code on October 1st, 2012 by admin – Be the first to comment

Right had to do a bit of research for this one, here is the fix I found that works from Natalie London

In your APP DELEGATE.m

NSString *reqSysVer = @"6.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];

if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending){
    [window setRootViewController:viewController]; // This is for IOS6.....
}else{
    [window addSubview: viewController.view]; //This is for < IOS6....... (the old way)
}

In your ROOTVIEWCONTROLLER.m //will only be compiled for IOS6

FOR LANDSCAPE USE THIS

- (NSUInteger) supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeRight;
}

- (BOOL) shouldAutorotate {
    return YES;
}

FOR PORTRAIT USE THIS

- (NSUInteger) supportedInterfaceOrientations {
    returnĀ  UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

- (BOOL) shouldAutorotate {
    return YES;
}

The page I got this from is

https://devforums.apple.com/message/734618#734618

EXC_BAD_ACCESS

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
if(!myString){
    // then this is the first time it's been accessed
}else{
    // 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.

Cocos2d scene transitions

Posted in code on May 1st, 2012 by admin – Be the first to comment

Easy Peasy, check out this link for how to and al available types.

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
// WITH THE FOLLOWING
#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 ) );
    // WITH THE FOLLOWING
        return ( UIInterfaceOrientationIsPortrait( interfaceOrientation ) );

In your appdelegate.m class file

        [director setAnimationInterval:1.0/60];
    [director setDisplayFPS:YES];
        // ADD THE FOLLOWING LINE HERE
    [director setDeviceOrientation:kCCDeviceOrientationPortrait];

Project Code for Universal App Creation

Posted in code on March 29th, 2012 by admin – Be the first to comment

Step 1

In your Target Info set the Targeted Device Family to “iPhone/iPad”

Step 2

In your appDelegate.m put this bit of code

 - (BOOL) isIpad {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        return YES;
       
    } else {
        return NO;
    }
#else
    return NO;
#endif
}

To reference it in your other classes do this

in class.h

@interface YourLayerClass : CCLayer {
   
    YourAppDelegate *theDelegate;
    Boolean *isIpad;
}

in class.m in your init method

-(id) init
{
    if( (self=[super init] )) {

                //DEFINE iPad or iPhone
        theDelegate = (YourAppDelegate*)[[UIApplication sharedApplication] delegate];
        isIpad = [theDelegate isIpad];

    }
}

And you can use it in your class thusly

    if(isIpad == NO){
    bg = [CCSprite spriteWithFile:@"xx_iphone.png"];
    }else {
    bg = [CCSprite spriteWithFile:@"xx_ipad.png"];
    }

simple cocos2d animation using .png sequence

Posted in code on March 6th, 2012 by admin – Be the first to comment

Right, so I do a lot of animation in my apps, so here is the most simple creation of it

yourfile.h

@private CCSprite *youranimref;
@private CCAnimation *animation;

yourfile.m

-(id) init
{
    if( (self=[super init] )) {
       
        self.isTouchEnabled = YES;
       
        size = [[CCDirector sharedDirector] winSize];
       
        // DECLARE ANIMATION
        animation = [[CCAnimation alloc] initWithName:@"youranimref" delay:1/24.0];
        youranimref = [CCSprite spriteWithFile:@"yourFirstAnimGrfx.png"];
        [animation addFrameWithFilename: @"yourSecondAnimGrfx.png"];
        [animation addFrameWithFilename: @"yourThirdAnimGrfx.png"];
        [animation addFrameWithFilename: @"yourFourthAnimGrfx.png"];

       
        // ADD ANIMATION
        youranimref.position = ccp( 512, 435 );
        [self addChild:youranimref z:5];

    }
    return self;
}


- (void) dealloc
{

// !!!!! VERY IMPORTANT - REMOVE TEXTURES AND RELEASE REFERENCE TO THE ANIMATION

    [[CCTextureCache sharedTextureCache] removeUnusedTextures];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];

    // RELEASE RETAINED OBJECTS
    [animation release];
   
    [super dealloc];
}

- (void) runAnimation
{
   
    // RUN THE ANIMATION
   
    CCAnimate* action = [CCAnimate actionWithAnimation:animation];
    [youranimref runAction:action];

}