/ Published in: iPhone
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//1) Load the image: UIImage *collisionImage = [UIImage imageNamed:@"levelmask.png"]; //2) Get the image size: int width = (int)collisionImage.size.width; int height = (int)collisionImage.size.height; //3) Allocate our array for quick lookups and clear it: height ); //4) Get access to the raw bits of the image CFDataRef imageData = CGDataProviderCopyData( CGImageGetDataProvider ( collisionImage.CGImage ) ); const UInt32 *pixels = (const UInt32*)CFDataGetBytePtr( imageData ); //5) Build our collision map: for( j = 0; j < (width * height); j++ ) if ( pixels[j] & 0xff000000 ) collisionMap[j] |= 1; //6) Release what we no longer need: CFRelease( imageData ); //Now, whenever you need to see if you hit something, you call a routine //that does: -(int)collisionForPoint:(CGPoint)point { int x = (int)point.x; int y = (int)point.y; // TODO: check for boundaries here return collisionMap[(y*width)+x]; }