Skip to content

Commit c9ccaba

Browse files
committed
Fixed actions.py
1 parent 45fe9ae commit c9ccaba

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

basics/actions.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
# Here I make a class that extends cocos' ColorLayer class
1515
# This type of layer is different because it has a background color! (awesome right?)
1616
class Actions(ColorLayer):
17-
# When I initialize it I need to pass in an RRGB colour value so it can set the ColorLayer's background
17+
# When I initialize it I need to pass in an RGBA colour value so it can set the ColorLayer's background
1818
# I grabbed the Peter River colour (#3498db) from http://flatuicolors.com for this sample
1919
def __init__(self):
20-
super(Actions, self).__init__(52, 52, 152, 219)
20+
super(Actions, self).__init__(52, 152, 219, 1000)
2121

2222
# I initialize a sprite using the cocos sprite.Sprite class
2323
# Change the path below to whatever your sprite image is
@@ -30,8 +30,12 @@ def __init__(self):
3030
# Check those class functions for info on how to code them!
3131
self.fade_in()
3232
self.move_left()
33-
self.move_right()
34-
self.move_right()
33+
34+
# It's important to note that placing actions one after another can often lead to issues
35+
# For example, if you told the sprite to move left one and then move right twice, it would only move right once
36+
# This is because python executes the code so quickly that it doesn't have time to complete the first animations
37+
# Using the typical time.sleep from the time builtin doesn't work either, because it stops the entire game
38+
# Therefore, actions are best suited for user input, not cutscenes or animations
3539

3640
def fade_in(self):
3741
# First I make a FadeIn animation object
@@ -48,30 +52,14 @@ def fade_in(self):
4852
# I don't need to reset the opacity because the FadeIn action does that for me!
4953
self.sprite.do(fade_action)
5054

51-
def move_left(self, sleep_time=2):
55+
def move_left(self):
5256
# First I make a MoveBy animation object
5357
# I set this to move -150 on the X axis, 0 on the Y axis, and to take 2 seconds
5458
left = MoveBy((-150, 0), 2)
5559

5660
# Finally I make my sprite move left
5761
self.sprite.do(left)
5862

59-
# I need to make the program sleep so that it lets this animations finish
60-
sleep(sleep_time)
61-
62-
def move_right(self, sleep_time=2):
63-
# If you notice, this method is exactly the same as the move left method, but it moves +150 instead of -150
64-
65-
# First I make a MoveBy animation object
66-
# I set this to move +150 on the X axis, 0 on the Y axis, and to take 2 seconds
67-
right = MoveBy((150, 0), 2)
68-
69-
# And I make my sprite move right (just like in left)
70-
self.sprite.do(right)
71-
72-
# Again I need my program to sleep for the 2 seconds the animation requires
73-
sleep(sleep_time)
74-
7563

7664
# I initialize the director and run the layer (this is typical for cocos programs)
7765
director.init()

basics/getting_started.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
I chose these import statements just to make my close look cleaner
33
For this to work you would just need to import cocos and then add the subdirectory after
4-
Ex: self.label = Label(...) would be self.sprite = cocos.text.Label(...)
4+
Ex: self.label = Label(...) would be self.label = cocos.text.Label(...)
55
"""
66
import cocos
77
from cocos.text import Label
@@ -51,4 +51,4 @@ def __init__(self):
5151
)
5252
)
5353

54-
# That's it! Run it and see what happens
54+
# That's it! Run it and see what happens

basics/review.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,32 @@ def __init__(self):
5050
# We don't need anything else here, let's just let our sprite be moved in the event handlers
5151

5252
# So now we can overload some default event handlers
53+
# We'll let the user move in any direction on the screen with the arrow keys, and jump around with the spacebar
5354
# We'll only be doing keyboard input for this program
5455
def on_key_press(self, key, modifiers):
5556
# If you don't know what these next couple lines do, go check the previous tutorials
5657
move_left = MoveBy((-50, 0), .5)
5758
move_up = MoveBy((0, 50), .5)
59+
jump = Jump(50, 0, 1, .25)
5860

5961
# Check if they want to go left, and then actually make the sprite go left
6062
if symbol_string(key) == "LEFT":
6163
self.sprite.do(move_left)
6264

6365
# Or maybe if they want to move right?
64-
if symbol_string(key) == "RIGHT":
66+
elif symbol_string(key) == "RIGHT":
6567
self.sprite.do(Reverse(move_left))
6668

6769
# And lastly we need that jump game to be strong
68-
if symbol_string(key) == "UP":
70+
elif symbol_string(key) == "UP":
6971
self.sprite.do(move_up)
7072

71-
if symbol_string(key) == "DOWN":
73+
elif symbol_string(key) == "DOWN":
7274
self.sprite.do(Reverse(move_up))
7375

76+
elif symbol_string(key) == "SPACE":
77+
self.sprite.do(jump)
78+
7479

7580
# And finally we do our usual initialization and run the scene
7681
mixer.init()

0 commit comments

Comments
 (0)