Search code examples
cocos2d-iphonetile

Adding CCSprite on Tiled tile properties


I'm trying to add a Sprite beside the player, but only when the Tile beside the player is NOT a Wall. I know that the Tiled tiles are working properly, as they do their job in this method:

CGPoint p = CGPointMake(tileCoord.x, tileCoord.y - 1);
if ([self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
    [tmp addObject:[NSValue valueWithCGPoint:p]];
    t = YES;

I am checking for Tiled on coordinate with these two methods:

-(BOOL)isProp:(NSString*)prop atTileCoord:(CGPoint)tileCoord forLayer:(CCTMXLayer *)layer {
if (![self isValidTileCoord:tileCoord]) return NO;
int gid = [layer tileGIDAt:tileCoord];
NSDictionary * properties = [_tileMap propertiesForGID:gid];
if (properties == nil) return NO;    
return [properties objectForKey:prop] != nil;
}



-(BOOL)isWallAtTileCoord:(CGPoint)tileCoord {
return [self isProp:@"Wall" atTileCoord:tileCoord forLayer:_bgLayer];
}

(Thanks RayWenderlich)

And my code for adding the sprite is

CGPoint tileCoord =  ccp(_player.position.x - 24 +60, player.position.y);
CGPoint cTileCoord = [self tileCoordForPosition:tileCoord];

NSLog(@" t: %@, c: %@",
    CGPointCreateDictionaryRepresentation(tileCoord),
    CGPointCreateDictionaryRepresentation(cTileCoord)
    );

if (![self isWallAtTileCoord:cTileCoord])
{
    NSLog(@"False");
    circle1.position = ccp(_player.position.x - 24 +60, _player.position.y);
    [self addChild:circle1];

}
else
{
    NSLog(@"True");
}

What I want this to do is to only add the Circle1 sprite to the left of the player when there isn't a Wall tile there. The problem is that this code always detects false, no matter if there is a wall there or not. Do any of you guys understand why it does not detect the walls properly and how I can fix it?


Solution

  • You should check if your tile map has the correct layer settings. I did a mistake with this once, it is easy to forget