【问题标题】:I can't make my sprite move我无法让我的精灵移动
【发布时间】:2012-11-08 08:02:46
【问题描述】:

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

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

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

当我的程序运行时,我的精灵会在屏幕上自动弹跳,但它甚至不会从 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);
    }
  }
}

【问题讨论】:

  • int MaxX =添加断点并检查spriteSpeed是什么,gameTime.ElapsedGameTime.TotalSeconds是什么

标签: c# xna sprite


【解决方案1】:

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

您应该重写 Update 方法并从那里调用您的 UpdateSprite 方法...

编辑:

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

protected override void Update(GameTime gameTime)
{
    UpdateSprite(gameTime);
}

【讨论】:

  • 我没有Update方法,只有Update sprite方法
  • 这就是问题所在。游戏循环只会运行一个名为 Update() 的方法。
  • 我该如何解决这个问题,请把我当白痴对待,我从来没有接受过任何正式的教学,而且我对这一切都很陌生。
  • 你应该重写 Update 方法 => protected override Update(Gametime gametime) { UpdateSprite(gametime); } 和你用 draw 方法做的一样
  • 将他给你的代码直接复制并粘贴到游戏文件中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多