2

我正在使用此代码,但我仍然不明白为什么我开始游戏时球是模糊的,有人可以帮我吗??我只是在用户点击时试图给我球但我不知道为什么它是模糊的,我还在检查代码,请帮助

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 Critters
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D ball;


        Vector2 playerPosition;
        Vector2 direction;
        Vector2 destination;        
        float speed;

        float playerPosX;
        float playerPosY;


        MouseState oldState;

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

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            graphics.PreferredBackBufferWidth = 700;
            graphics.PreferredBackBufferHeight = 500;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
            Window.Title = "Darrells Game";


            playerPosY = graphics.GraphicsDevice.Viewport.Height / 10 * 8;
            playerPosX = graphics.GraphicsDevice.Viewport.Width / 2;
            playerPosition = new Vector2(playerPosX, playerPosY);
            destination = playerPosition;


            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);



            ball = Content.Load<Texture2D>("orb");

            // TODO: use this.Content to load your game content her
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }


        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();


            if (Vector2.DistanceSquared(destination, playerPosition) >= speed * speed)
            {
                MovePlayer();
            }
            else
            {
                playerPosition = destination;
            }



            MouseState leftState = Mouse.GetState();
            MouseState rightState = Mouse.GetState();             

            if ((leftState.LeftButton == ButtonState.Pressed && rightState.RightButton == ButtonState.Pressed))
            {
                int mouseX = leftState.X;
                int mouseY = leftState.Y;
                destination = new Vector2(mouseX, mouseY);
                speed = 8.0f;

            }

            else if (leftState.LeftButton == ButtonState.Pressed)
            {
                int mouseX = leftState.X;
                int mouseY = leftState.Y;
                destination = new Vector2(mouseX, mouseY);
                speed = 2.0f;
            }


            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);


            spriteBatch.Begin();
            DrawPlayer();
            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }

        private void DrawPlayer()
        {

            Vector2 textureCentre = new Vector2(ball.Width / 2, ball.Height / 2);

            spriteBatch.Draw(ball, playerPosition, null, Color.White, 0f, textureCentre, 0.8f, SpriteEffects.None, 1f);

        }

        private void MovePlayer()
        {

            {
                direction = destination - playerPosition;
                direction.Normalize();
                playerPosition += direction * speed;
            }

        }


    }
}
4

1 回答 1

5

在您的抽奖电话中:

spriteBatch.Draw
(
   ball,                   // texture
   playerPosition,         // location
   null,                   // source rect
   Color.White,            // color
   0f,                     // rotation
   textureCentre,          // origin
   0.8f,                   // scale
   SpriteEffects.None,     // effects
   1f                      // layerDepth
);

您已将比例设置为0.8f。这将使您的纹理比实际更小,这将涉及一些“模糊”。将比例值更改为1f应该消除由缩放引起的任何模糊。

编辑:如果您所指的模糊是在您单击之前发生的闪烁,那是由于目的地与当前位置完全相同。然后,您的MovePlayer方法会导致当前位置变为(NaN, NaN)

// before the user has clicked, destination == playerPosition
private void MovePlayer()
{
   direction = destination - playerPosition;  // direction = (0, 0)
   direction.Normalize();                     // direction = (NaN, NaN)
   playerPosition += direction * speed;       // playerPosition = (NaN, NaN)
}

快速解决此问题的方法是在您的Initialize方法中,更改行:

protected override void Initialize()
{
   ...

   // change this
   mDestination = mPlayerPosition;

   // to this
   mDestination = mPlayerPosition + new Vector2(1, 1);
于 2012-04-16T01:27:48.650 回答