Skip to content

Commit

Permalink
Merge pull request #1362 from xmkevinchen/develop-v3.5
Browse files Browse the repository at this point in the history
Fixed contentScale logic while parsing the tile map file
  • Loading branch information
s1ddok committed Mar 6, 2016
2 parents daeb9fb + 9826a0c commit 1225ab0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
27 changes: 15 additions & 12 deletions cocos2d-ext/TileMaps/CCTMXXMLParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,11 @@ -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName name
tileset.firstGid = _currentFirstGID;
_currentFirstGID = 0;
}
tileset.spacing = [[attributeDict objectForKey:@"spacing"] intValue]/_contentScale;
tileset.margin = [[attributeDict objectForKey:@"margin"] intValue]/_contentScale;
tileset.spacing = [[attributeDict objectForKey:@"spacing"] intValue] / _contentScale;
tileset.margin = [[attributeDict objectForKey:@"margin"] intValue] / _contentScale;
CGSize s;
s.width = [[attributeDict objectForKey:@"tilewidth"] intValue]/_contentScale;
s.height = [[attributeDict objectForKey:@"tileheight"] intValue]/_contentScale;
s.width = [[attributeDict objectForKey:@"tilewidth"] intValue] / _contentScale;
s.height = [[attributeDict objectForKey:@"tileheight"] intValue] / _contentScale;
tileset.tileSize = s;
tileset.tileOffset = CGPointZero; //default offset (0,0)
tileset.contentScale = _contentScale;
Expand Down Expand Up @@ -303,6 +303,8 @@ -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName name
CGSize s;
s.width = [[attributeDict objectForKey:@"width"] intValue];
s.height = [[attributeDict objectForKey:@"height"] intValue];

// The layer size is the row * column
layer.layerSize = s;

layer.visible = ![[attributeDict objectForKey:@"visible"] isEqualToString:@"0"];
Expand All @@ -313,9 +315,10 @@ -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName name
layer.opacity = 1.0;
}

int x = [[attributeDict objectForKey:@"x"] intValue];
int y = [[attributeDict objectForKey:@"y"] intValue];
layer.offset = ccp(x,y);
CGPoint offset;
offset.x = [[attributeDict objectForKey:@"offsetx"] floatValue];
offset.y = [[attributeDict objectForKey:@"offsety"] floatValue];
layer.offset = ccpMult(offset , 1 / _contentScale);

[_layers addObject:layer];

Expand All @@ -327,9 +330,9 @@ -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName name
CCTiledMapObjectGroup *objectGroup = [[CCTiledMapObjectGroup alloc] init];
objectGroup.groupName = [attributeDict objectForKey:@"name"];
CGPoint positionOffset;
positionOffset.x = [[attributeDict objectForKey:@"x"] intValue] * _tileSize.width;
positionOffset.y = [[attributeDict objectForKey:@"y"] intValue] * _tileSize.height;
objectGroup.positionOffset = positionOffset;
positionOffset.x = [[attributeDict objectForKey:@"offsetx"] floatValue];
positionOffset.y = [[attributeDict objectForKey:@"offsety"] floatValue];
objectGroup.positionOffset = ccpMult(positionOffset, 1 / _contentScale);

[_objectGroups addObject:objectGroup];

Expand Down Expand Up @@ -386,14 +389,14 @@ -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName name
// X
NSString *value = [attributeDict objectForKey:@"x"];
if( value ) {
int x = [value intValue] + objectGroup.positionOffset.x;
int x = [value intValue] / _contentScale + objectGroup.positionOffset.x;
[dict setObject:[NSNumber numberWithInt:x] forKey:@"x"];
}

// Y
value = [attributeDict objectForKey:@"y"];
if( value ) {
int y = [value intValue] + objectGroup.positionOffset.y;
int y = [value intValue] / _contentScale + objectGroup.positionOffset.y;

// Correct y position. (Tiled uses Flipped, cocos2d uses Standard)
y = (_mapSize.height * _tileSize.height) - y - [[attributeDict objectForKey:@"height"] intValue];
Expand Down
7 changes: 3 additions & 4 deletions cocos2d-ext/TileMaps/CCTiledMapLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,13 @@ -(id) initWithTilesetInfo:(CCTiledMapTilesetInfo*)tilesetInfo layerInfo:(CCTiled
_tileWidth = mapInfo.tileSize.width;
_tileHeight = mapInfo.tileSize.height;
_layerOrientation = mapInfo.orientation;

CGFloat pixelsToPoints = tex ? 1.0/tex.contentScale : 1.0;


// offset (after layer orientation is set);
CGPoint offset = [self calculateLayerOffset:layerInfo.offset];
[self setPosition:ccpMult(offset, pixelsToPoints)];
[self setPosition:offset];

[self setContentSize:CGSizeMake( _mapColumns * _tileWidth * pixelsToPoints, _mapRows * _tileHeight * pixelsToPoints )];
[self setContentSize:CGSizeMake( _mapColumns * _tileWidth, _mapRows * _tileHeight)];

_useAutomaticVertexZ= NO;
_vertexZvalue = 0;
Expand Down

0 comments on commit 1225ab0

Please sign in to comment.