Showing posts with label COCOS2D. Show all posts
Showing posts with label COCOS2D. Show all posts

Monday, May 14, 2012

Cocos2d/Gamecenter: How to implement correctly GameCenter into Cocos2d

Hi,

How's it going there?
Here's a tutorial that i saw loads of people having the same problem and after some research  I figured it out.

I don't know if it's the ideal way, but as it worked I can suit you.

Cocos2D: 1.0.1

What was the problem that it wouldn't work?
R: For some reason the RootViewController wasn't referenced and so it gave me a 12h delay on the project.

Step 1:

Go to you AppDelegate.h


#import <UIKit/UIKit.h>

@class RootViewController;

@interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *viewController;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) RootViewController *viewController;

@end



Step 2:

Go to your AppDelegate.m


#import "cocos2d.h"



#import "AppDelegate.h"

#import "RootViewController.h"

@implementation AppDelegate

@synthesize window;
@synthesize viewController;

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.rootViewController = viewController;
//blablablá
// Init the View Controller
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
}

Step 3:

Import GameKit.framework

Step 4:

Implement some code.
I'm going to put only some basic stuff and of course the most important the leaderboard.



-(void)localUserAuthentication{

    

    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    [localPlayer authenticateWithCompletionHandler:^(NSError *error) {
        if (!error){
            isLocalUserAuthenticated = YES;
            //NSLog(@"AUTHETICATION SUCCEDED");
        } else {
            isLocalUserAuthenticated = NO;
            //NSLog(@"AUTHETICATION FAILED");
        }
    }];
}





-(void)showLeaderboard{
    
    
    GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
    if (leaderboardController != NULL) {
        leaderboardController.category = @"com.yourcompany.gamename.leaderboard.whatleaderboard"; 
        leaderboardController.timeScope =GKLeaderboardTimeScopeAllTime;
        leaderboardController.leaderboardDelegate = self; 
        AppDelegate *delegate = [UIApplication sharedApplication].delegate; 
        [delegate.viewController presentModalViewController:leaderboardController animated:YES];
    }
    
    leaderboardController.toolbar.autoresizesSubviews = YES;
    leaderboardController.view.bounds = CGRectMake(0, 0, 480, 320);
    leaderboardController.view.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90));
}



-(void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController{
    CCMenu* gamemenu = (CCMenu*)[self getChildByTag:kGameMenu];
    gamemenu.isTouchEnabled = YES;
    [self show];
    
    AppDelegate *delegate = [UIApplication sharedApplication].delegate; 
    [delegate.viewController dismissModalViewControllerAnimated:YES];
}



Well, so that's that.
If you guys need any help, just get in contact.
Hasta luego.

Friday, January 20, 2012

Cocos2D/Orientation: How to get the orientation you want.


Yo, dudes,

How's it going? Kind of shitty, hun? No Megaupload, no FBI, no WhiteHouse.
What else to do? Well, here goes a fast topic for orientation issues.
Know when you have that real cool game that you're using Cocos2D but want to use Portrait Orientation?
Just do This:


1) Go to "RootViewController.m";
2) Find this piece of code:



#elif GAME_AUTOROTATION == kGameAutorotationUIViewController
//
// EAGLView will be rotated by the UIViewController
//
// Sample: Autorotate only in landscpe mode
//
// return YES for the supported orientations
return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );

3)Change the "UIInterfaceOrientationIsLandscape" to "UIInterfaceOrientationIsPortrait", or any other that you would like. (Just try not using "PortraitUpSideDown" if would be kind of awkward.

Now your code should be something like this:

#elif GAME_AUTOROTATION == kGameAutorotationUIViewController
//
// EAGLView will be rotated by the UIViewController
//
// Sample: Autorotate only in landscpe mode
//
// return YES for the supported orientations
return ( UIInterfaceOrientationIsPortrait( interfaceOrientation ) );


Any problems, get in contact.
Cheers.

Thursday, October 6, 2011

Cocos2D: Press&Hold button

Hey, sports!


Today the time is really really short, so I made an example and uploaded it.
It's no a virus or anything, it's a project that can help you when you need to make action like this:


1)Press button;
2)Wish that it would increase/decrease a "number label";
3)Release button;
4)Action stop right after releasing




                                           here's the link:
                   Project: Button Press&Hold


Hasta la vista, dudes.

Saturday, September 24, 2011

Cocos2D: How to create a "Credits" scene

How's it going, bros!?


Here's another fast a simple post to help you enhance you app and consolidate you name on the credits:




#import "cocos2d.h"
#import "CCLayer.h"
#import "MainMenu.h"

@interface Credits : CCLayer{
    CCMenu* GAME_CREDITS;
}

+(CCScene *)scene;
-(void)runAnimation:(CCNode *)node;
-(id)init;

@end






#import "Credits.h"


@implementation Credits

+(CCScene *)scene{
    
    CCScene *scene = [CCScene node];
    Game *layer = [Credits node];
    [scene addChild:layer];
    return scene;
    
}

-(void)backToMenu:(id)sender{
    [[SoundManager sharedSoundManager] playSound:@"TOUCH"];
    CCScene* _scene = [MainMenu scene];
    [[CCDirector sharedDirector]replaceScene:[CCTransitionMoveInL transitionWithDuration:1.0 scene:_scene]];
    
}

-(void)doIt{
    [self runAnimation:GAME_CREDITS];
}

-(void)removeMe:(CCNode*)node{
    //NSLog(@"ENTROU NO REMOVE ME");
    GAME_CREDITS.position = ccp(CENTER_X, CENTER_Y*(-1)+(-100));
    [self runAnimation:GAME_CREDITS];
}

-(void)runAnimation:(CCNode *)node{
    
    [node runAction: [CCSequence actions: [CCMoveTo actionWithDuration:15.0 position:ccp(CENTER_X,CENTER_Y*3+150)], 
                                          [CCCallFuncN actionWithTarget:self selector:@selector(removeMe:)],nil]];
}

-(void)credits{
    //NSLog(@"ENTROU NOS CREDITS");
    [self runAnimation:GAME_CREDITS];
}

- (id)init
{
    self = [super init];
    if (self) {
        
        CCSprite* background = [CCSprite spriteWithFile:@"SV-BACKGROUND.png"];
        background.position = ccp(CENTER_X, CENTER_Y);
        [self addChild:background z:1];
        
        CCLabelTTF* BACK_N = [CCLabelTTF labelWithString:@"+BACK" fontName:FONT1_1 fontSize:MENU_ITEM_SIZE]; BACK_N.color = ccBLACK;
        CCLabelTTF* BACK_S = [CCLabelTTF labelWithString:@"+BACK" fontName:FONT1_1 fontSize:MENU_ITEM_SIZE]; BACK_S.color = ccGRAY;
        CCMenuItem* iBACK = [CCMenuItemSprite itemFromNormalSprite:BACK_N selectedSprite:BACK_S target:self selector:@selector(backToMenu:)];
        
        CCMenu* back = [CCMenu menuWithItems:iBACK, nil];
        back.position = ccp(CENTER_X-115, CENTER_Y-150);
        [self addChild:back z:3];
        
   
        CCLabelTTF* LABEL_0 = [CCLabelTTF labelWithString:@"0" fontName:FONT0 fontSize:20];
        CCLabelTTF* LABEL_N = [CCLabelTTF labelWithString:@"N" fontName:FONT0 fontSize:20];
        LABEL_0.color = ccBLACK;
        LABEL_N.color = ccBLACK;
        
        CCMenuItemLabel* iLABEL_0 = [CCMenuItemLabel itemWithLabel:LABEL_0];
        CCMenuItemLabel* iLABEL_N = [CCMenuItemLabel itemWithLabel:LABEL_1];
        
        
        GAME_CREDITS = [CCMenu menuWithItems:iLABEL_0, iLABEL_N,  nil];
        
        GAME_CREDITS.position = ccp(CENTER_X, CENTER_Y*(-1));
        [GAME_CREDITS alignItemsVerticallyWithPadding:5];
        [self addChild:GAME_CREDITS z:2];
        
        [self performSelector:@selector(credits) withObject:self afterDelay:1.7];
    }
    
    return self;
}

@end


Monday, September 19, 2011

Cocos2D: How to install it?

Now that i successfully got it installed here's the step-by-step:


1)Get the Cocos2D stuff;
2)Open your TERMINAL;
3)Go to the directory you downloaded the Cocos2D stuff;
4)After that just bring the .sh file (install-templates.sh) in to the code line;
    4.1)if you're not an administrator, you'll neet to put sudo before everything in the line and after you'll need to put the admin password;
5)Open Xcode and be happy.


Good luck (:

Cocos2D: How to remove it?

Just a fast note on how to delete it.
I had to delete it because the installation got kind of crapy, so here it goes:


1) Open TERMINAL
2) Execute these steps:
    2.1) cd/
    2.2) cd Library/
    2.3) cd Application/
    2.4) cd Developer/
    2.5) cd Xcode/
    2.6) cd Templates/
    2.7) ls (just to confirm that the cocos2d is in there)
    2.8) rm -rfd cocos2d-iphone-1.0.1/
     2.9) ls (just to confirm that the work is done)
3)Open Xcode an the the little devils wont be there.




good night!
Tweet