C
C#4w ago
eliza

Weird behaviour with SpriteBatch.Draw in MonoGame

I'm creating a rhythm game. I'm trying to move automatically Notes from upside but something went wrong. Notes just stuck. Below is code, thanks in advance for help because I don't understand what I'm did bad.
c#
public void Update(GameTime gameTime)
{
if (!isInitialized) Initialize(game);

soundEngine.Update();

foreach (var gameplayObject in gameplayObjects)
{
gameplayObject.Update(gameTime, spriteBatch, contentManager);
}
}

public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Begin();

mainMenuBackground = contentManager.Load<Texture2D>("Background");
pianoTexture = contentManager.Load<Texture2D>("Piano");

spriteBatch.Draw(mainMenuBackground, Vector2.Zero, Color.White);
spriteBatch.Draw(pianoTexture, new Vector2(100, 0), Color.White);

foreach (GameplayObject gameplayObject in gameplayObjects)
{
gameplayObject.Draw(spriteBatch, contentManager);
}

spriteBatch.End();
}
c#
public void Update(GameTime gameTime)
{
if (!isInitialized) Initialize(game);

soundEngine.Update();

foreach (var gameplayObject in gameplayObjects)
{
gameplayObject.Update(gameTime, spriteBatch, contentManager);
}
}

public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Begin();

mainMenuBackground = contentManager.Load<Texture2D>("Background");
pianoTexture = contentManager.Load<Texture2D>("Piano");

spriteBatch.Draw(mainMenuBackground, Vector2.Zero, Color.White);
spriteBatch.Draw(pianoTexture, new Vector2(100, 0), Color.White);

foreach (GameplayObject gameplayObject in gameplayObjects)
{
gameplayObject.Draw(spriteBatch, contentManager);
}

spriteBatch.End();
}
Note.cs:
c#
namespace Rhythmical.GameModes.Rhythmical.Tiles
{
public class Note : GameplayObject
{
public float FallSpeed = 50.0f;
private int x;

public Note(int time, int x, int position) : base(time, position)
{
this.Position = position;
this.x = x;
}

public override void Update(GameTime gameTime, SpriteBatch spriteBatch, ContentManager contentManager)
{
float time = (float)(gameTime.TotalGameTime.TotalSeconds);
Position = 100.0f + (float)Math.Sin(time) * 100;
}

public override void Draw(SpriteBatch spriteBatch, ContentManager contentManager)
{
Texture2D noteTexture = contentManager.Load<Texture2D>("NoteTexture");
spriteBatch.Draw(noteTexture, new Vector2(x, Position), Color.White);
}
}
}
c#
namespace Rhythmical.GameModes.Rhythmical.Tiles
{
public class Note : GameplayObject
{
public float FallSpeed = 50.0f;
private int x;

public Note(int time, int x, int position) : base(time, position)
{
this.Position = position;
this.x = x;
}

public override void Update(GameTime gameTime, SpriteBatch spriteBatch, ContentManager contentManager)
{
float time = (float)(gameTime.TotalGameTime.TotalSeconds);
Position = 100.0f + (float)Math.Sin(time) * 100;
}

public override void Draw(SpriteBatch spriteBatch, ContentManager contentManager)
{
Texture2D noteTexture = contentManager.Load<Texture2D>("NoteTexture");
spriteBatch.Draw(noteTexture, new Vector2(x, Position), Color.White);
}
}
}
No description
2 Replies
eliza
eliza4w ago
They should move to down
poffertjesporum
Hi! I'm not a coder chad, but I'll try and help even though I may be totally wrong. Since the notes do show up. And they don't disappear it means that they are drawn to screen each frame. Given this I'd say the problem is in your update loop, where you set the position of the notes. Position = 100.0f + (float)Math.Sin(time) * 100; What I think this line does is: Set the y position of the note to the result of that equation but since the equation bases of off a sine wave, it doesn't really go anywhere. (Might be moving but sorta vibrating) And you use seconds so it would update each second, might take a while before you see some actual displacement. I'd say try a really basic thing like Position += 5; This adds 5 to the y value each update. And just as a little extra, I'd suggest putting Texture2D noteTexture = contentManager.Load<Texture2D>("NoteTexture"); In the constructor, you really only need to set a texture once. Setting it multiple times like this is a waste of resources.