C# 如何设置矩形帧的动画?

C# 如何设置矩形帧的动画?,c#,image,xna,frame,C#,Image,Xna,Frame,所以我有一个有两帧的图像,我想把它放在一个矩形中。这个图像将动画化它的帧,并将它们循环-第一帧,第二帧 这是我的密码: //Create the image Texture2D image; //Load, Draw and Update the image in the rectangle //Load Contnet protected override void LoadContent() { // TODO: use this.Content to load your gam

所以我有一个有两帧的图像,我想把它放在一个矩形中。这个图像将动画化它的帧,并将它们循环-第一帧,第二帧

这是我的密码:

//Create the image
Texture2D image;
//Load, Draw and Update the image in the rectangle
//Load Contnet

protected override void LoadContent()
{
     // TODO: use this.Content to load your game content here
     image = Content.Load<Texture2D>("image");
}
protected override void Update(GameTime gameTime)
{
     //Don't know what to write here, but it should make the frames loop
}
protected override void Draw(GameTime gameTime)
{
     //Begin drawing the image
     spriteBatch.Begin();
     spriteBatch.Draw(image, new Rectangle(244, 536, 32, 32), Color.White)
     spriteBatch.End();    
}
//创建图像
纹理2D图像;
//加载、绘制和更新矩形中的图像
//负荷控制网
受保护的覆盖void LoadContent()
{
//TODO:使用此.Content在此处加载游戏内容
image=Content.Load(“image”);
}
受保护覆盖无效更新(游戏时间游戏时间)
{
//我不知道在这里写什么,但它应该会使帧循环
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
//开始绘制图像
spriteBatch.Begin();
spriteBatch.Draw(图像,新矩形(244536,32,32),彩色,白色)
spriteBatch.End();
}

你能告诉我如何填写我的更新方法吗

我建议您查看以下关于
精灵动画的教程

见:


我建议您查看以下有关精灵动画的教程

见:


您可以保留一个运行时间计数器,然后按一定间隔切换。您还需要有多个图像

const int NUM_IMAGES = 2; // or whatever
Texture2D[] images = new Texture2D[NUM_IMAGES];
double elapsedTime = 0;
int currentFrame = 0;
double frameInterval = .5; // the time, in seconds, between frames

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);

    // load all of your images. here, I'm assuming that they are numbered.
    for (int i = 0; i < NUM_IMAGES; i++)
        images[i] = Content.Load<Texture2D>("image" + i);
}
protected override void Update(GameTime gameTime)
{
    // increment elapsed time by the frametime
    elapsedTime += gameTime.ElapsedGameTime.TotalSeconds;
    if (elapsedTime >= frameInterval)
    {
        // reset the elapsed time, and then update the frame counter.
        elapsedTime %= frameInterval;
        currentFrame++;
        if (currentFrame >= NUM_IMAGES)
            currentFrame %= NUM_IMAGES;
    }
}
protected override void Draw(GameTime gameTime)
{
    spriteBatch.Begin();
    // images[currentFrame] to draw the correct frame.
    spriteBatch.Draw(images[currentFrame], new Rectangle(244, 536, 32, 32), Color.White);
    spriteBatch.End();
}
const int NUM_IMAGES=2;//或者别的什么
纹理2d[]图像=新纹理2d[NUM_图像];
双延时时间=0;
int currentFrame=0;
双帧间隔=.5;//帧之间的时间(以秒为单位)
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
//加载你所有的图片。在这里,我假设它们是编号的。
对于(int i=0;i=帧间隔)
{
//重置运行时间,然后更新帧计数器。
elapsedTime%=帧间隔;
currentFrame++;
如果(当前帧>=NUM\u图像)
currentFrame%=NUM_图像;
}
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
spriteBatch.Begin();
//图像[currentFrame]以绘制正确的帧。
spriteBatch.Draw(图像[currentFrame],新矩形(244536,32,32),颜色为白色);
spriteBatch.End();
}

您可以保留一个运行时间计数器,然后按一定间隔切换。您还需要有多个图像

const int NUM_IMAGES = 2; // or whatever
Texture2D[] images = new Texture2D[NUM_IMAGES];
double elapsedTime = 0;
int currentFrame = 0;
double frameInterval = .5; // the time, in seconds, between frames

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);

    // load all of your images. here, I'm assuming that they are numbered.
    for (int i = 0; i < NUM_IMAGES; i++)
        images[i] = Content.Load<Texture2D>("image" + i);
}
protected override void Update(GameTime gameTime)
{
    // increment elapsed time by the frametime
    elapsedTime += gameTime.ElapsedGameTime.TotalSeconds;
    if (elapsedTime >= frameInterval)
    {
        // reset the elapsed time, and then update the frame counter.
        elapsedTime %= frameInterval;
        currentFrame++;
        if (currentFrame >= NUM_IMAGES)
            currentFrame %= NUM_IMAGES;
    }
}
protected override void Draw(GameTime gameTime)
{
    spriteBatch.Begin();
    // images[currentFrame] to draw the correct frame.
    spriteBatch.Draw(images[currentFrame], new Rectangle(244, 536, 32, 32), Color.White);
    spriteBatch.End();
}
const int NUM_IMAGES=2;//或者别的什么
纹理2d[]图像=新纹理2d[NUM_图像];
双延时时间=0;
int currentFrame=0;
双帧间隔=.5;//帧之间的时间(以秒为单位)
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
//加载你所有的图片。在这里,我假设它们是编号的。
对于(int i=0;i=帧间隔)
{
//重置运行时间,然后更新帧计数器。
elapsedTime%=帧间隔;
currentFrame++;
如果(当前帧>=NUM\u图像)
currentFrame%=NUM_图像;
}
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
spriteBatch.Begin();
//图像[currentFrame]以绘制正确的帧。
spriteBatch.Draw(图像[currentFrame],新矩形(244536,32,32),颜色为白色);
spriteBatch.End();
}

在公共游戏1()上面添加了这些变量:

接下来,在
LoadContent()
方法中:

        //Texture load
        texture = Content.Load<Texture2D>("gameGraphics\\Enemies\\Sprite");
        //Increase the timer by the number of milliseconds since update was last called
        timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
        //Check the timer is more than the chosen interval
        if (timer > interval)
        {
            //Show the next frame
            currentFrame++;
            //Reset the timer
            timer = 0f;
        }
        //If we are on the last frame, reset back to the one before the first frame (because currentframe++ is called next so the next frame will be 1!)
        currentFrame = currentFrame % 2;
        sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
        origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
        //Texture Draw
        spriteBatch.Draw(texture, new Vector2(263, 554), sourceRect,Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);
最后是
Draw()
方法:

        //Texture load
        texture = Content.Load<Texture2D>("gameGraphics\\Enemies\\Sprite");
        //Increase the timer by the number of milliseconds since update was last called
        timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
        //Check the timer is more than the chosen interval
        if (timer > interval)
        {
            //Show the next frame
            currentFrame++;
            //Reset the timer
            timer = 0f;
        }
        //If we are on the last frame, reset back to the one before the first frame (because currentframe++ is called next so the next frame will be 1!)
        currentFrame = currentFrame % 2;
        sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
        origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
        //Texture Draw
        spriteBatch.Draw(texture, new Vector2(263, 554), sourceRect,Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);

注意:这是适合我的解决方案。。。如果有人觉得这个答案很有用,他们应该知道他们的精灵表的帧必须从左到右对齐,否则动画将无法工作。

public Game1()上面添加了这些变量。

接下来,在
LoadContent()
方法中:

        //Texture load
        texture = Content.Load<Texture2D>("gameGraphics\\Enemies\\Sprite");
        //Increase the timer by the number of milliseconds since update was last called
        timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
        //Check the timer is more than the chosen interval
        if (timer > interval)
        {
            //Show the next frame
            currentFrame++;
            //Reset the timer
            timer = 0f;
        }
        //If we are on the last frame, reset back to the one before the first frame (because currentframe++ is called next so the next frame will be 1!)
        currentFrame = currentFrame % 2;
        sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
        origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
        //Texture Draw
        spriteBatch.Draw(texture, new Vector2(263, 554), sourceRect,Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);
最后是
Draw()
方法:

        //Texture load
        texture = Content.Load<Texture2D>("gameGraphics\\Enemies\\Sprite");
        //Increase the timer by the number of milliseconds since update was last called
        timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
        //Check the timer is more than the chosen interval
        if (timer > interval)
        {
            //Show the next frame
            currentFrame++;
            //Reset the timer
            timer = 0f;
        }
        //If we are on the last frame, reset back to the one before the first frame (because currentframe++ is called next so the next frame will be 1!)
        currentFrame = currentFrame % 2;
        sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
        origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
        //Texture Draw
        spriteBatch.Draw(texture, new Vector2(263, 554), sourceRect,Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);

注意:这是适合我的解决方案。。。如果有人觉得这个答案有用,他们应该知道,他们的精灵表的帧必须从左到右对齐,否则动画将无法工作。

我相信您将需要多个图像。然后你可以反复浏览这些图片。我相信你需要多张图片。在Update()方法中,elapsedTime给出“检测到不合理的代码”警告。怎么办?@PowerUser你确定我给你的代码中有警告吗?因为这根本没有任何意义。@PowerUser这是什么解决方案?在Update()方法中,elapsedTime给出“检测到不合理的代码”警告。怎么办?@PowerUser你确定我给你的代码中有警告吗?因为这毫无意义。@PowerUser这个解决方案是什么?