Showing posts with label Objective-C. Show all posts
Showing posts with label Objective-C. Show all posts

Tuesday, January 24, 2012

Adding GameFeed to your OpenFeint Enabled Cocos2d App

In order for us to add the new GameFeed feature of OpenFeint to our Word Crank app on iOS we needed to upgrade to the latest version of OpenFeint, version 2.12.5.  After a few issues with linking errors we were finally running with the latest and greatest that OpenFeint has to offer. Complete instructions for upgrading are included here.

Next we wanted to include the GameFeed component, but only on the home screen.  We used a similar approach to how we included AdMob in the free version.  Some code snippets are included below to help others.

 

[sourcecode language="objc"]
-(id) init
{
if ( (self=[super init]) ) {

CGSize s = [[CCDirector sharedDirector] winSize];

controller = [[RootViewController alloc] init];
controller.view.frame = CGRectMake(0,0,s.width,s.height);

gameFeed = [OFGameFeedView gameFeedView];

[controller.view addSubview:gameFeed];
[[[CCDirector sharedDirector] openGLView]addSubview : controller.view];
}

return self;
}

- (void) dealloc
{
[controller.view removeFromSuperview];
[controller release];

[super dealloc];
}
[/sourcecode]

Do you know a different way to do it? Leave a comment below.

Sunday, September 11, 2011

SOLUTION: Apple Mach-O Linker Error When Compiling iPhone App

Recently encountered the following error when compiling an iPhone Application, "Apple Mach-O Linker Error".  After some digging I realized that I had included the same file twice in the application.  After removing the duplicate file the error went away.  Hope this helps someone else that has a similar error.

Read more about iPhone Development

Download Word Crank on Android for FREE!

Monday, August 29, 2011

Tips: Programming for Android and iOS

As I continue my foray into game development on the iOS and Android platforms I wanted to take a few moments to share some tips that I have found to be very useful in creating an application that can will eventually run on both platforms.

Tips


General



  1. Separate the game logic from the user interface layer.  This will make it easier to port the code over to the accompanying platform.  For example, if you put the main game loop logic in a separate Java class, then you basically can just do a line by line conversion to Objective-C when you port it to iOS.

  2. Place all Strings in one location.  Have a Constants.java where you put all of your static String objects, then when you port to iOS you just use the same thing, except now it is Constants.h.

  3. If you are going to use Texture Atlases in your game, create them using a tool such as Texture Packer and then you will have the exact location of each Sprite stored in a file that can be easily referenced for both versions of the game.


iOS


Use a framework! Working with Cocos2d made the transition relatively smooth as we converted Word Crank from its Android version to iOS.

Android


Get a good book, I read Beginning Android Games, it helped lay a good foundation for working with Android and understanding the various states of a running application and the GLSurfaceView that serves as the main background for all OpenGL drawings.

Any questions? Just leave a comment. Thanks.

Tuesday, August 23, 2011

Adding Touch Events to a Custom Cocos2d Sprite

In creating our game Word Crank, we decided that we would make the clickable block objects handle the touch events themselves instead of using the CCScene object. In order to do that we had to implement the CCTargetedTouchDelegate.  A sample of the code can be found below:

Inside the Header File:


[sourcecode language="objc"]
@interface CustomSprite : CCSprite <CCTargetedTouchDelegate>
[/sourcecode]

Inside the Implementation File:


[sourcecode language="objc"]

- (void) onEnterTransitionDidFinish
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:YES];
}

- (void) onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
}
[/sourcecode]

Sunday, August 14, 2011

Adding Pause/Play Buttons to your iPhone Game using Cocos2d

In developing the iPhone version of Word Crank it took me a while to figure out how to add the pause/play buttons using Cocos2d.  One post that I found suggested using the CCMenuItemToggle class of Cocos2d.  That is exactly what I used and after some playing around with the code I finally was able to get the the pause/play buttons to function properly.

I'm sharing the code below to assist others from going through the endless search that I made.

Source Code


[sourcecode language="objc"]
CCSprite *pauseButton = [CCSprite spriteWithSpriteFrameName:@"pause_button.png"];
CCSprite *pauseButton2 = [CCSprite spriteWithSpriteFrameName:@"pause_button.png"];

CCSprite *playButton = [CCSprite spriteWithSpriteFrameName:@"play_button.png"];
CCSprite *playButton2 = [CCSprite spriteWithSpriteFrameName:@"play_button.png"];

pauseBtn = [[CCMenuItemImage itemFromNormalSprite:pauseButton
selectedSprite:pauseButton2
target:nil
selector:nil] retain];

playBtn = [[CCMenuItemImage itemFromNormalSprite:playButton
selectedSprite:playButton2
target:nil
selector:nil] retain];

CCMenuItemToggle *toggleItem = [CCMenuItemToggle itemWithTarget:self
selector:@selector(pausePlayButtonTapped:)
items:pauseBtn, playBtn, nil];

// Create a menu and add your menu items to it
CCMenu *pausePlayMenu = [CCMenu menuWithItems:toggleItem, nil];
pausePlayMenu.position = ccp(300, 56);
[self addChild:pausePlayMenu z:10];
[/sourcecode]

Source Code Notes


One thing to notice is that I had to create 2 Sprites for each of the pause and play buttons because Cocos2d doesn't let you add the same Sprite twice to create the MenuItem object.

Download Word Crank on Android for FREE!