简体   繁体   English

Cocos2d:AttributeError:'Director'对象没有属性'_window_virtual_width'

[英]Cocos2d: AttributeError: 'Director' object has no attribute '_window_virtual_width'

We are using the cocos2d framework to create a game. 我们正在使用cocos2d框架来创建游戏。 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'

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. 导演是初始化屏幕,设置Cocos2D框架等的全局对象。

I found a few other errors: 我发现了一些其他错误:

  • You need to change ColorLayer to be fully qualified, eg cocos.layer.ColorLayer . 您需要将ColorLayer更改为完全限定,例如cocos.layer.ColorLayer
  • on_enter needs to have self as the first argument. on_enter需要将self作为第一个参数。
  • You need to define f_refresh_marks in your TestScene class. 您需要在TestScene类中定义f_refresh_marks

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()

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(). 我遇到了同样的问题(堆栈跟踪非常相似),这是因为我在调用director.init()之前尝试创建一个层。 Moving director.init() to earlier in the code fixed it for me. 将director.init()移到代码中的早期,为我修复了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM