Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ v0.6.6.dev - ??

- py 3.3 and py 2.6 support dropped
- collision manager honors promises to accept non-known objects, #300
- fix loading tmx tilesets composed of individual tile elements, #301

v0.6.5 - August 24, 2017

Expand Down
51 changes: 34 additions & 17 deletions cocos/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ def load_tmx(filename):
firstgid = int(tag.attrib['firstgid'])

name = tag.attrib['name']
tileset = None

spacing = int(tag.attrib.get('spacing', 0))
for c in tag.getchildren():
Expand All @@ -325,24 +326,39 @@ def load_tmx(filename):
tileset = TileSet.from_atlas(name, firstgid, path, tile_width,
tile_height, row_padding=spacing,
column_padding=spacing)
# TODO consider adding the individual tiles to the resource?
tilesets.append(tileset)
resource.add_resource(name, tileset)
elif c.tag == 'tile':
# add properties to tiles in the tileset
gid = tileset.firstgid + int(c.attrib['id'])
tile = tileset[gid]
props = c.find('properties')
if props is None:
continue
for p in props.findall('property'):
# store additional properties.
name = p.attrib['name']
value = p.attrib['value']
# TODO consider more type conversions?
if value.isdigit():
value = int(value)
tile.properties[name] = value
try:
# add properties to tiles in the tileset
gid = tileset.firstgid + int(c.attrib['id'])
tile = tileset[gid]
props = c.find('properties')
if props is None:
continue
for p in props.findall('property'):
# store additional properties.
name = p.attrib['name']
value = p.attrib['value']
# TODO consider more type conversions?
if value.isdigit():
value = int(value)
tile.properties[name] = value
except (KeyError, AttributeError):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an exceptionally broad try block, those two errors could come from any number of lines above. What specific key and attribute access errors are being caught here?

# If we couldn't get the tile, then it doesn't coordinate to any existing tile, so it's a new tile
if tileset is None:
tileset = TileSet(name, {})
tileset.firstgid = firstgid
imgpath = resource.find_file(c.find('image').attrib['source'])
id = int(c.attrib['id'])
tile = Tile(tileset.firstgid + id, {}, pyglet.image.load(imgpath))
tile.usertype = c.attrib['type']
tileset[tileset.firstgid + id] = tile

if tileset is not None:
tilesets.append(tileset)
# TODO consider adding the individual tiles to the resource?
resource.add_resource(name, tileset)



# now load all the layers
for layer in map.findall('layer'):
Expand Down Expand Up @@ -407,6 +423,7 @@ def load_tmx(filename):
return resource



#
# XML PROPERTY PARSING
#
Expand Down