Search code examples
c#debugginganimationmonogame

MonoGame - calling the same function from different classes result in different behavior


I'm working a game using XNA/MonoGame, and I'm stuck with a bug.

I have this function Expload, that show a very simple animation using a sprite sheet.

public void Exploade(Rectangle explosionRect)
    {
        ex = new ExpolsionAnimation(content, "Explsion4", explosionRect,
            _frameWaiteTime:50,
            _frameWidth:90 , _frameHeight: 45,
            _rows: 4       , _culloms: 3,
            _rotation: 0
            );

        playAnimation = true;
    }

I call this function from 2 classes:

  • A class that function was made in, called Arrow
  • A different class, that call currentArrow.Explode (and work fine) called OnCollsionEvents

here is how i call the function in the different classes: in class Arrow (which giving me the problem/bug:

 public override void Update(GameTime gt)
    {
        if (!MonsterHit)
        {
            this.location.Y -= movmentSpeed;
        }


 //when the arrow touch the top of the window, it need to expload, but for some reason when i start the animation here, it just doesn't work.

       if (ReachedTheTop)
            this.Exploade(new Rectangle(location.X - 10, 1, 30, 30));

       //here i start updating the animation
        if (playAnimation)
            ex.Update(gt);

        base.Update(gt);
    }

in class OnCollsionEvents

public void Update(SpaceShip _spaceShip, Monsters _monsters)
{
    List<Arrow> gameArrows = player.Gun.ArrowList;

    if (MonsterCount > 0)
    {
            foreach (var monster in monsters.MonstersList)
            {

                if (monster.ReachedTheButtom)
                {
                    MonsterHitTheFloor = true;
                }

                if (monster.CollsionWith(player))
                {
                    MonsterHitTheSpaceship = true;
                }

                foreach (var currentArrow in gameArrows)
                {
                if (currentArrow.CollsionWith(monster))
                {
                    currentArrow.MonsterHit = true;
                    currentArrow.Exploade(new Rectangle(monster.Location.X, monster.Location.Y, monster.AreaRect.Width, monster.AreaRect.Height));

                    monster.GotHit = true;
                }
            }
    }
}

here is where Arrow.Update is called (in class Gun)

      public override void Update(GameTime gt)
        {
            if (arrowsNotEmpty)
            {
                for (int i = 0; i < ArrowList.Count; i++)
                {
                    ArrowList[i].Update(gt);
//because of the bug, i cant even remove an arrow afther it reach the top of the window
//because the animation is not Ended.
                    if ((ArrowList[i].IsOverTheTop || ArrowList[i].MonsterHit) && ArrowList[i].AnimationEnded)
                        ArrowList.RemoveAt(i);
                }
            }
    }

now the problem i'm having is like this. in the animation class i have a integer that get the milliseconds pass, so i can create a sort of a timer to run the animation with. when i call the function Exploade, and start updating the animation in OnCollsionEvents class, the integer value is 64 and start the animation correctly.

when i call the Exploade from Arrow and start updating, the integer value is 16, which doesn't start the animation.

what i don't understand is, how its possible to call the same function, from different class, and update the function in the same class it was created, and get different results.

  • what did i miss?
  • or where should i try looking?
  • what is a good way to test this sort of stuff?

Solution

  • I found my problem.

    when the arrow got to the top of the screen, it kept making explostions. so i just add another if statment, to check if there isnt explonstion animation allrdy runing.