0

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

这是我的代码:

//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();    
}

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

4

3 回答 3

3

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

    //Sprite Texture
    Texture2D texture;
    //A Timer variable
    float timer = 0f;
    //The interval (300 milliseconds)
    float interval = 300f;
    //Current frame holder (start at 1)
    int currentFrame = 1;
    //Width of a single sprite image, not the whole Sprite Sheet
    int spriteWidth = 32;
    //Height of a single sprite image, not the whole Sprite Sheet
    int spriteHeight = 32;
    //A rectangle to store which 'frame' is currently being shown
    Rectangle sourceRect;
    //The centre of the current 'frame'
    Vector2 origin;

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

        //Texture load
        texture = Content.Load<Texture2D>("gameGraphics\\Enemies\\Sprite");

Update()方法:

        //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);

最后,Draw()方法:

        //Texture Draw
        spriteBatch.Draw(texture, new Vector2(263, 554), sourceRect,Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);

注意:这是对我有用的解决方案......如果有人觉得这个答案有用,他们应该知道他们的 spritesheet 的帧必须从左到右对齐否则动画将不起作用。

于 2013-08-05T16:47:38.133 回答
1

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

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();
}
于 2013-07-31T19:58:49.443 回答
0

我建议你看看下面的教程sprite animation

看:

于 2013-07-31T19:47:34.887 回答