2016-11-14 87 views
0

這是我到目前爲止的代碼。我試圖讓子彈向鼠標點擊的方向移動。我有一個子彈Rectangle和一個Texture2D,對於炮彈從哪裏發射炮彈也是一樣。讓Texture2D在用XNA點擊鼠標的方向移動?

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

     int screenWidth; 
     int screenHeight; 

     int speedX; 
     int speedY; 

     MouseState oldMouse; 

     Texture2D cannon; 
     Rectangle cannonRect; 

     Texture2D bullet; 
     Rectangle bulletRect; 

     KeyboardState kb; 

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

     protected override void Initialize() 
     { 
      // TODO: Add your initialization logic here 
      screenWidth = graphics.GraphicsDevice.Viewport.Width; 
      screenHeight = graphics.GraphicsDevice.Viewport.Height; 
      oldMouse = Mouse.GetState(); 

      speedX = 0; 
      speedY = 0; 

      cannonRect = new Rectangle((screenWidth/2) - 100, (screenHeight/2) - 100, 100, 100); 
      bulletRect = new Rectangle(cannonRect.X, cannonRect.Y, 10, 10); 

      this.IsMouseVisible = true; 

      base.Initialize(); 
     } 

     protected override void LoadContent() 
     { 
      // Create a new SpriteBatch, which can be used to draw textures. 
      spriteBatch = new SpriteBatch(GraphicsDevice); 
      cannon = Content.Load<Texture2D>("Smoothed Octagon"); 
      bullet = Content.Load<Texture2D>("White Square"); 
      // TODO: use this.Content to load your game content here 
     } 

     protected override void UnloadContent() 
     { 
      // TODO: Unload any non ContentManager content here 
     } 

     protected override void Update(GameTime gameTime) 
     { 
      bulletRect.X += speedX; 
      bulletRect.Y += speedY; 

      this.IsMouseVisible = true; 
      // Allows the game to exit 
      if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || kb.IsKeyDown(Keys.Escape)) 
       this.Exit(); 

      // TODO: Add your update logic here 
      MouseState mouse = Mouse.GetState(); 
      kb = Keyboard.GetState(); 

      if (mouse.LeftButton == ButtonState.Pressed && oldMouse.LeftButton == ButtonState.Released) 
      { 
       bulletRect.X += speedX; 
       bulletRect.Y += speedY; 
      } 

      oldMouse = mouse; 
      base.Update(gameTime); 
     } 

     protected override void Draw(GameTime gameTime) 
     { 
      GraphicsDevice.Clear(Color.Black); 

      // TODO: Add your drawing code here 
      spriteBatch.Begin(); 
      spriteBatch.Draw(bullet, bulletRect, Color.White); 
      spriteBatch.Draw(cannon, cannonRect, Color.White); 
      spriteBatch.End(); 

      base.Draw(gameTime); 
     } 
    } 
} 

回答

1

下面是一些如何處理相對於點的子彈速度和方向的僞代碼。

// On MouseClick 

float angle = Math.Atan2(mouseClick.X - player.X, mouseClick.Y - player.Y); 
bulletVelocity.X = (Math.Cos(angle) + Math.Sin(angle)) * bulletSpeed; 
bulletVelocity.Y = (-Math.Sin(angle) + Math.Cos(angle)) * bulletSpeed; 

// On Update Positions 

bullet.X += bulletVelocity.X; 
bullet.Y += bulletVelocity.Y; 

對於像大炮靜態的實體,將「播放器」與「大炮」和「鼠標點擊」與「運動員」。

(我回憶的Sin並從內存Cos的位置,所以希望有人可以糾正我,如果這是錯誤的設置。)

+0

'bulletVelocity'是,子彈是怎麼回事的速度,和'mouseClick'是代表鼠標位置的點。 – Abion47

+0

謝謝!這就是我想知道的。 – turbog3n