Accelerometer and cocos2d v2.x

I decided to mess around with the accelerometer a little using cocos2d. I started a new project in Xcode using the basic cocos2d template. I dragged over a few images from Vicki Wenderlich’s web site into my Resources folder and I’m ready to go.

In the AppDelegate.m file change [director_ setDisplayStats:YES]; to [director_ setDisplayStats:NO]; to get rid of the frame rate and all of those stats in the simulator. I also changed the top line from #import "IntroLayer.h" to #import HelloWorldLayer.h then I changed the line [director runWithScene: [IntroLayer scene]]; to [director runWithScene: [HelloWorldLayer scene]];. This makes the app go right to the HelloWorldLayer instead of going to the IntroLayer first.

Ok, in HelloWorldLayer.h change the @interface line to say @interface HelloWorldLayer : CCLayer {. Then add these two lines:
CCSprite *background;
CCSprite *monkey;

Then in the HelloWorldLayer.m delete everything from the init after the line if( (self=[super init]) ) { and add this to setup a background:
background = [CCSprite spriteWithFile:@"bg-jungle.png"];
background.anchorPoint = ccp(0,0);
[self addChild:background];

Build and Run and you should have a background image with no stats and no default stuff (hopefully) that the cocos2d template does for you.

Ok now what? This was all pretty simple so far. First we need to add a line to the init to enable the accelerometer:
self.AccelerometerEnabled = YES;

Ok that was easy. Add the monkey to your project now using this code:
-(void) monkey {
CGSize winSize = [[CCDirector sharedDirector] winSize];
monkey = [CCSprite spriteWithFile:@"monkey_arms_up.png"];
monkey.position = ccp(winSize.width/2, monkey.contentSize.height/2);
monkey.tag = 2;
[self addChild: monkey];
}

Make sure you add [self monkey]; to your init. Build and Run and you should have a monkey at the bottom of the screen in the middle.

Do you see the line above that says monkey.tag = 2;? In the +(CCScene *) scene method above, add this before the return scene;:
layer.tag = 1;

These tags are used later in the accelerometer method. In fact, let’s add that code now!

#define kPlayerSpeed 20
#define kHeroMovementAction 1
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
HelloWorldLayer *layer = (HelloWorldLayer *)[[[CCDirector sharedDirector] runningScene] getChildByTag:1];
CCSprite *player = (CCSprite *)[layer getChildByTag:2];

float destX, destY;
float currentX = player.position.x;
float currentY = player.position.y;
BOOL shouldMove = NO;

if(acceleration.y < -0.25) {
destX = currentX + (acceleration.y * kPlayerSpeed);
destY = currentY;
shouldMove = YES;
} else if (acceleration.y > 0.25) {
destX = currentX + (acceleration.y * kPlayerSpeed);
destY = currentY;
shouldMove = YES;
}

if(shouldMove) {
CGSize wins = [[CCDirector sharedDirector] winSize];
if(destX < 30) {
id move = [CCMoveBy actionWithDuration:3 position:ccp(10,0)];
id action = [CCEaseBackOut actionWithAction:move];
[monkey runAction: action];
} else if (destX > wins.width - 30) {
id move = [CCMoveBy actionWithDuration:3 position:ccp(-10,0)];
id action = [CCEaseBackOut actionWithAction:move];
[monkey runAction: action];
} else {
CCAction *action = [CCMoveTo actionWithDuration:1 position: CGPointMake(destX, destY)];
[action setTag:kHeroMovementAction];
[player runAction:action];
}
} else {
[player stopActionByTag:kHeroMovementAction];
}
}

This code will make the monkey move right or left. Connect your iOS device then build and run to use it. The accelerometer does not work in the simulator. Most of this code was taken from the official cocos2d site where they showed how to use the accelerometer. I added a way to make the monkey bounce off the sides when he reaches the end so he doesn’t go off the screen.

Contact me via twitter at @jon01 if you have any questions! You can download the whole project from Github.

iLearn Math v1.3

An update to my game iLearn Math was just released! It includes a new app icon, a new unlockable theme, and an unlockable custom difficulty! The custom difficulty allows you to select a minimum number and a maximum number for practice a specific range of numbers!

Its a FREE update to everyone who has it already. Everyone else can get the app for $1.99! Download it in the App Store now! iLearn Math

Indie Devs

So I just released a FREE version of my app iLearn Math. Now I need downloads and ratings. I was thinking, there are so many indie devs out there and they are all on twitter, hmmmm.

I recently joined the IDRTG in hopes of improving sales, but that hasnt done a lot for me yet. What if we posted our apps on twitter with a hashtag, then every dev then does the download, rate (5 stars with or without a written review), delete (or DRD). Tag the post as #DRD and check that hashtag whenever you can. Obviously don’t delete the app if its something you would use or even share with others :)

What do you think? Contact me on twitter @jon01. I also made a Facebook group called Indie Devs, UNITE feel free to join and post your app!

I’ll do it if you do it!

UPDATE: It seems the facebook group is doing really well. Please join it and share your app! Indie Devs, UNITE

iLearn Math Lite

Just released tonight! The LITE version of my game, iLearn Math. In this version you can play the basic game but only unlock the beginner difficulty.

The FULL version (99 cents) has 2 more difficulties and themes to unlock too! Give the free version a try and PLEASE rate the game.

Sharing Photos on iOS

I was about to create a shared Photo Stream with my wife’s side of the family and figured I should probably be typing out what I did in case anyone else wants to know how to set it up. Shared Photo Streams is one of the new features in iOS 6, so everyone you invite has to be running it on their device. It’s a great feature that I hope I use more often and I hope apple improves on in future versions. Anyway, here’s how to do it.

Open the Photos app and into your camera roll. Tap edit in the upper right corner and select the pictures you want to add then tap Share on the bottom and tap Photo Stream. Tap New Photo Stream then enter the email addresses of who you want to see the pictures. Name the Photo Stream and select if you want to make it a public website or not then tap Next. Add a comment on the album you just created if you want then tap Post in the upper right. Everyone you invited will get a push notification asking them to join your Photo Stream. If you made a public website, you can go to Photo Stream and hit the blue arrow next to the album you just created. Just tap Share Link and you can email, iMessage, twitter or facebook the link.

It’s as simple as that! Good luck, let me know how it goes.

Posted in iOS

iLearn Math v1.2

There is an update out for iLearn Math. I added a bunch of new features, here they are:

The icon has been updated.

I added 2 new UNLOCKABLE themes, one is a winter and the other is Holiday. They each include a background and 4 different icons.

PARENTS/TEACHERS – Swipe LEFT on the CREDITS menu to reset ALL difficulties and themes to locked again. This also sets the coins to 0, and sets the highest streak to 0 as well.

Added a RANDOM mode which still goes by the difficulty you selected but asks all of the math operations randomly.

Added “Best Streak”.

When you guess the right answer, the icon you selected will make a sound that it would make (except for the turtle).

Added text at the bottom that says you have enough coins to unlock a difficulty or just more then 20 coins.

You can not unlock a difficulty if the previous ones were not unlocked yet. For example, you won’t be able to unlock HARD if EASY was never unlocked.

Let me know what you think!