Code for multitouch

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
}
}
}

Icons required for a universal app

When preparing my universal app for iTunes I was having trouble with getting the icon.png file the right size. When I saved a 57×57 pixel icon iTunes complained about needing a 72×72 pixel icon for the iPad an vice versa for the iPhone. So I googled and found there are a couple of things you need to do.

STEP 1

Create all the icons listed in here under Table 3: Universal apps icon requirements. Then add them to your project in xcode. (you know, drag em into your resources folder)

http://developer.apple.com/library/ios/#qa/qa1686/_index.html

STEP 2

On that same page down the bottom are the instructions for how to enter these to your info.plist. Follow them, or just add this to your info.plist if it isn’t in there already.  (I prefer to edit the info.plist myself in textEdit) If you are using a cocos project, especially 0.99.5 or newer, it should already be in there.


Icon files

Icon.png
Icon@2x.png
Icon-72.png
Icon-Small-50.png
Icon-Small.png
Icon-Small@2x.png

STEP 3

If you have the following in your info.plist,


CFBundleIconFile

replace it with

CFBundleIconFiles

Icon.png
Icon-72.png

Fonts supported by iPad

I had to look this up, so here it is for future reference

American Typewriter, AppleGothic, Arial, Baskerville, Bodoni 72, Bradley Hand, Chalkduster, Cochin, Copperplate, Courier, Courier New Didot, Futura, Geeza Pro, Georgia, Gill Sans, Heiti J, Helvetica, Hoefler Text, Marker Felt, Optima, Palatino, Papyrus, Party LET, Snell Roundhand, Thonburi, Times New Roman, Trebuchet MS, Verdana, Zapf Dingbats, Zapfino.

Code – link from your app to the app store

A good way to get people to review your app easily is to put a link to the review page in your app. Easy to do, just add this


[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/us/app/a-fine-musician/id406573790?mt=8"]];

and replace “a-fine-musician” with your app name, and “406573790” with your app id.

Adding Accelerometer Data

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

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

Detecting touch of a sprite in a sprite sheet

Because I’m going to be doing this a lot, I’m making a note of it.

So at the top of my page.m

enum {
kTagSpriteSheet = 1,
kTagSprite = 2,
};

In my init function

mySheet = [CCSpriteSheet spriteSheetWithFile:@"myatlas.png"];
[self addChild:mySheet z:0 tag:kTagSpriteSheet];

// create the sprite
mySprite = [CCSprite spriteWithTexture:mySheet.texture rect:CGRectMake(0, 0, 200,200)];
[mySheet addChild:mySprite z:0 tag:kTagSprite];

Then in ccTouchesEnded method

BOOL *isSpriteTouched = NO;

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
CCSpriteSheet *mySheet = (CCSpriteSheet*) [self getChildByTag: kTagSpriteSheet];
CCSprite *mySprite = (CCSprite*) [mySheet getChildByTag: kTagSprite];
CGRect myRect = CGRectMake(mySprite.position.x, mySprite.position.y, mySprite.contentSize.width,
mySprite.contentSize.height);

if(CGRectContainsPoint(myRect, location)) {
isSpriteTouched = YES;
}