Morsus Morsus - 3 months ago 29
C# Question

C# .net picturebox reset

I am making a simple game and I'm stuck with character rotation.
I made a simple control with arrow keys but without rotation.
Then I used the RotateFlipType function to get my character rotate but he kept rotating on the same key. So I need to make the picturebox image reset every time a different key is pressed and then rotate it in right direction.

void Rotate(string a)
{
switch (a)
{
case "up":
if (rotation != "up")
{
rotation = "up";
pb_sprite.ImageLocation = null;
pb_sprite.ImageLocation = @"Images/tenk.png";
}
break;
case "down":
if (rotation != "down")
{
rotation = "down";
pb_sprite.ImageLocation = null;
pb_sprite.ImageLocation = @"Images/tenk.png";
pb_sprite.Image.RotateFlip((RotateFlipType.Rotate180FlipNone));
}
break;
}
}


The "up" case works fine. But I do get an error when the code comes to rotation.

System.NullReferenceException


I get it that I removed the location. But I put it back (for resetting the rotation). Why does the compiler still think there is no image?
Thank you.

Answer

Try

case "down":
            if (rotation != "down")
            {
                rotation = "down";
                pb_sprite.ImageLocation = null;
                pb_sprite.ImageLocation = @"Images/tenk.png";
                pb_sprite.Load();
                pb_sprite.Image.RotateFlip((RotateFlipType.Rotate180FlipNone));
            }
            break;
Comments