Note that there are some explanatory texts on larger screens.

plurals
  1. POLimiting actions under state events c#
    primarykey
    data
    text
    <p>I am trying to create a screenManager that can open and close multiple screens at a time. I have bound the P key to open a screen called Pause when pressed. However, in a debug statement i can see that the screen is being created and opened multiple times with the press of the P key. I can stop this by checking if the screen variable is null or not before creating/adding it to the screen list. Then, though, I get a StackOverflow exception when trying to dispose of it. Her eis my code:</p> <p>Pause.cs- If I dont call Dispose() here I am unable to reopen the screen in MainMenu because Pause is no longer null.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; namespace RoomGame { public class Screen : IDisposable { public ScreenController screenController; public bool isUpdating=true; public bool isDrawn=true; public GraphicsDeviceManager graphics; /*public Screen(ScreenController sc) { this.screenController = sc; this.isActive = true; }*/ public virtual void Initialize() { } public virtual void LoadContent(ContentManager c) { } public virtual void Update(GameTime gameTime) { } public virtual void Draw(GameTime gameTime,SpriteBatch sb) { } public void Dispose() { this.Dispose(); } } } </code></pre> <p>Screen.cs - The StackOVerflow exception occurs in Dispose here</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; namespace RoomGame { public class Screen : IDisposable { public ScreenController screenController; public bool isUpdating=true; public bool isDrawn=true; public GraphicsDeviceManager graphics; /*public Screen(ScreenController sc) { this.screenController = sc; this.isActive = true; }*/ public virtual void Initialize() { } public virtual void LoadContent(ContentManager c) { } public virtual void Update(GameTime gameTime) { } public virtual void Draw(GameTime gameTime,SpriteBatch sb) { } public void Dispose() { this.Dispose(); } } } </code></pre> <p>MainMenu.cs - This is the class that creates the Pause screen</p> <pre><code>using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Input; namespace RoomGame { public class MainMenu : Screen { GameController gameController; ScreenController screenController; MyTexture start; Rectangle area; Vector2 position; ContentManager content; Screen pause = null; //public GraphicsDeviceManager graphics; public MainMenu(GameController gc) { this.gameController = gc; gameController.Game.IsMouseVisible = true; //this.content = c; this.isUpdating = true; this.isDrawn = true; //this.graphics = this.gameController.game.graphics; } public override void LoadContent(ContentManager c) { position = new Vector2(100, 100); start = new MyTexture("Textures/Start",position,c); //texture = this.gameController.game.Content.Load&lt;Texture2D&gt;("Textures/Start"); //texture=this.gameController.content.Load&lt;Texture2D&gt;("Textures/Start"); //texture = this.gameController.Game.Content.Load&lt;Texture2D&gt;("Textures/Start"); } public override void Update(GameTime gameTime) { //texture = this.gameController.content.Load&lt;Texture2D&gt;("Textures/Start"); area = start.getBounds(); MouseState mouse = Mouse.GetState(); if ((mouse.LeftButton == ButtonState.Pressed) &amp;&amp; (area.Contains(mouse.X, mouse.Y))) { gameController.Game.Exit(); } KeyboardState key = Keyboard.GetState(); if (key.IsKeyDown(Keys.P)) { if (pause == null) { pause = new Pause(this.gameController); gameController.screenController.AddScreen(pause); } } } public override void Draw(GameTime gameTime,SpriteBatch sb) { start.Draw(sb); } } } </code></pre> <p>EDIT: My mistake. The debug state was in the wrong place so while it was showing multiple adds the List.Contains statement was preventing it from acctually happening.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload