0

我是 XNA 的新手,目前正在学习本教程:

http://msdn.microsoft.com/en-us/library/bb203893.aspx

完成第五步后运行程序时会出现问题。我不认为这是一个错字,因为其他人在评论中指出了同样的问题。

当我的程序运行时,我的精灵会在屏幕上自动弹跳,但是它甚至不会从 0,0 移动一个像素。

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }


    protected override void Initialize()
    {


        base.Initialize();
    }

    Texture2D myTexture;

    Vector2 spritePosition = Vector2.Zero;

    Vector2 spriteSpeed = new Vector2(50.0f, 50.0f);

    protected override void LoadContent()
    {

        spriteBatch = new SpriteBatch(GraphicsDevice);
        myTexture = Content.Load<Texture2D>("Main");

    }


    protected override void UnloadContent()
    {

    }


     void UpdateSprite(GameTime gameTime)
    {
        //Move the sprite by speed, scaled by elapsed time

         spritePosition +=
            spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

        int MaxX =
            graphics.GraphicsDevice.Viewport.Width - myTexture.Width;
         int MinX = 0;

         int MaxY =
             graphics.GraphicsDevice.Viewport.Height - myTexture.Height;
         int MinY = 0;

         //Check for bounce.
         if (spritePosition.X > MaxX)
         {
             spriteSpeed.X *= -1;
             spritePosition.X = MaxX;

         }

         else if (spritePosition.X < MinX)
         {
             spriteSpeed.X *= -1;
             spritePosition.X = MinX;

         }

         if (spritePosition.Y > MaxY)
         {
             spriteSpeed.Y *= -1;
             spritePosition.Y = MaxY;
         }

         else if (spritePosition.Y < MinY)
         {
             spriteSpeed.Y *= -1;
             spritePosition.Y = MinY;
         }


    }


    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);



        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        spriteBatch.Draw(myTexture, spritePosition, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
  }
}
4

1 回答 1

4

不会从任何地方调用 UpdateSprite 方法...

您应该覆盖 Update 方法并从那里调用您的 UpdateSprite 方法...

编辑:

将此添加到您的游戏类中:

protected override void Update(GameTime gameTime)
{
    UpdateSprite(gameTime);
}
于 2012-11-07T22:55:48.897 回答