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

bea*_*sad 6 python cocos2d-iphone

我们正在使用cocos2d框架来创建游戏.我们对这个框架完全陌生,所以我们不能让导演对象像我们期望的那样工作.这是我们的代码框架:

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)
Run Code Online (Sandbox Code Playgroud)

因此,导演没有我们想要的所有属性.我们的堆栈跟踪是:

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'
Run Code Online (Sandbox Code Playgroud)

Lau*_*ers 6

在实例化第一个场景之前,需要初始化控制器.导演是初始化屏幕,设置Cocos2D框架等的全局对象.

我发现了一些其他错误:

  • 您需要更改ColorLayer为完全合格,例如cocos.layer.ColorLayer.
  • on_enter需要self作为第一个参数.
  • 您需要f_refresh_marks在TestScene类中定义.

这是代码的工作副本.(工作,从某种意义上说它不会抛出错误,而不是它会进行任何类型的滚动.)

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()
Run Code Online (Sandbox Code Playgroud)