OnscreenImage issues

Hi guys, I’m using a state machine to manage the game between the main game state and the main menu. I want the mainmenu image to disappear when moving to the main game state and reappear when moving back to the main menu state.

Problem:
When I switch to the game state, the main menu image stays on the screen

Here is the relevant code:

class GameManager:
“”“Manages all game states and any other global information”""
gameState = “NULL” #Current state of the game - “gameMain, mainMenu, startUp”
changeStateTo = “NULL” #State to change to on the next pass of the render loop
firstStateRun = 1 #Flag for if this the first run of the state in the update loop
frameCount = 0 #Number of frames rendered since the game start
lastFrame = 0.0 #Amount of time elapsed since the last frame
menuFilename = ‘2D/Menu.png’
menuScreen = 0

def __init__(self):
    menuScreen = OnscreenImage(image = self.menuFilename, pos = (0.0, 0.0, 0.0))

def gameStateManager(task):
“”“Manages the entry and exit of all game state tasks”""

#Update frame count
GameMgr.frameCount = GameMgr.frameCount + 1

#if the state has changed in the last render loop
if (GameMgr.changeStateTo != GameMgr.gameState):
    print "Game State Changing to: " + GameMgr.changeStateTo
    
    #--STATE EXIT EVENTS------------------------------------
    
    #if the state is changing from mainMenu
    if (GameMgr.gameState == "mainMenu"):
        print "mainMenu Teardown"
        
        #Events
        GameMgr.menuScreen.destroy()
        
        #Remove task from task manager
        taskMgr.remove("Main Menu Loop")
        
    #if the state is changing from gameMain
    if (GameMgr.gameState == "gameMain"):
        print "gameMain Teardown"
        
        #Events
        testworld.detachNode()
        
        #Remove task from task manager
        taskMgr.remove("Game Main Loop")
        


    #--STATE ENTRY EVENTS----------------------------------
    
    #if the state is changing to mainMenu
    if (GameMgr.changeStateTo == "mainMenu"):
        print "mainMenu Setup"
        
        #Events
        GameMgr.menuScreen = OnscreenImage(image = GameMgr.menuFilename, pos = (0.0, 0.0, 0.0))
        base.enableMouse()
        props.setCursorHidden(False)
        
        #Add task to task manager
        taskMgr.add(mainMenuLoop, "Main Menu Loop")
        
    #if the state is changing to gameMain
    if (GameMgr.changeStateTo == "gameMain"):
        print "gameMain Setup"
        
        #Events
        testworld.reparentTo(render)
        base.disableMouse()
        props.setCursorHidden(True)
        
        #Add task to task manager
        taskMgr.add(gameMainLoop, "Game Main Loop")
    
    #Offically mark the game state as changed
    GameMgr.gameState = GameMgr.changeStateTo
    
return Task.cont

taskMgr.add(gameStateManager, “Game State Manager”)

#END CODE

it seems like the GameMgr.menuScreen.destroy() line of the Exit State Events block does not execute as intended (it does execute that line, I’ve checked via print statements)

Hmm, it’s real hard to read your code as posted–next time, try putting it within the Code tags, which will preserve indentations.

As to what’s going wrong, my first guess is that you have somehow created multiple OnscreenImages on top of each other, so that when you destroy one, it still leaves the others in the same place. Is this possible?

David

Ah, there’s the code tag button staring me right in the face.

I managed to solve the problem (although I’m still not sure how it works), by moving the menuImage object handling to the mainMenu task instead of as a member of the persistent global object GameManager.

Beforehand, I thought this would cause some kind of flicker, though now I remember how a frame buffer actually works, lol.

Thanks for the advice and tolerating my hard-to-read text.