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)