2011-11-22 41 views
1

對,我已經將自己的賽道添加到了我將要製作的賽車遊戲中,現在我試圖將賽車添加到賽道上,但是當我加載它時,賽車會變得超大。這是怎麼顯示出來,當我調試...Texture2D在XNA 4.0中過大了嗎?

http://oi42.tinypic.com/106hpxd.jpg

有任何錯誤或任何東西,但爲什麼會這樣過大?請告訴我關於如何修復它的重新編輯的代碼。謝謝!

這是我的編碼;

public class Game1 : Microsoft.Xna.Framework.Game 
{ 

    Texture2D background; 
    Rectangle mainFrame; 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    Texture2D car1Texture; 
    Vector2 car1Position = new Vector2(400f, 100f); 

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



    /// <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 
     //Change the resolution to 800x600    
     graphics.PreferredBackBufferWidth = 1000; 
     graphics.PreferredBackBufferHeight = 800; 
     graphics.ApplyChanges(); 
     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); 
     // Load the background content. 
     background = Content.Load<Texture2D>("roadscan"); 
     // Set the rectangle parameters 
     mainFrame = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); 

     spriteBatch = new SpriteBatch(GraphicsDevice); 
     car1Texture = Content.Load<Texture2D>("car1"); 

    } 


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


    /// <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 
    } 

    /// <summary> 
    /// Allows the game to run logic such as updating the world, 
    /// checking for collisions, gathering input, and playing audio. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     KeyboardState keyboard = Keyboard.GetState(); 
     GamePadState gamePad = GamePad.GetState(PlayerIndex.One); 

     if (keyboard.IsKeyDown(Keys.Left) || gamePad.DPad.Left == ButtonState.Pressed) 
     { 
      car1Position.X -= 3f; 
     } 

     if (keyboard.IsKeyDown(Keys.Right) || gamePad.DPad.Right == ButtonState.Pressed) 
     { 
      car1Position.X += 3f; 
     } 

     if (keyboard.IsKeyDown(Keys.Up) || gamePad.DPad.Right == ButtonState.Pressed) 
     { 
      car1Position.Y += 3f; 
     } 

     if (keyboard.IsKeyDown(Keys.Down) || gamePad.DPad.Right == ButtonState.Pressed) 
     { 
      car1Position.Y -= 3f; 
     } 

     // TODO: Add your update logic here 

     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); 
     // Draw the background. 


     // Start building the sprite. 
     spriteBatch.Begin(); 
     // Draw the background. 
     spriteBatch.Draw(background, mainFrame, Color.White); 
     spriteBatch.Draw(car1Texture, mainFrame, Color.White); 

     // End building the sprite. 
     spriteBatch.End(); 


     // TODO: Add your drawing code here 

     base.Draw(gameTime); 
    } 
} 

回答

4

您正在使用

SpriteBatch.Draw (Texture2D, Rectangle, Color) 

,讓您的汽車的精靈走Rectangle的位置和大小。我建議你使用版本

SpriteBatch.Draw (Texture2D, Vector2, Color) 

Vector2是雪碧的位置。查看所有不同版本的MSDN

因此,這將是:

spriteBatch.Draw(car1Texture, car1Position, Color.White); 
0

你的問題就在這裏:

spriteBatch.Draw(car1Texture, mainFrame, Color.White); 

您已經定義儀器的主機「是在屏幕的大小,所以你的車被拉伸到屏幕的大小。用'mainFrame'代替'car1Position',一切都應該運行得更順利。