Richard
Richard

Reputation: 425

Cocos2d Python - AttributeError: 'Resource' object has no attribute 'set_view'

import cocos
from cocos.tiles import load
from cocos.layer import ScrollingManager
from cocos.director import director
from cocos.scene import Scene

director.init()

MapLayer = load("themap.tmx")

scroller = ScrollingManager()

scroller.add(MapLayer)

director.run(Scene(scroller))

Just started using cocos and trying to figure out Tilemaps. Getting ridiculous errors and would appreciate some help.

Upvotes: 1

Views: 226

Answers (1)

Koraken
Koraken

Reputation: 115

When you load a tmx file in cocos2d-python you get a Resource object, this includes more data about the map than just the layers. And important there also is that a map can have multiple layers.

The ScrollingManager requires a layer object, not a resource object. To get the layer you want to add out of Resource object you can access it like a dictionary, like so:

MapLayer = load("themap.tmx")["The name of the layer"]

Here's a modification of your example with my own test map that works:

import cocos
from cocos.tiles import load
from cocos.layer import ScrollingManager
from cocos.director import director
from cocos.scene import Scene

director.init()

loaded_tmx = load("test.tmx")

MapLayer = loaded_tmx["Tile Layer 1"]

scroller = ScrollingManager()

scroller.add(MapLayer)

director.run(Scene(scroller))

As a forewarning though, the current version of TMX file handling in cocos2d-python does not properly handle the most current version of the TMX file format. I've had to make some modifications to get it to work.

Upvotes: 1

Related Questions