1
+ # I won't explain the inputs this time - I think you guys are getting the jist of it
2
+ import cocos
3
+ from cocos .scene import Scene
4
+ from cocos .layer import Layer , ColorLayer
5
+ from cocos .director import director
6
+ from cocos .sprite import Sprite
7
+ from cocos .text import Label
8
+ from cocos .actions import *
9
+ from cocos .audio .pygame .mixer import Sound
10
+ from cocos .audio .pygame import mixer
11
+
12
+
13
+ # For this tutorial, we will be making two scenes and switching between them
14
+ # Theoretically, we could have two scenes made of the same layer, but there would be no visual difference between them
15
+ # For that reason we need to make two scenes with different text
16
+ # As a bonus I make them different colours as well
17
+
18
+
19
+ class Layer1 (ColorLayer ):
20
+ # I set the layer to be an event handler because I want to be able to transition to another scene on a key press
21
+ is_event_handler = True
22
+
23
+ def __init__ (self ):
24
+ # Remember that I need to pass in an RGBA colour value because it's a ColorLayer
25
+ super (Layer1 , self ).__init__ (155 , 89 , 182 , 1000 )
26
+
27
+ # I make a simple label with no extra parameters such as font or anchors
28
+ text = Label ("This is the first scene" )
29
+
30
+ # For the position, I do something we haven't done before
31
+ # We've been making the position static, but we don't exactly know the width and height of a player's window
32
+ # Therefore, getting the virtual height and width and then just dividing them by two is a safer bet
33
+ text .position = director ._window_virtual_width / 2 , director ._window_virtual_height / 2
34
+
35
+ # And lastly we add it
36
+ self .add (text )
37
+
38
+ # Here is the overload for the on_key_press function
39
+ def on_key_press (self , key , modifiers ):
40
+ # Here is entire purpose of this lesson!
41
+ director .replace (Scene (Layer2 ()))
42
+ # That line replaces whatever scene is currently running in the director with the scene indicated
43
+ # In this case, we create a new scene object inline out of the second layer, which I have coded below
44
+
45
+
46
+ # Here is the second Layer I make, for the second scene
47
+ class Layer2 (ColorLayer ):
48
+ # Same as before, we let it handle user input
49
+ is_event_handler = True
50
+
51
+ # Initialize it and call the super class, but this time pass in a different colour
52
+ def __init__ (self ):
53
+ super (Layer2 , self ).__init__ (231 , 76 , 60 , 1000 )
54
+
55
+ # Same Label code as before but this time with different text
56
+ text = Label ("This is the second scene" )
57
+ text .position = director ._window_virtual_width / 2 , director ._window_virtual_height / 2
58
+ self .add (text )
59
+
60
+ # And once again the same user input code
61
+ def on_key_press (self , key , modifiers ):
62
+ director .replace (Scene (Layer1 ()))
63
+
64
+
65
+ # Here we initialize the director and start running the first scene
66
+ director .init ()
67
+ director .run (Scene (Layer1 ()))
68
+
69
+ # But if you remember, when you press a key that layer will tell the director to make a new scene out of the second layer
70
+ # And that's about it. As you can see, it's pretty easy to switch between scenes
0 commit comments