2012-04-15 41 views
2

我正在使用此代码,但我仍然不知道为什么当我开始游戏时球是模糊的,有人可以帮助我吗??当用户点击时我只是想向我发球,但我不知道为什么它是模糊的,我还是cheking代码,请帮忙当我在xna 4.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 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; 
      } 

     } 


    } 
} 
+0

只是注意:不要打扰制作playerPosX和playerPosY浮点变量。直接设置playerPosition.X和playerPosition.Y即可。您还可以将速度和方向组合为一个Velocity向量,其方向为'方向',长度为'速度'。这些只是减少代码混乱的小事情。除了已经解决的尺度问题之外,我找不到任何其他方法,这会使问题变得模糊。也许提供一些图像? – 2012-04-16 20:27:01

+0

全屏模式下的模糊程度如何? – kallotec 2012-04-16 23:56:37

+0

@ A型,令人毛骨悚然的另一个问题,请让我试图做一个点球大战的比赛,我让守门员一个矩形的碰撞球,如果球相交守门员它不是一个目标,但如果球在里面线的面积它的目标,你是怎么做这种游戏? – bentham 2012-04-17 17:01:32

回答

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); 
+0

我改变了,它仍然有些模糊,我不知道该怎么办?感谢您的回答 – bentham 2012-04-16 19:49:04

+0

@GeorgeBecj k,我增加了更多的答案 – 2012-04-17 07:19:37

+0

感谢您的答案它现在正在工作 – bentham 2012-04-17 16:57:31

相关问题