"Hello world' throwing attribute error

341 views
Skip to first unread message

Michael Calvey

unread,
Feb 4, 2016, 8:21:15 PM2/4/16
to cocos2d discuss
Hello all,

I am brand new to cocos, and I am trying to find the best engine/ library for a top down game I am about to start working on. While going through the hello world exercise in the docs, I am receiving an AttributeError.

Here is the code:
import cocos

class HelloWorld(cocos.layer.Layer):

    def __init__(self):
        super( HelloWorld, self).__init__()

    label = cocos.text.Label('Hello, world',
                              font_name='Times New Roman',
                              font_size=32,
                              anchor_x='center', anchor_y='center')
    label.position = 320,240
    self.add( label )
    cocos.director.director.init()
    hello_layer = HelloWorld ()
    main_scene = cocos.scene.Scene (hello_layer)
    cocos.director.director.run (main_scene)

And the traceback:
Traceback (most recent call last):
  File "/Users/michaelcalvey/Documents/cocos.py", line 1, in <module>
    import cocos
  File "/Users/michaelcalvey/Documents/cocos.py", line 4, in <module>
    class HelloWorld(cocos.layer.Layer):
AttributeError: module 'cocos' has no attribute 'layer'

Thanks a lot for the help, sorry if it is an obvious problem but I have looked around and haven't found anything relevant.
I am really looking forward to getting this to work!

Michael

Daniel Gillet

unread,
Feb 15, 2016, 2:40:30 PM2/15/16
to cocos2d discuss
Hello,

Glad you give cocos a try.

The solution to your problem is quite simple. You've named your file `cocos.py`. So when importing cocos, Python only imports your file itself. Rename it to something else, and you won't have this problem. :)

Cheers,
Dan.

Philip McDermott

unread,
Apr 29, 2017, 11:58:54 PM4/29/17
to cocos2d discuss
I am using the same code and am getting this error: Traceback (most recent call last):
  File "C:/Users/Jeanne and Phil/AppData/Local/Programs/Python/Python35-32/helloworld.py", line 3, in <module>
    class HelloWorld(cocos.layer.Layer):
  File "C:/Users/Jeanne and Phil/AppData/Local/Programs/Python/Python35-32/helloworld.py", line 11, in HelloWorld
    anchor_x='center', anchor_y='center')
  File "C:\Users\Jeanne and Phil\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cocos\text.py", line 65, in __init__
    super(TextElement, self).__init__()
  File "C:\Users\Jeanne and Phil\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cocos\cocosnode.py", line 136, in __init__
    self.camera = Camera()
  File "C:\Users\Jeanne and Phil\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cocos\camera.py", line 61, in __init__
    self.restore()
  File "C:\Users\Jeanne and Phil\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cocos\camera.py", line 81, in restore
    width, height = director.get_window_size()
  File "C:\Users\Jeanne and Phil\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cocos\director.py", line 554, in get_window_size
    return self._window_virtual_width, self._window_virtual_height
AttributeError: 'Director' object has no attribute '_window_virtual_width'
Any suggestions? My file name is helloworld.py 

Claudio Canepa

unread,
Apr 30, 2017, 12:36:28 AM4/30/17
to cocos2d discuss
If your code looks like the one posted above, there is a problem with indentation.
The python interpreter sees
 - "import cocos" : no problem here
 - "class HelloWorld(cocos.layer.Layer):" : begins class definition, no problem here
 - "    def __init__(self):
         super( HelloWorld, self).__init__()
   " : begins method __init__, and the only instruction in that method is the super(...) call, because the next line dedents
 - "label = cocos.text.Label(...)" : a class member "label" is defined, and the code tries to assign a Label instance. But the label instantiation wants to know the window geometry to do the self centering; problem is, the window was not yet created, hence the AttributeError.

I fixed the indentation to what seemed the desired intent, and it runs as expected.
Attached (so as the formatting don't get lost) the fixed code 
helloworld.py

Nathan Allen

unread,
Nov 3, 2019, 11:26:13 PM11/3/19
to cocos2d discuss
I know this is super old, but for me it was caused by installing just "cocos" from pip, then i tried installing cocos2d and the two modules like merged into the same folder. so just delete the directory and install cocos2d with pip again.


On Thursday, February 4, 2016 at 8:21:15 PM UTC-5, Michael Calvey wrote:
Reply all
Reply to author
Forward
0 new messages