2014-04-02 99 views
0

我想在我的代碼中旋轉「golf_ball」精靈,但我遇到了很多困難。我已經檢查過教程,但我沒有運氣。我的比賽基本上應該儘可能長時間地用球把球保持在空中。任何幫助將非常感激!XNA Sprite旋轉

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

    // Ball sprite 
    Texture2D ballSprite; 

    // Ball location 
    Vector2 ballPosition = Vector2.Zero; 

    // Store some information about the sprite's motion. 
    Vector2 ballSpeed = new Vector2(150, 150); 

    // Paddle sprite 
    Texture2D paddleSprite; 

    // Paddle location 
    Vector2 paddlePosition; 

    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() 
    { 
     base.Initialize(); 

     // Set the initial paddle location 
     paddlePosition = new Vector2(
      graphics.GraphicsDevice.Viewport.Width/2 - paddleSprite.Width/2, 
      graphics.GraphicsDevice.Viewport.Height - paddleSprite.Height); 
    } 


    /// <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); 
     ballSprite = Content.Load<Texture2D>("golf_ball"); 
     paddleSprite = Content.Load<Texture2D>("Paddle"); 

     // 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(); 

     // Move the sprite by speed, scaled by elapsed time 
     ballPosition += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; 

     int maxX = GraphicsDevice.Viewport.Width - ballSprite.Width; 
     int maxY = GraphicsDevice.Viewport.Height - ballSprite.Height; 

     // Check for bounce 
     if (ballPosition.X > maxX || ballPosition.X < 0) 
      ballSpeed.X *= -1; 

     if (ballPosition.Y < 0) 
      ballSpeed.Y *= -1; 
     else if (ballPosition.Y > maxY) 
     { 
      // Ball hit the bottom of the screen, so reset ball 
      ballPosition.Y = 0; 
      ballSpeed.X = 150; 
      ballSpeed.Y = 150; 
     } 


     // Ball and paddle collide? Check rectangle intersection between objects 

     Rectangle ballRect = 
      new Rectangle((int)ballPosition.X, (int)ballPosition.Y, 
      ballSprite.Width, ballSprite.Height); 

     Rectangle handRect = 
      new Rectangle((int)paddlePosition.X, (int)paddlePosition.Y, 
       paddleSprite.Width, paddleSprite.Height); 

     if (ballRect.Intersects(handRect)) 
     { 
      // Increase ball speed 
      ballSpeed.Y += 50; 
      if (ballSpeed.X < 0) 
       ballSpeed.X -= 50; 
      else 
       ballSpeed.X += 50; 

      // Send ball back up the screen 
      ballSpeed.Y *= -1; 
     } 

     base.Update(gameTime); 
     // Update the paddle's position 
     KeyboardState keyState = Keyboard.GetState(); 
     if (keyState.IsKeyDown(Keys.Right)) 
      paddlePosition.X += 5; 
     else if (keyState.IsKeyDown(Keys.Left)) 
      paddlePosition.X -= 5; 
    } 


    /// <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 sprite 
     spriteBatch.Begin(); 
     spriteBatch.Draw(ballSprite, ballPosition, Color.White); 
     spriteBatch.Draw(paddleSprite, paddlePosition, Color.White); 
     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 
} 

}

+1

http://msdn.microsoft.com/en-us/library/ff433992.aspx檢出此頁,是你想要的嗎? '旋轉'參數? – user3449857

+1

您有具體問題嗎?請儘量將代碼縮小到相關部分。 – Robin

回答

2

歡迎堆棧溢出!

由於@ user3449857已經指出,您的代碼實際上並沒有旋轉精靈。

首先,請嘗試以下操作:

spriteBatch.Draw(ballSprite, ballPosition, null, Color.White, 1.5f, Vector2.Zero, SpriteEffects.None, 1.0f); 

你可以看到,第5參數是旋轉,而且它應該轉動你的球。這需要一個浮動。

用這些知識武裝起來,嘗試使用谷歌搜索其他教程如何增加旋轉。