[英]Python + Pyglet Cocos2D: TypeError: 'class' object is not callable
I've been working on a tiled map renderer, and I've tried to make a seperate Class in another file. 我一直在研究平铺的地图渲染器,并且尝试在另一个文件中创建单独的类。 I get this error message: 我收到此错误消息:
Type Error: 'renderer' object is not callable
Here's the render.py file: 这是render.py文件:
import pyglet, json
from pyglet.window import key
from pyglet.gl import *
from ConfigParser import SafeConfigParser
from cocos.layer import *
from cocos.batch import *
from cocos.sprite import Sprite
class renderer( Layer ):
def __init__(self, mapname):
super( renderer, self ).__init__()
parser = SafeConfigParser()
try:
world = parser.read('maps/'+mapname+'.txt')
print world
except IOError:
print("No world file!")
return
layer = json.loads(parser.get('layer1', 'map'))
tiletype = parser.get('type', 'tile')
print tiletype
tilesize = 64
for x in range(0, len(layer)):
for y in range(0, len(layer[x])):
self.spr = Sprite("image/tiles/"+tiletype+"/"+str(layer[x][y])+".png")
self.spr.position = ((x+1)*tilesize, (y+1)*tilesize)
self.add(self.spr)
And this is the piece of code I call it with: 这是我用以下代码调用的代码:
from other.render import renderer
world = renderer('buildnew')
world()
File Structure: 档案结构:
game/main.py
game/other/render.py
What am I doing wrong? 我究竟做错了什么?
world = renderer('buildnew')
world()
First you make an instance of the renderer class and store it to world. 首先,创建一个renderer类的实例并将其存储到世界。 But then you write world()
, which is wrong cause this object is not callable. 但是随后您编写world()
,这是错误的,因为此对象不可调用。 If you want to make world
a callable object, you should implement the __call__
method in the renderer class. 如果要使world
成为可调用对象,则应在renderer类中实现__call__
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.