gpt4 book ai didi

c# - 使用 for 循环 C# 制作动画的多个图像

转载 作者:行者123 更新时间:2023-11-30 16:53:29 25 4
gpt4 key购买 nike

我正在尝试使用一个简单的管理器制作一个基本的、非常简单的动画功能。问题是,截至目前,当用户在主窗体上单击“开始”时,它将显示第一张图片,然后跳转到最后一张图片。

我还尝试插入一个延迟,将事情从 1 延迟到最后一帧,但它只是跳到最后一帧。我正在使用几个 PNG 文件来使这个东西工作,而不是尝试使用“sprite 表”,这将是我想象中的一个更大的痛苦...

Here's 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;
}
}

}
}


}
}

这是我所能得到的。如果有人有任何见解或能为我指明正确的方向以使其发挥作用,那就太棒了。

最佳答案

你的代码有一些问题,所以我重新写了一遍,我把你有问题的地方注释掉了

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
}
}
}

关于c# - 使用 for 循环 C# 制作动画的多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31362980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com