使用XNA鼠标点击方向使Texture2D移动?

时间:2016-11-14 00:23:13

标签: c# xna

这是我到目前为止的代码。我试图让子弹朝着点击鼠标的方向移动。我有一个子弹矩形和一个Texture2D,同样适用于射击子弹的大炮。

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 个答案:

答案 0 :(得分: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;

对于像大炮这样的静态实体,将“player”替换为“cannon”,将“mouseClick”替换为“player”。

(我想起了记忆中SinCos的位置,所以如果设置错误,有人可以纠正我。)