Experiencing frame drops (Draw methods fault?)

Hi!

I’m developing my first mobile game using monogame.
I have developed in XNA(making windows games) before, so i am familiar with the API’s.

The problem is: At random times i get a frame drop(there’s between 6-50 sprites bouncing on the screen). It can run smoothly for like 10 seconds, before the frame drops for like 1 second before it runs smoothly again.

Got a simple game with a sprite class which contains every property a sprite needs (texture, position, velocity, frameSize etc). In this sprite class i got the draw method:

public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Draw(
texture,
position,
new Rectangle(frameCurrent.X * frameSize.X, frameCurrent.Y * frameSize.Y, frameSize.X, frameSize.Y),
Color.White, rotate, origin,
scale, spriteEffects, 0);
}

In the game class i got a list of sprites(some of them with different textures). In the game Draw() i got the following code (_heads is the list of the sprites):

public void Draw(GameTime gameTime)
{
game.GraphicsDevice.Clear(Color.Black);

        spriteBatch.Begin();
        spriteBatch.Draw(_background,_mainFrame, Color.White);
         foreach (var head in _heads)
        {
            head.Draw(gameTime, spriteBatch);
        }
        spriteBatch.DrawString(font, tekst, new Vector2(3, 15), Color.Red, 0f, Vector2.Zero, 3f, SpriteEffects.None, 0f);
        spriteBatch.End();
    }

The movement of the sprites is computed in the following way:

if (whichDirection == 1)
{
position.X = position.X - velocity.X;
position.Y = position.Y + velocity.Y;
}
else if (whichDirection == 2)
{
position.X = position.X + velocity.X;
position.Y = position.Y + velocity.Y;
}
else if (whichDirection == 3)
{
position.X = position.X + velocity.X;
position.Y = position.Y - velocity.Y;
}
else
{
position.X = position.X - velocity.X;
position.Y = position.Y - velocity.Y;
}

Can it be because i’m doing per-pixel collision detection?:

touches = TouchPanel.GetState();
            for (int i = 0; i < _heads.Count; i++)
            {
                foreach (TouchLocation tl in touches)
                {
                    if (tl.State == TouchLocationState.Pressed)
                    {
                        Rectangle rect = new Rectangle((int)tl.Position.X, (int)tl.Position.Y, 5, 5);
                        if (_heads[_heads.Count - (1 + i)].CollisionRectangle.Intersects(rect))
                        {
                                            //Code her
                        }
                 }
            }

I am testing on a Samsung Galaxy S4
All of the code is in the PCL(Portable Class Library) project, which my Android and IOS project is using.

Its offcourse crucial for the game to run smoothly, is there anyone who got any tips of what the problem can be and how to avoid it? I would be forever grateful:)

Friendly, Kent.

“It can run smoothly for like 10 seconds, before the frame drops for like 1 second before it runs smoothly again.”

It sounds like garbage collection, it is a good idea to do some profiling to see where your code is generating that garbage.

Hmm, i am used to C# having automatic garbage collection so got no experience with profiling or handling it manually.

What can be possible reasons in the code for c# gargbage collection not being able to handle the garbage?

Thanks for replying :slight_smile: