2011-08-27 58 views
0

喜上我的瓷砖地图瓷砖地图编辑器作出这样一个对象层:Objective-C的iPhone游戏编程:检测瓦片地图对象层上触摸

http://i.stack.imgur.com/f5fZK.png

大广场代表我命名对象层:ObjectIt。

我的问题是当我触摸模拟器中的方块,它不符合正确的地方。有些人告诉我浮动价值或开放的问题,但我是noob我需要明确的答案。

这是我的代码:

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    NSLog(@"touch detect"); 
    CCNode* node = [self getChildByTag:TileMapNode]; 
    NSAssert([node isKindOfClass:[CCTMXTiledMap class]], @"not a CCTMXTiledMap"); 
    CCTMXTiledMap* tileMap = (CCTMXTiledMap*)node; 
    // Get the position in tile coordinates from the touch location 
    CGPoint touchLocation = [self locationFromTouches:touches]; 
    CGPoint tilePos = [self tilePosFromLocation:touchLocation tileMap:self.tileMap]; 
    // Check if the touch was on water (e.g., tiles with isWater property) 
    bool isTouchOnWater = NO; 


    CCTMXObjectGroup* objectLayer = [tileMap objectGroupNamed:@"ObjectIt"]; 
    bool isTouchInRectangle = NO; 
    int numObjects = [objectLayer.objects count]; 
    for (int i = 0; i < numObjects; i++) 
    { 

     NSDictionary* properties = [objectLayer.objects objectAtIndex:i]; 
     CGRect rect = [self getRectFromObjectProperties:properties tileMap:tileMap]; 
     if (CGRectContainsPoint(rect, touchLocation)) 
     { 
      isTouchInRectangle = YES; 
      break; } 
    } 


    if (isTouchInRectangle) 
    { 

     NSLog(@"TOUCH IN RECTANGLE"); 
     // get the ALGORITH FOR TOWERS 
    } 

} 
- 

(CGPoint) locationFromTouch:(UITouch*)touch 
{ 
    CGPoint touchLocation = [touch locationInView: [touch view]]; 
    return [[CCDirector sharedDirector] convertToGL:touchLocation]; 
} 

-(CGPoint) locationFromTouches:(NSSet*)touches 
{ 
    return [self locationFromTouch:[touches anyObject]]; 
} 

-(CGRect) getRectFromObjectProperties:(NSDictionary*)dict 
           tileMap:(CCTMXTiledMap*)tileMap 
{ 
    float x, y, width, height; 
    x = [[dict valueForKey:@"x"] floatValue] + tileMap.position.x; 
    y = [[dict valueForKey:@"y"] floatValue] + tileMap.position.y; 
    width = [[dict valueForKey:@"width"] floatValue]; 
    height = [[dict valueForKey:@"height"] floatValue]; 
    return CGRectMake(x, y, width, height); 
} 

那代码,我给你更多,如果你需要的。

另外:当我在iPhone上运行我有垂直的黑线...

回答

1

为了获得在cocos2d点正确的触摸位置使用CCNode便捷方法:

- (CGPoint)convertTouchToNodeSpace:(UITouch *)touch 
{ 
    CGPoint point = [touch locationInView: [touch view]]; 
    point = [[CCDirector sharedDirector] convertToGL: point]; 
    return [self convertToNodeSpace:point]; 
} 
+0

确定我更换由locationfromtouch这种方法吗? – hugo

+0

ITS WORKING THX MAN但我仍然在我的iPhone上移动屏幕时出现垂直黑线 – hugo

0

我不能确定它是否被添加到API,因为phix23公布他们的答案,但convertTouchToNodeSpace:方法基本上做同样的事情:

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    CCTMXTiledMap* tileMap = (CCTMXTiledMap*)[self getChildByTag:TileMapNode]; 

    CGPoint p = [tileMap convertTouchToNodeSpace:touch]; 

    // If you wanted to figure out which tile in a layer they touched (assuming 
    // it's Ortho) you could do something like this: 
    CCTMXLayer *layer = [tileMap layerNamed:@"Tile Layer 1"]; 
    CGPoint q = { 
     (int) (p.x/tileMap.tileSize.width), 
     // World origin is bottom left but map coordinates are top left so flip 
     // the y. 
     layer.layerSize.height - (int) (p.y/tileMap.tileSize.height) - 1 
    }; 
    CCSprite *s = [layer tileAt:q]; 
    NSLog(@"Touched %@", s); 

    return YES; 
}