Game Over, game pausing

31 views
Skip to first unread message

Jason

unread,
Oct 26, 2012, 4:38:00 PM10/26/12
to cocos-...@googlegroups.com
What's the best way to just pause the game. Let's say the user has caused the game to be over through some lose condition. I jsut want to pause all the actions in the scene and layers, then throw up some info on a Sprite or Label or whatehaveyou.

This is from the active layer and is called by a lose condition in one of the game loops:

    def endGame(self):
        print 'GAME OVER MAN, GAME OVER!'
        nextScene = cocos.scenes.pause.get_pause_scene()
        nextScene.add(cocos.scenes.pause.PauseLayer())
        director.push(nextScene)


BTW, this is the start.py  script I have:

def init():
    try:
        director.init()
        win = director.window
        cursor = win.get_system_mouse_cursor(BaseWindow.CURSOR_CROSSHAIR)
        win.set_mouse_cursor(cursor)
        StartScene = cocos.scene.Scene()
        StartScene.add(SpaceLayer())

#        StartScene.add(PlanetLayer(GlobalVariables()))
#        StartScene.add(CityLayer(GlobalVariables()))
#        StartScene.add(BuildingLayer(GlobalVariables()))
        director.run(StartScene)
    except:
        traceback.print_exc(file=sys.stdout)
        pyglet.app.exit()
        sys.exit()

The commented out bits are just to test the different layers. Each one is put into a Scene object, then has the director push it out from the previous layer. Both handleLaunchCraft and endGame are called from the active layer.

    def handleLaunchCraft(self):
        if (self.currentPopnCenter != ''):
            nextScene = cocos.scene.Scene()
            nextScene.add(self.getNextLayer())
            director.push(nextScene)



But then I get a PAUSE text up front, is there any way to override that?

Also, am I structuring this correctly?

Thanks,
J

claudio canepa

unread,
Oct 26, 2012, 7:37:05 PM10/26/12
to cocos-...@googlegroups.com
In your endGame method you are:
    using the default pause scene, which adds that text
    adding the PauseLayer, which also display that text

So
  don't add cocos.scene.pause.PauseLayer, 
  dont get the pause scene with cocos.scenes.pause.get_pause_scene(); instead copy paste the function cocos.pause.default_pause_scene , rename it, and shorten the return line to
       return PauseScene(texture.get_region(0, 0, w, h))

Obviously you can add the other info you want to display.

 
Also, am I structuring this correctly?


When the old scene is never to continue, it is better to use director.replace than director.push: some resources are then released. 
 

Jason

unread,
Oct 27, 2012, 7:20:22 PM10/27/12
to cocos-...@googlegroups.com
Hi Claudio,

Thanks, that worked perfectly. I'm curious as to why it seems, at least to me, a little bit complicated to pause a scene. I'd imagine something int he API where we could call director.pause() and it'd just do all this for me. (Thanks for the tip about replacing scenes).

Here is my finished working code, for anyone int he future looking for an answer.

This code is from the currently executing Layer:


    def endGame(self):
        #make game over screen
        endGameLayer = Layer()
        posx = int(self.windowWidth / 2 - 140)
        posy = int(self.windowHeight/2)
        txtWarning = cocos.text.Label('GAME OVER MAN, \n GAME OVER!!',
                                           font_size=12,
                                           font_name="Disco",
                                           x=posx,
                                           y=posy
                                           )
        endGameLayer.add(txtWarning, 20)
        #add pause scene
        endScene = self.getPauseScene()
        endScene.add(endGameLayer)
        director.push(endScene)
       
    def getPauseScene(self):
        w, h = director.window.width, director.window.height
        texture = pyglet.image.Texture.create_for_size(GL_TEXTURE_2D, w, h, GL_RGBA)
        texture.blit_into(pyglet.image.get_buffer_manager().get_color_buffer(), 0,0,0)

        return PauseScene(texture.get_region(0, 0, w, h))



Reply all
Reply to author
Forward
0 new messages