Ich habe leider wieder ein kleines Problem. Ich möchte ein kleines Auswahlmenü(RowState) machen das man während des Hauptspiels aufrufen/einblenden kann. Wenn man in diesem Auswahlmenü eine Einstellung wählt, hat das Einfluss auf das Spielgeschehen. Wichtig dabei ist, wenn man etwas auswählt und das Auswahlmenü danach ausblendet, dann muss beim nächsten Einblenden des Auswahlmenüs die Einstellung noch ausgewählt sein.
Das ganze hat bei mir fast funktioniert. Das einzige was nicht funktioniert hat war dass die Einstellungen im Auswahlmenü nicht gespeichert blieben wenn man das Auswahlmenü wieder aufgerufen/eingeblendet hat. Ich bin mir sicher dass es dáran lag dass jedes Mal folgender Block in Game1 aufgerufen wurde wenn man das Auswahlmenü aufgerufen hat:
|
C#-Quelltext
|
1
2
3
4
|
case GameStates.RowState:
currentState = new Reihen(this);
currentGameState = GameStates.RowState;
break;
|
Deshalb habe ich meinen Code in der Maingame-Klasse ein wenig geändert, jedoch funktioniert es nicht. Wenn ich die Taste Space im Hauptspiel drücke passiert nichts, das Auswahlmenü erscheint gar nicht. Komischerweise wird aber ein Breakpoint in der Load-Methode der Auswahlmenü-Klasse ausgelöst. Dabei stehen CurrentState/currentGameState auf RowState und LastState/lastGameState auf MaingameState was ja richtig ist.
Obwohl ich den Code schon mehrmals mit Breakpoints durchgegangen bin, kann ich keinen Fehler finden
Was ist da nur falsch
Das erste Mal wird das Auswahlmenü mit dieser Zeile "game1.ChangeGameState(Game1.GameStates.RowState);" aufgerufen und die Variable erstesMal auf false gesetzt. Danach soll man nur noch mit der Methode ChangeToLastandCurrent(); vom Hauptspiel(MaingameState) ins Auswahlmenü(RowState) und umgekehrt wechseln können. Das Hauptspiel läuft immer im Hintergrund weiter wenn man im Auswahlmenü ist.
Game1-Klasse:
|
C#-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
IState lastState, currentState, savelastState;
public enum GameStates
{
IntroState = 0,
MenuState = 1,
MaingameState = 2,
IngamemenuState = 3,
GameoverState = 4,
RowState = 5
}
public void ChangeGameState(GameStates newState)
{
if (newState == currentGameState)
return;
lastGameState = currentGameState;
lastState = currentState;
switch (newState)
{
case GameStates.IntroState:
currentState = new Intro(this);
currentGameState = GameStates.IntroState;
break;
case GameStates.MenuState:
currentState = new Menu(this);
currentGameState = GameStates.MenuState;
break;
case GameStates.MaingameState:
currentState = new Maingame(this);
currentGameState = GameStates.MaingameState;
break;
case GameStates.IngamemenuState:
currentState = new Ingamemenu(this);
currentGameState = GameStates.IngamemenuState;
break;
case GameStates.GameoverState:
currentState = new Gameover(this);
currentGameState = GameStates.GameoverState;
break;
case GameStates.RowState:
currentState = new Reihen(this);
currentGameState = GameStates.RowState;
break;
}
currentState.Load(Content);
}
public void ChangeToLastandCurrent()
{
savelastGameState = currentGameState;
currentGameState = lastGameState;
lastGameState = savelastGameState;
savelastState = currentState;
currentState = lastState;
lastState = savelastState;
}
public GameStates CurrentState
{
get { return currentGameState; }
set { currentGameState = value; }
}
public GameStates LastState
{
get { return lastGameState; }
set { lastGameState = value; }
}
private GameStates currentGameState;
private GameStates lastGameState;
private GameStates savelastGameState;
public KeyboardState lastkeyState;
public KeyboardState currentkeyState = Keyboard.GetState();
public bool paused = false;
private bool pauseKeyDown = false;
private void BeginPause(bool UserInitiated)
{
paused = true;
ChangeGameState(GameStates.IngamemenuState);
}
private void EndPause()
{
paused = false;
ChangeToLastandCurrent();
}
public void checkPauseKey(KeyboardState keyboardState)
{
bool pauseKeyDownThisFrame = (keyboardState.IsKeyDown(Keys.Escape));
// If key was not down before, but is down now, we toggle the
// pause setting
if (!pauseKeyDown && pauseKeyDownThisFrame)
{
if (!paused)
BeginPause(true);
else
EndPause();
}
pauseKeyDown = pauseKeyDownThisFrame;
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
currentState = new Intro(this);
currentGameState = GameStates.IntroState;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
currentState.Load(Content);
}
protected override void Update(GameTime gameTime)
{
lastkeyState = currentkeyState;
currentkeyState = Keyboard.GetState();
currentState.Update(gameTime);
if ((lastGameState == GameStates.MaingameState) && (currentGameState == GameStates.IngamemenuState))
{
lastState.Update(gameTime);
}
if ((lastGameState == GameStates.MaingameState) && (currentGameState == GameStates.RowState))
{
lastState.Update(gameTime);
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
if ((lastGameState == GameStates.MaingameState) && (currentGameState == GameStates.IngamemenuState))
{
lastState.Render(spriteBatch);
}
if ((lastGameState == GameStates.MaingameState) && (currentGameState == GameStates.RowState))
{
lastState.Render(spriteBatch);
}
currentState.Render(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
|
Maingame-Klasse:
|
C#-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
public class Maingame : IState
{
Texture2D Spielbildschirm, axe;
Vector2 position = new Vector2(0,0);
bool erstesMal = true;
private Game1 game1;
public Maingame(Game1 game)
{
game1 = game;
}
public void Load(ContentManager content)
{
Spielbildschirm = content.Load<Texture2D>("hauszombie");
axe = content.Load<Texture2D>("axxx");
}
public void Update(GameTime gametime)
{
// KeyboardState keyboardState = Keyboard.GetState();
// Check to see if the user has paused or unpaused
// If the user hasn't paused, Update normally
if (!game1.paused)
{
float delta = (float)gametime.ElapsedGameTime.TotalSeconds;
position.X += 5 * delta;
position.Y += 3 * delta;
if (game1.currentkeyState.IsKeyDown(Keys.Space) && game1.lastkeyState.IsKeyUp(Keys.Space))
{
if (erstesMal == false)
game1.ChangeToLastandCurrent();
else
{
game1.ChangeGameState(Game1.GameStates.RowState);
erstesMal = false;
}
}
game1.checkPauseKey(game1.currentkeyState);
}
}
public void Render(SpriteBatch batch)
{
batch.Draw(Spielbildschirm, new Rectangle(0, 0, 1280, 720), Color.White);
batch.Draw(axe, position, Color.White);
}
}
|