Page 1 of 1

Error of using cocos2d python 3.5

Posted: Thu Mar 15, 2018 4:18 pm
by Ivan219
Hello, I have problem with use of the cocos2d on python 3.5

I install cocos2d with pip3. Cocos2d successfully installed.
I tried to start program:
https://github.com/CoderDojoLu/cocos2d- ... loWorld.py
But I got:

Code: Select all

Cannot import cocos. Did you run 'pip install cocos2d' ?
Traceback (most recent call last):
  File "/home/pi/Desktop/test.py", line 7, in <module>
    class HelloWorld(cocos.layer.Layer):
NameError: name 'cocos' is not defined
How I can resolve problem?

Re: Error of using cocos2d python 3.5

Posted: Fri Mar 16, 2018 3:00 am
by KLL
hi Ivan,
bad style, just missing the main required info,
but i was able to get the same error:

Code: Select all

pi@RPI3:~/projects/cocos $ python helloWorld.py
Cannot import cocos. Did you run 'pip install cocos2d' ?
Traceback (most recent call last):
  File "helloWorld.py", line 10, in <module>
    class HelloWorld(cocos.layer.Layer):
NameError: name 'cocos' is not defined
pi@RPI3:~/projects/cocos $ 
when i started with python .. === python2x
but not installed with pip
( like you i used pip3 so must start with python3 )

Code: Select all

pi@RPI3:~/projects/cocos $ pip3 install cocos2d
Collecting cocos2d
  Downloading https://www.piwheels.org/simple/cocos2d/cocos2d-0.6.5-py3-none-any.whl (288kB)
    100% |████████████████████████████████| 296kB 651kB/s
Collecting six>=1.4 (from cocos2d)
  Downloading six-1.11.0-py2.py3-none-any.whl
Collecting pyglet>=1.2 (from cocos2d)
  Downloading pyglet-1.3.1-py2.py3-none-any.whl (1.0MB)
    100% |████████████████████████████████| 1.0MB 274kB/s
Collecting future (from pyglet>=1.2->cocos2d)
  Downloading https://www.piwheels.org/simple/future/future-0.16.0-py3-none-any.whl (489kB)
    100% |████████████████████████████████| 491kB 309kB/s
Installing collected packages: six, future, pyglet, cocos2d
Successfully installed cocos2d-0.6.5 future-0.16.0 pyglet-1.3.1 six-1.11.0
pi@RPI3:~/projects/cocos $
so you miss to show the
- - install line
- - start line


Image
beginning with IDLE3 can help
Image

anyhow possibly better to do the full install like here
start with download zip file ...
what includes docs and samples.

Re: Error of using cocos2d python 3.5

Posted: Fri Mar 16, 2018 12:22 pm
by Ivan219
Thank for your answer. I tried to do this steps again:

Code: Select all

pi@raspberrypi:~ $ cd Desktop
pi@raspberrypi:~/Desktop $ pip3 install cocos2d
Collecting cocos2d
Collecting six>=1.4 (from cocos2d)
  Using cached six-1.11.0-py2.py3-none-any.whl
Collecting pyglet>=1.2 (from cocos2d)
  Using cached pyglet-1.3.1-py2.py3-none-any.whl
Collecting future (from pyglet>=1.2->cocos2d)
Installing collected packages: six, future, pyglet, cocos2d
Successfully installed cocos2d-0.6.5 future-0.16.0 pyglet-1.3.1 six-1.11.0
pi@raspberrypi:~/Desktop $ python3 test.py
Cannot import cocos. Did you run 'pip install cocos2d' ?
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    class HelloWorld(cocos.layer.Layer):
NameError: name 'cocos' is not defined
pi@raspberrypi:~/Desktop $ 
But the error still remains.

Re: Error of using cocos2d python 3.5

Posted: Fri Mar 16, 2018 12:38 pm
by KLL
not look good,

sorry when i created that error by using "python" instead "python3" the error talk about
LINE 10
but you report:
File "test.py", line 7

hm, pls. post your file test.py

just make sure, is your system updated?

Re: Error of using cocos2d python 3.5

Posted: Fri Mar 16, 2018 12:44 pm
by Ivan219

Code: Select all

try:
	import cocos
except:
	print('Cannot import cocos. Did you run \'pip install cocos2d\' ?')

# define a simple HelloWorld() class that inherits from cocos.layer.Layer (making it a sub-class of it)
class HelloWorld(cocos.layer.Layer):
	# __init__ is run every time you instantiate the class
	def __init__(self):
		# Always call super in the constructor
		super(HelloWorld, self).__init__()
		# label will become an object with all the necessary to display a text, with font TimesNewRoman and it will be center-anchored
		# Note: .Label is a subclass of CocosNode
		label = cocos.text.Label('Hello, world', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center')
		# set the position of our text to x:320 y:240
		label.position = (320, 240)
		# add our label as a child. It is a CocosNode object, which know how to render themselves.
		self.add(label)

# our main function
def main():
	# We initialize the director, that takes care of our main window
	cocos.director.director.init()
	# We instantiate hello_layer with our HelloWorld() class
	hello_layer = HelloWorld()
	# Now we create a .Scene and pass our HelloWorld() object stored in hello_layer, as a child
	main_scene = cocos.scene.Scene(hello_layer)
	# All setup now. Let's run our main_scene
	cocos.director.director.run(main_scene)
	# The above could have been compacted to:
	# cocos.director.director.run(cocos.scene.Scene(HelloWorld()))

if __name__ == '__main__': main()

Re: Error of using cocos2d python 3.5

Posted: Fri Mar 16, 2018 12:55 pm
by KLL
your code works here!
( you deleted only the headerline, no problem as long you not want to start it by "./test.py" )
Image

actually i have no idea whats wrong with your system...