Cocos2d: AttributeError: 'Director' object does not have '_window_virtual_width' attribute

We use the cocos2d framework to create a game. We are completely new to this structure, so we cannot get the director object to work as we expect. Here is our code skeleton:

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) 

Therefore, for some reason, the director does not have all the attributes we need. Our stack trace:

 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' 
+4
source share
2 answers

You need to initialize the director before creating your first scene. A director is a global object that initializes your screen, customizes your Cocos2D environment, etc.

I found some more errors:

  • You need to change the ColorLayer to be fully prepared, for example. cocos.layer.ColorLayer .
  • on_enter should have self as the first argument.
  • You need to define f_refresh_marks in your TestScene class.

Here is a working copy of the code. (Work in the sense that it does not cause errors, not something like scrolling.)

 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() 
+4
source

I had the same problem (with a very similar stack trace), and that was because I was trying to create a layer before calling director.init (). Moving director.init () earlier in the code fixed this for me.

0
source

Source: https://habr.com/ru/post/1444277/


All Articles