Skip to content

Commit b61f989

Browse files
committed
Fixed contentScale logic while parsing the tile map file
1 parent 7a41874 commit b61f989

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

cocos2d-ext/TileMaps/CCTMXXMLParser.m

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,11 @@ -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName name
268268
tileset.firstGid = _currentFirstGID;
269269
_currentFirstGID = 0;
270270
}
271-
tileset.spacing = [[attributeDict objectForKey:@"spacing"] intValue]/_contentScale;
272-
tileset.margin = [[attributeDict objectForKey:@"margin"] intValue]/_contentScale;
271+
tileset.spacing = [[attributeDict objectForKey:@"spacing"] intValue] / _contentScale;
272+
tileset.margin = [[attributeDict objectForKey:@"margin"] intValue] / _contentScale;
273273
CGSize s;
274-
s.width = [[attributeDict objectForKey:@"tilewidth"] intValue]/_contentScale;
275-
s.height = [[attributeDict objectForKey:@"tileheight"] intValue]/_contentScale;
274+
s.width = [[attributeDict objectForKey:@"tilewidth"] intValue] / _contentScale;
275+
s.height = [[attributeDict objectForKey:@"tileheight"] intValue] / _contentScale;
276276
tileset.tileSize = s;
277277
tileset.tileOffset = CGPointZero; //default offset (0,0)
278278
tileset.contentScale = _contentScale;
@@ -303,6 +303,8 @@ -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName name
303303
CGSize s;
304304
s.width = [[attributeDict objectForKey:@"width"] intValue];
305305
s.height = [[attributeDict objectForKey:@"height"] intValue];
306+
307+
// The layer size is the row * column
306308
layer.layerSize = s;
307309

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

316-
int x = [[attributeDict objectForKey:@"x"] intValue];
317-
int y = [[attributeDict objectForKey:@"y"] intValue];
318-
layer.offset = ccp(x,y);
318+
CGPoint offset;
319+
offset.x = [[attributeDict objectForKey:@"offsetx"] floatValue];
320+
offset.y = [[attributeDict objectForKey:@"offsety"] floatValue];
321+
layer.offset = ccpMult(offset , 1 / _contentScale);
319322

320323
[_layers addObject:layer];
321324

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

334337
[_objectGroups addObject:objectGroup];
335338

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

393396
// Y
394397
value = [attributeDict objectForKey:@"y"];
395398
if( value ) {
396-
int y = [value intValue] + objectGroup.positionOffset.y;
399+
int y = [value intValue] / _contentScale + objectGroup.positionOffset.y;
397400

398401
// Correct y position. (Tiled uses Flipped, cocos2d uses Standard)
399402
y = (_mapSize.height * _tileSize.height) - y - [[attributeDict objectForKey:@"height"] intValue];

cocos2d-ext/TileMaps/CCTiledMapLayer.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,13 @@ -(id) initWithTilesetInfo:(CCTiledMapTilesetInfo*)tilesetInfo layerInfo:(CCTiled
9797
_tileWidth = mapInfo.tileSize.width;
9898
_tileHeight = mapInfo.tileSize.height;
9999
_layerOrientation = mapInfo.orientation;
100-
101-
CGFloat pixelsToPoints = tex ? 1.0/tex.contentScale : 1.0;
100+
102101

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

107-
[self setContentSize:CGSizeMake( _mapColumns * _tileWidth * pixelsToPoints, _mapRows * _tileHeight * pixelsToPoints )];
106+
[self setContentSize:CGSizeMake( _mapColumns * _tileWidth, _mapRows * _tileHeight)];
108107

109108
_useAutomaticVertexZ= NO;
110109
_vertexZvalue = 0;

0 commit comments

Comments
 (0)