Você está na página 1de 5

/*How to detect the transparent area on a sprite and avoid it from touch: cocos2d */ //First map the desired

sprite and find the x and y coordinates by using Adobe illustrator or some other suitable software: //You will get something like: 8.0000, 186.0000 83.1167, 187.4476 144.6842, 160.1118 169.0000, 109.0000 182.1755, 81.3052 186.7299, 35.7403 161.0000, 16.0000 106.1747, -26.0627 58.7547, 40.1885 47.0000, 79.0000 44.0003, 97.3315 40.9997, 115.6685 38.0000, 134.0000 29.6651, 154.2978 12.0629, 166.0801 2.0000, 184.0000 2.6666, 184.0000 3.3334, 184.0000 4.0000, 184.0000 5.3332, 184.6666 6.6668, 185.3334 8.0000, 186.0000 //Here, in each line, first value indicates the x - coordinate and the second value indicates the y - coordinate. You can save them to a text file and read them with your code (if the image shape is complicated), or you can directly write them on to your code (for simple shaped images). //First I will introduce the hard code method for simple shaped image: in .h: @interface HelloWorldLayer : CCLayer { CCSprite *mySprite; CGMutablePathRef mCollisionPath; } +(CCScene *) scene; -(bool) isPointInsideTheMap:(CGPoint)inPoint; @end in .m: -(id) init { if( (self=[super init])) { self.isTouchEnabled = YES; // Enable Touch //

//In your init method, create the sprite as below: CCSprite *mySprite = [CCSprite spriteWithFile:@"blue.png"]; mySprite.position = ccp(160, 240); [self addChild:mySprite z:1]; //Then create the path as below: mCollisionPath = CGPathCreateMutable(); CGPathMoveToPoint(mCollisionPath, NULL, 8.000000, 186.000000); CGPathAddLineToPoint(mCollisionPath, NULL, 83.116699, 187.447601); CGPathAddLineToPoint(mCollisionPath, NULL, 144.684204, 160.111801); CGPathAddLineToPoint(mCollisionPath, NULL, 169.000000, 109.000000); CGPathAddLineToPoint(mCollisionPath, NULL, 182.175507, 81.305199); CGPathAddLineToPoint(mCollisionPath, NULL, 186.729904, 35.740299); CGPathAddLineToPoint(mCollisionPath, NULL, 161.000000, 16.000000); CGPathAddLineToPoint(mCollisionPath, NULL, 106.174698, -26.062700); CGPathAddLineToPoint(mCollisionPath, NULL, 58.754700, 40.188499); CGPathAddLineToPoint(mCollisionPath, NULL, 47.000000, 79.000000); CGPathAddLineToPoint(mCollisionPath, NULL, 44.000301, 97.331497); CGPathAddLineToPoint(mCollisionPath, NULL, 40.999699, 115.668503); CGPathAddLineToPoint(mCollisionPath, NULL, 38.000000, 134.000000); CGPathAddLineToPoint(mCollisionPath, NULL, 29.665100, 154.297806); CGPathAddLineToPoint(mCollisionPath, NULL, 12.062900, 166.080093); CGPathAddLineToPoint(mCollisionPath, NULL, 2.000000, 184.000000); CGPathAddLineToPoint(mCollisionPath, NULL, 2.666600, 184.000000); CGPathAddLineToPoint(mCollisionPath, NULL, 3.333400, 184.000000); CGPathAddLineToPoint(mCollisionPath, NULL, 4.000000, 184.000000); CGPathAddLineToPoint(mCollisionPath, NULL, 5.333200, 184.666595); CGPathAddLineToPoint(mCollisionPath, NULL, 6.666800, 185.333405); CGPathAddLineToPoint(mCollisionPath, NULL, 8.000000, 186.000000); CGPathCloseSubpath(mCollisionPath); } return self; } //Create the check function as: -(bool) isPointInsideTheMap:(CGPoint)inPoint { if (CGPathContainsPoint(mCollisionPath, NULL, inPoint, NO) ) { // Check whether the touched point is inside the sprite rigid area// // If yes, return true // return true; } // Else, return false // return false; } //Then, in touches began method; do as follows: - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:[touch view]]; location = [[CCDirector sharedDirector] convertToGL:location]; CGPoint loc =[mySprite convertToNodeSpace:location]; if ([self isPointInsideMap:loc]) { // If touch is inside the visible sprite area NSLog(@"You are clicked inside the sprite ..."); }else{ // If touch is not inside the visible sprite area NSLog(@"Sorry, your click is anywhere outside the sprite's visible area ..."); } }

// While trying the above methode, you may have thought that it is too difficult for more number of cordinates or for complex sprites. So, I'll show you the systematic methd of reading the co-ordinates from file, creating the path and checking the sprite area. For that, first save the generated co-ordinates in a text file( say blue.txt ), save it in the bundle. Then: in .h: @interface HelloWorldLayer : CCLayer { CCSprite *mySprite; CGMutablePathRef mCollisionPath; int path_Count; } +(CCScene *) scene; -(void)ReadFile; -(void)createArrayTableArrayElements:(NSString *)readData; -(void)createPath:(float )X_:(float )Y_; @end in .m: -(id) init { if( (self=[super init])) { self.isTouchEnabled = YES; // Enable Touch // //In your init method, create the sprite as below: CCSprite *mySprite = [CCSprite spriteWithFile:@"blue.png"]; mySprite.position = ccp(160, 240); [self addChild:mySprite z:1];

// Call method to read values from file [self ReadFile]

-(void)ReadFile{ NSString *filePath = [[NSBundle mainBundle] pathForResource:@"blue" ofType:@"txt"]; if (filePath) { // Fetch the content from file NSString *contentOfFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; // call the method to split the content of file and store it in arrays [self createArrayTableArrayElements:contentOfFile]; } } -(void)createArrayTableArrayElements:(NSString *)readData{ NSArray *lineSplitedArray = [[NSMutableArray alloc]init]; lineSplitedArray = [readData componentsSeparatedByString:@"\n"]; NSMutableArray *X_arr = [[NSMutableArray alloc]init]; // X coordinate array NSMutableArray *Y_arr = [[NSMutableArray alloc]init]; // Y coordinate array for (int i=0; i<lineSplitedArray.count; i++) { // Split and add X coordinates to X_arr int comaIndex = [[lineSplitedArray objectAtIndex:i] rangeOfString:@"," options:NSBackwardsSearch].location; [X_arr insertObject:[[NSString stringWithFormat:@"%@",[[lineSplitedArray objectAtIndex:i] substringWithRange:NSMakeRange(0,comaIndex)]] stringByReplacingOccurrencesOfString:@" " withString:@""] atIndex:i]; // Split and add Y coordinates to Y_arr int stringLength = [[lineSplitedArray objectAtIndex:i] length]; [Y_arr insertObject:[[NSString stringWithFormat:@"%@",[[lineSplitedArray objectAtIndex:i] substringWithRange:NSMakeRange(comaIndex+1,stringLengthcomaIndex-1)]] stringByReplacingOccurrencesOfString:@" " withString:@""] atIndex:i]; // Call method to create path from coordinate array values [self createPath:[[X_arr objectAtIndex:i] floatValue] :[[Y_arr objectAtIndex:i] floatValue]]; if (i==lineSplitedArray.count-1) { // Close the path if i = lineSplitedArray.count-1 CGPathCloseSubpath(mCollisionPath); } } } -(void)createPath:(float )X_:(float )Y_{

path_Count++; if (path_Count==1) { // First time, create starting point CGPathMoveToPoint(mCollisionPath, NULL, X_, Y_); }else{ // If not first time, then add line to that point CGPathAddLineToPoint(mCollisionPath, NULL, X_,Y_ ); } }

Você também pode gostar