2012-11-06 67 views
4

我們使用cocos2d框架來創建遊戲。我們對這個框架完全陌生,所以我們無法讓導演對象像我們所期望的那樣工作。這裏是我們的代碼骨架:Cocos2d:AttributeError:'Director'對象沒有屬性'_window_virtual_width'

from cocos.director import director 
from cocos.layer import base_layers 


import sys 
import math 
import os 

import pyglet 
import cocos 


world_width = 1000 
world_height = 1000 
class NetworkMap(cocos.layer.ScrollableLayer): 
    def __init__(self, world_width, world_height): 
     self.world_width = world_width 
     self.world_height = world_height 
     super(NetworkMap, self).__init__() 
     bg = ColorLayer(170,170,0,255,width=500,height=500) 
     self.px_width = world_width 
     self.px_height = world_height 
     self.add(bg,z=0) 

class TestScene(cocos.scene.Scene): 
    def __init__(self): 
     super(TestScene,self).__init__() 

    def on_enter(): 
     director.push_handlers(self.on_cocos_resize) 
     super(TestScene, self).on_enter() 

    def on_cocos_resize(self, usable_width, usable_height): 
     self.f_refresh_marks() 

def main(): 
    scene = TestScene() 
    director.init(world_width, world_height, do_not_scale=True) 
    world_map = NetworkMap(world_width, world_height) 
    scroller = cocos.layer.ScrollingManager() 
    scroller.add(world_map) 
    scene.add(scroller) 
    director.run(scene) 

因此,由於某種原因導演沒有我們想要的所有屬性。 我們的堆棧跟蹤是:

Traceback (most recent call last): 
File "map.py", line 49, in <module> 
main() 
File "map.py", line 39, in main 
scene = TestScene() 
File "map.py", line 29, in __init__ 
super(TestScene,self).__init__() 
File "/usr/local/lib/python2.7/dist-packages/cocos2d-0.5.5-py2.7.egg/cocos/scene.py",  line 95, in __init__ 
super(Scene,self).__init__() 
File "/usr/local/lib/python2.7/dist-packages/cocos2d-0.5.5-py2.7.egg/cocos/cocosnode.py", line 114, in __init__ 
self.camera = Camera() 
File "/usr/local/lib/python2.7/dist-packages/cocos2d-0.5.5-py2.7.egg/cocos/camera.py", line 56, in __init__ 
self.restore() 
File "/usr/local/lib/python2.7/dist-packages/cocos2d-0.5.5-py2.7.egg/cocos/camera.py", line 76, in restore 
width, height = director.get_window_size() 
File "/usr/local/lib/python2.7/dist-packages/cocos2d-0.5.5-py2.7.egg/cocos/director.py", line 522, in get_window_size 
return (self._window_virtual_width, self._window_virtual_height) 
AttributeError: 'Director' object has no attribute '_window_virtual_width' 
+0

沒有幫助!抱歉! –

回答

4

您需要在實例化的第一個場景前初始化主任。導演是全局對象初始化屏幕,樹立cocos2d的框架等。

我發現了幾個其他錯誤:

  • 您需要更改ColorLayer是完全合格的,例如cocos.layer.ColorLayer
  • on_enter需要有self作爲第一個參數。
  • 您需要在TestScene類中定義f_refresh_marks

下面是代碼的工作副本。 (工作,在某種意義上說,它不會引發錯誤,而不是它做任何形式的滾動。)

from cocos.director import director 
from cocos.layer import base_layers 


import sys 
import math 
import os 

import pyglet 
import cocos 


world_width = 1000 
world_height = 1000 
class NetworkMap(cocos.layer.ScrollableLayer): 
    def __init__(self, world_width, world_height): 
     self.world_width = world_width 
     self.world_height = world_height 
     super(NetworkMap, self).__init__() 
     bg = cocos.layer.ColorLayer(170,170,0,255,width=500,height=500) 
     self.px_width = world_width 
     self.px_height = world_height 
     self.add(bg,z=0) 

class TestScene(cocos.scene.Scene): 
    def __init__(self): 
     super(TestScene,self).__init__() 

    def on_enter(self): 
     director.push_handlers(self.on_cocos_resize) 
     super(TestScene, self).on_enter() 

    def on_cocos_resize(self, usable_width, usable_height): 
     self.f_refresh_marks() 

    def f_refresh_marks(self): 
     pass 

def main(): 
    director.init(world_width, world_height, do_not_scale=True) 
    scene = TestScene() 
    world_map = NetworkMap(world_width, world_height) 
    scroller = cocos.layer.ScrollingManager() 
    scroller.add(world_map) 
    scene.add(scroller) 
    director.run(scene) 

if __name__ == '__main__': main() 
+0

有趣的是:他們的「做這個第一」快速啓動示例代碼確實試圖啓動導演,但仍然給我錯誤。 http://python.cocos2d.org/doc/programming_guide/quickstart.html – user391339

0

我有同樣的問題(具有非常類似的堆棧跟蹤),這是因爲我想在調用director.init()之前創建一個圖層。將director.init()移動到代碼中的更早版本中爲我解決了這個問題。