Skip to content

Commit 88c6cbe

Browse files
committed
- Added actions to our helloWorld example
1 parent 1b8de52 commit 88c6cbe

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

helloWorldActions.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
11
#!/usr/bin/env python3
22

3-
# try to import the cocos module
3+
# try to import the cocos module and import to our namespace the .actions from the cocos module
44
try:
55
import cocos
6+
from cocos.actions import *
67
except:
78
print('Cannot import cocos. Did you run \'pip install cocos2d\' ?')
89

9-
# define a simple HelloWorld() class that inherits from cocos.layer.Layer (making it a sub-class of it)
10-
class HelloWorld(cocos.layer.Layer):
10+
# define a simple HelloWorld() class that inherits from cocos.layer.ColorLayer (making it a sub-class of it)
11+
# Difference between .Layer and .ColorLayer is, well, you can Colorize
12+
class HelloWorld(cocos.layer.ColorLayer):
1113
# __init__ is run every time you instantiate the class
1214
def __init__(self):
13-
# Always call super in the constructor
14-
super(HelloWorld, self).__init__()
15+
# Always call super in the constructor, we taint the layer with r:64 g:64 b:224 alpha:255
16+
super(HelloWorld, self).__init__(64,64,224,255)
1517
# label will become an object with all the necessary to display a text, with font TimesNewRoman and it will be center-anchored
1618
# Note: .Label is a subclass of CocosNode
1719
label = cocos.text.Label('Hello, world', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center')
1820
# set the position of our text to x:320 y:240
1921
label.position = (320, 240)
2022
# add our label as a child. It is a CocosNode object, which know how to render themselves.
2123
self.add(label)
24+
# sprite becomes a .Sprite object with our molecule picture
25+
sprite = cocos.sprite.Sprite('sprites/molecule.png')
26+
# set the position of our sprite to x:320 y;240 the default position is x:0 y:0
27+
sprite.position = (320,240)
28+
# scale the sprite 2x
29+
sprite.scale = 2
30+
# add our sprite as a child and make sure it is on top by defining the z-axis, default is z: 0
31+
self.add(sprite, z=1)
32+
# create a ScaleBy action object that scales our sprite 3x during 2 seconds
33+
scale = ScaleBy(3, duration=2)
34+
# Repeat the scale action plus the revers of the scale action to our label
35+
label.do(Repeat(scale + Reverse(scale)))
36+
# Repeat the reverse of our scale action plus the scale action to our sprite
37+
sprite.do(Repeat(Reverse(scale) + scale))
2238

2339
# our main funciton
2440
def main():
2541
# We initialize the director, that takes care of our main window
2642
cocos.director.director.init()
2743
# We instantiate hello_layer with our HelloWorld() class
2844
hello_layer = HelloWorld()
45+
# All CocosNode objects can execute actions, so we can execute ONE rotate action on ALL of our layers. 360deg-turn in 10 seconds
46+
hello_layer.do(RotateBy(360, duration=10))
2947
# Now we create a .Scene and pass our HelloWorld() object stored in hello_layer, as a child
3048
main_scene = cocos.scene.Scene(hello_layer)
3149
# All setup now. Let's run our main_scene

0 commit comments

Comments
 (0)