Not increasing Score integer
Hello, I'm creating a rhythm game using XNA. The problem is the
Score
increasing by value of earned score then to resets to 0
. Finally score is 0 too so, i don't have ideas what to do
C#
public void Update(GameTime gameTime)
{
if (!isInitialized) Initialize(game);
List<GameplayObject> objectsToRemove = new List<GameplayObject>();
soundEngine.Update();
Note lastNote = (Note)gameplayObjects.FindLast(gameplayObject =>
{
return gameplayObject is Note note;
});
if ((int)gameTime.TotalGameTime.TotalMilliseconds == lastNote.Time + 2000)
{
game.SwitchToState(RhythmicalState.Score);
}
foreach (GameplayObject gameplayObject in gameplayObjects.ToList())
{
if (gameplayObject is Note note)
{
if (pianoEndRectangle.Intersects(note.rectangle))
{
if (isTouched)
{
objectsToRemove.Add(note);
if (note.rectangle.Y >= 530)
{
Score += 100; // Here is problem
}
else if (note.rectangle.Y <= 530 && note.rectangle.Y >= 510)
{
Score += 50;
}
isTouched = false;
}
}
}
}
foreach (var obj in objectsToRemove.ToList())
{
gameplayObjects.Remove(obj);
}
}
C#
public void Update(GameTime gameTime)
{
if (!isInitialized) Initialize(game);
List<GameplayObject> objectsToRemove = new List<GameplayObject>();
soundEngine.Update();
Note lastNote = (Note)gameplayObjects.FindLast(gameplayObject =>
{
return gameplayObject is Note note;
});
if ((int)gameTime.TotalGameTime.TotalMilliseconds == lastNote.Time + 2000)
{
game.SwitchToState(RhythmicalState.Score);
}
foreach (GameplayObject gameplayObject in gameplayObjects.ToList())
{
if (gameplayObject is Note note)
{
if (pianoEndRectangle.Intersects(note.rectangle))
{
if (isTouched)
{
objectsToRemove.Add(note);
if (note.rectangle.Y >= 530)
{
Score += 100; // Here is problem
}
else if (note.rectangle.Y <= 530 && note.rectangle.Y >= 510)
{
Score += 50;
}
isTouched = false;
}
}
}
}
foreach (var obj in objectsToRemove.ToList())
{
gameplayObjects.Remove(obj);
}
}
6 Replies
Where's the
Score
variable declared?in the class Gameplay
And what is that class? Is the method you posted inside of that class?
Does anything else change the
Score
variable?
Does an instance of Gameplay
get destroyed or replaced at any point?nope
nothing
here is code for beginning of the class
C#
public class Gameplay
{
private Game1 game;
private GraphicsDevice graphicsDevice;
public ContentManager contentManager;
private Texture2D mainMenuBackground;
private Texture2D pianoTexture;
public Texture2D pianoEndTexture;
public Rectangle pianoEndRectangle;
public Texture2D NoteTexture;
public Texture2D SliderTexture;
private static List<GameplayObject> gameplayObjects;
private SpriteFont font;
private Parser parser = new Parser();
private LowLevelKeyboardListener keyboardListener;
private static SoundEngineXact soundEngine;
private string beatmapFile;
private SpriteBatch spriteBatch;
private bool isInitialized;
private static bool isTouched;
private int Score;
public Gameplay(ContentManager contentManager, Game1 game, Texture2D noteTexture, Texture2D sliderTexture, string beatmapFile)
{
this.contentManager = contentManager;
this.game = game;
this.NoteTexture = noteTexture;
this.SliderTexture = sliderTexture;
this.beatmapFile = beatmapFile;
isTouched = false;
LoadContent(contentManager, spriteBatch);
Initialize(game);
}
C#
public class Gameplay
{
private Game1 game;
private GraphicsDevice graphicsDevice;
public ContentManager contentManager;
private Texture2D mainMenuBackground;
private Texture2D pianoTexture;
public Texture2D pianoEndTexture;
public Rectangle pianoEndRectangle;
public Texture2D NoteTexture;
public Texture2D SliderTexture;
private static List<GameplayObject> gameplayObjects;
private SpriteFont font;
private Parser parser = new Parser();
private LowLevelKeyboardListener keyboardListener;
private static SoundEngineXact soundEngine;
private string beatmapFile;
private SpriteBatch spriteBatch;
private bool isInitialized;
private static bool isTouched;
private int Score;
public Gameplay(ContentManager contentManager, Game1 game, Texture2D noteTexture, Texture2D sliderTexture, string beatmapFile)
{
this.contentManager = contentManager;
this.game = game;
this.NoteTexture = noteTexture;
this.SliderTexture = sliderTexture;
this.beatmapFile = beatmapFile;
isTouched = false;
LoadContent(contentManager, spriteBatch);
Initialize(game);
}
Well, best advice I can give right now is to just debug this code
See when exactly the variable gets zeroed out
Update: moving variable to main File (Game1.cs) and make it static helped