Multiple Images for Animation Usage for C # Loop

I am trying to make a rudimentary, very simple animation function using a simple manager. The problem is, right now, when the user clicks "Go" on the main form, they display the first image and then navigate to the last image.

I also tried to insert a delay that delays the subject going from 1 to the last frame, but it just jumps to the last frame. I'm using a few PNG files to make this work instead of trying to use a "sprite sheet" which would be a pain in the ass. I would like to...

Here the code for the AnimationManager class...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace Animation
{
public class AnimationManager
{

    public PictureBox AnimationBox;
    public float widthX, heightY;
    public string ImageDirectory, ImageName;
    public int frame, maxFrames;
    public int speed; // in milliseconds
    public bool isLooping;


    public AnimationManager()
    {
        AnimationBox = new PictureBox();
        widthX = 0;
        heightY = 0;
        ImageName = null;
        frame = 1;
        speed = 0;
        maxFrames = 0;
        isLooping = false;
    }

    public async void CycleThrough(string ImageDirectory, int FPMS, int maxFrames, bool isLooping)
    {
        for (int i = frame; i < maxFrames + 1; i++)
        {
            ImageName = ImageDirectory + @"\" + frame.ToString() + ".png";
            AnimationBox.Image = Image.FromFile(ImageName);
            AnimationBox.Refresh();
            await Task.Delay(FPMS);
            switch (isLooping)
            {
                case false:
                default:
                    {
                        frame = maxFrames;
                        break;
                    }
                case true:
                    {
                        frame = 1;
                        break;
                    }
            }

        }
    }


}
}

      

What I was able to get. If anyone knows or can point me in the right direction to get this to work, that would be awesome.

+3


source to share


2 answers


You had some problems with the code, so I am writing it again and I will comment where you had the problem.



public async void CycleThrough(string ImageDirectory, int FPS, int maxFrames, bool isLooping)
{
    for (int i = frame; i < maxFrames + 1; i++) // frame and maxFrames must not change. counter is i
    {
        ImageName = ImageDirectory + @"\" + i.ToString() + ".png"; // Get the i-th png using counter.
        AnimationBox.Image = Image.FromFile(ImageName);
        AnimationBox.Refresh();
        await Task.Delay(TimeSpan.FromSeconds(1/FPS)); // delay in seconds.
        if(isLooping && i == maxframes) // if must Loop and counter is in last iteration then start over
        {
            i = frame - 1; // set the counter to the first frame
        }
    }
}

      

+2


source


While waiting for an answer, I went and tried to do it myself ... :) Thanks for your responses and comments ...

This was my final decision ...

private async void CycleThrough(string ImageDirectory, int FPMS, int maxFrames, bool isLooping)
    {
        this.speed = FPMS;
        for (int i = frame; i < maxFrames + 1; i++)
        {
            ImageName = ImageDirectory + @"\" + i.ToString() + ".png";
            Console.WriteLine(frame.ToString());
            AnimationBox.Refresh();
            AnimationBox.Image = Image.FromFile(ImageName);
            await Task.Delay(FPMS);
            if (isLooping)
            {
                if (i >= maxFrames) i = 1;
            }
        }
    }

      

I made a basic mistake and didn't use the "i" that was configured. Instead, I was manipulating a bunch of other things that had nothing to do with the loop itself.



I agree with the person above - I didn't use a switch in my solution. It didn't make sense since I only have one condition to reset the loop (manipulating I went back to 1) if isLooping was true.

This works great, so I offer this solution to anyone who wants to create a rudimentary animator for their code :)

Thanks everyone for your answers and time.

0


source







All Articles