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 



On Sunday, 30 April 2017 00:58:54 UTC-3, Philip McDermott wrote:
>
> 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'
> >>> 
>
> On Thursday, February 4, 2016 at 8:21:15 PM UTC-5, Michael Calvey wrote:
>>
>> 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'
>>
>
> Any suggestions? My file name is helloworld.py 
>
>>
>> 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
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"cocos2d discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/cocos-discuss.
For more options, visit https://groups.google.com/d/optout.
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)

Reply via email to