can not change scenes in Cocos2d

30 Views Asked by At

I am working on a cocos2d learning project. When I want to change the scenes, I got only the first scene, the second scene never shows up. Could anyone tell me about what was wrong in my code?

import cocos
import time
from cocos.scene import *
from cocos.director import director


class HelloCocos(cocos.layer.Layer):
    def __init__(self, text):
        super().__init__()
        self.text = text
        size = director.get_window_size()
        label = cocos.text.Label(self.text, font_name="Times New Roman", font_size=32)
        label.position = size[0] / 2, size[1] / 2
        self.add(label)


class HelloCocos2(cocos.layer.Layer):
    def __init__(self, text):
        super().__init__()
        self.text = text
        size = director.get_window_size()
        label = cocos.text.Label(self.text, font_name="Times New Roman", font_size=32)
        label.position = size[0] / 3, size[1] / 3
        self.add(label)


director.init(width=1280, height=800, caption="my cocos window")

first_scene = Scene(HelloCocos("hello cocos"))

second_scene = Scene(HelloCocos2("end cocos"))

director.run(first_scene)

director.replace(second_scene)

1

There are 1 best solutions below

0
blue pine On

In cocos2d, in order to replace scenes, you push the first scene to the stack, and pop up the second scene like this.

Director::getInstance()->replaceScene(second_scene);