2012-07-19 38 views
0

對於我的碰撞檢測,我需要檢查球矩形與任何牆體矩形相交。現在我有它的工作,但它的檢查,看看球的位置是在瓷磚的GID之一,使用這個。如何從瓷磚地圖製作矩形

-(void) checkHits:(CGPoint)position { 
    CGPoint tileCoord = [self tileCoordForPosition:position]; 
    int tileGid = [levelLayer tileGIDAt:tileCoord]; 
    //NSLog(@"%g",tileRect.origin); 
    if (tileGid) { 
     NSDictionary *properties = [level propertiesForGID:tileGid]; 
     if (properties) { 
      NSString *collision = [properties valueForKey:@"break"]; 
      if (collision && [collision compare:@"True"] == NSOrderedSame) { 
       //for blocks 
       //[[SimpleAudioEngine sharedEngine] playEffect:@"hit.caf"]; 
       [levelLayer removeTileAt:tileCoord]; 
       velocity.y *= -1; 
      } 
      if (collision && [collision compare:@"False"] == NSOrderedSame) { 
       //for edges 
       //[[SimpleAudioEngine sharedEngine] playEffect:@"hit.caf"]; 
       velocity.y *= -1; 
      }  
     }  
    } 
} 

我需要知道如何改變這檢查,看看球RECT /外接矩形框的任意瓷磚的相交rects/boundingboxex(以及如何讓瓷磚RECT放在第一位/外接矩形框),其財產中斷。 P.S.我正在使用Tiled地圖編輯器。

回答

0

想通這一點,而前:

CCSprite *tile; 
    for (int i = 0; i < level.contentSize.width/level.tileSize.width; i ++) 
     for (int j = 0; j < level.contentSize.height/level.tileSize.height; j ++){ 
      tile = [levelLayer tileAt:ccp(i,j)]; 
      if (CGRectIntersectsRect(ball.boundingBox, tile.boundingBox)) { 
       //Do whatever... 
      } 
     } 
    } 

我建議一個碰撞層,使地圖製作更容易。