Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocos2d: AttributeError: 'Director' object has no attribute '_window_virtual_width'

We are using the cocos2d framework to create a game. We're completely new to this framework, so we cannot get the director object to work as we are expecting. 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)

So for some reason the director doesn't have all the attributes we want. Our stack trace is:

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'
like image 335
beardless_sysad Avatar asked Nov 06 '12 07:11

beardless_sysad


Video Answer


2 Answers

You need to initialise the director before you instantiate your first scene. The director is the global object that initialises your screen, sets up the Cocos2D framework, etc.

I found a few other errors:

  • You need to change ColorLayer to be fully qualified, e.g. cocos.layer.ColorLayer.
  • on_enter needs to have self as the first argument.
  • You need to define f_refresh_marks in your TestScene class.

Here's a working copy of the code. (Working, in the sense that it does not throw errors, not that it does any sort of 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()
like image 144
Laurence Dougal Myers Avatar answered Oct 14 '22 23:10

Laurence Dougal Myers


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

like image 27
patriot Avatar answered Oct 15 '22 00:10

patriot