Cześć,
Robię projekt, grę typu Snake na Windows Universal Platform. Zrobiłem już proste menu i przyciski i sceny. Problem z którym walczę dotyczy sceny informacje o grze, a mianowicie gdy nacisnę przycisk Cofnij raz aplikacja wraca do menu głównego, innym razem przechodzi do rozgrywki(tak nie powinno być). Przy uruchomieniu zdarzy się że mogę kilka razy wejść i wyjść z informacji, a innym razem już po pierwszej próbie włącza się scena rozgrywki.
klasa główna
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace GRA
{
public class MainGame : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
ScenaMenu menuScene;
ScenaGra gameScene;
ScenaKoniecGry gameOverScene;
ScenaInformacje scenaInformacje;
public MainGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
menuScene = new ScenaMenu(this);
gameScene = new ScenaGra(this);
scenaInformacje = new ScenaInformacje(this);
gameOverScene = new ScenaKoniecGry(this);
IsMouseVisible = true;
Window.AllowUserResizing = true;
StanMenu.IsShowMainMenuScene = true;
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if (StanMenu.IsShowMainMenuScene)
{
menuScene.Update();
}
if (StanMenu.IsShowGameScene)
{
gameScene.Update(gameTime);
}
if(StanMenu.IsShowInfoScene)
{
scenaInformacje.Update();
}
if (StanMenu.IsShowGameOverScene)
{
gameOverScene.Update();
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
if (StanMenu.IsShowMainMenuScene)
{
menuScene.Draw(spriteBatch, gameTime);
}
if (StanMenu.IsShowGameScene)
{
gameScene.Draw(spriteBatch, gameTime);
}
if(StanMenu.IsShowInfoScene)
{
scenaInformacje.Draw(spriteBatch, gameTime);
}
if (StanMenu.IsShowGameOverScene)
{
gameOverScene.Draw(spriteBatch, gameTime);
}
base.Update(gameTime);
base.Draw(gameTime);
}
}
}
klasa wyboru stanu menu
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GRA
{
class StanMenu
{
private static bool isShowMainMenuScene;
private static bool isShowGameScene;
private static bool isShowGameOverScene;
private static bool isShowInfoScene;
public static bool IsShowMainMenuScene
{
get
{
return isShowMainMenuScene;
}
set
{
if (value == true)
{
isShowInfoScene = false;
isShowGameOverScene = false;
isShowGameScene = false;
}
isShowMainMenuScene = value;
}
}
public static bool IsShowInfoScene
{
get
{
return isShowInfoScene;
}
set
{
if (value == true)
{
isShowMainMenuScene = false;
isShowGameScene = false;
isShowGameOverScene = false;
}
isShowInfoScene = value;
}
}
public static bool IsShowGameScene
{
get => isShowGameScene;
set
{
if (value == true)
{
isShowMainMenuScene = false;
isShowInfoScene = false;
isShowGameOverScene = false;
}
isShowGameScene = value;
}
}
public static bool IsShowGameOverScene
{
get
{
return isShowGameOverScene;
}
set
{
if (value == true)
{
isShowMainMenuScene = false;
isShowInfoScene = false;
isShowGameScene = false;
}
isShowGameOverScene = value;
}
}
}
}
Klasa Sceny Informacje
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace GRA
{
class ScenaInformacje : DrawableGameComponent
{
Game game;
Texture2D returnButtonTexture;
Rectangle returnButtonRectangle;
Color returnButtonColor = Color.White;
MouseState mouseState2;
Rectangle cursorRectangle2;
public ScenaInformacje(Game game) : base(game)
{
this.game = game;
LoadContent();
}
protected override void LoadContent()
{
returnButtonTexture = game.Content.Load<Texture2D>("returnbutton");
}
public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(returnButtonTexture, returnButtonRectangle, returnButtonColor);
spriteBatch.End();
}
public void Update()
{
CalculateItemsSize();
CalculateItemsPositions();
updateCursorPosition();
buttonsEvents();
}
private void CalculateItemsSize()
{
int height = GraphicsDevice.Viewport.Height / 7;
int width = GraphicsDevice.Viewport.Height / 3;
returnButtonRectangle.Height = height;
returnButtonRectangle.Width = width;
}
private void CalculateItemsPositions()
{
returnButtonRectangle.X = GraphicsDevice.Viewport.Width / 2 - returnButtonRectangle.Width / 2; ;
returnButtonRectangle.Y = GraphicsDevice.Viewport.Height/2- returnButtonRectangle.Height/2;
}
private void updateCursorPosition()
{
mouseState2 = Mouse.GetState();
cursorRectangle2.X = mouseState2.X;
cursorRectangle2.Y = mouseState2.Y;
}
private void buttonsEvents()
{
if (returnButtonRectangle.Intersects(cursorRectangle2))
{
returnButtonColor = Color.Green;
if (mouseState2.LeftButton == ButtonState.Pressed)
{
returnButtonColor = Color.Red;
StanMenu.IsShowMainMenuScene = true;
}
}
else
{
returnButtonColor = Color.White;
}
}
}
}
Proszę o pomoc w rozwiązaniu mojego problemu