0

好的,所以我正在使用 xna studio 在 Visual Studio 2010 c# 中制作太空入侵者类型的游戏,目前卡在子弹类上,它会生成子弹,但不会朝精灵指向的方向发射。项目符号类表示 Public Vector2 来源;没有被分配给任何东西并保持其默认值,但我不知道是什么原因造成的。

子弹代码

   class Bullets
    {
        public Texture2D texture;

        public Vector2 position;
        public Vector2 velocity;
        public Vector2 origin;

        public bool isVisible;

        public Bullets(Texture2D newTexture)
        {
            texture = newTexture;
            isVisible = false;
        }




        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, null, Color.White, 0f, origin, 1f, SpriteEffects.None, 1);
        }

    }

游戏代码如下:

namespace Rotationgame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Vector2 spriteVelocity;
        const float tangentialVelocity = 0f;
        float friction = 1f;

        Texture2D spriteTexture;
        Rectangle spriteRectangle;

        Rectangle screenRectangle;

        // The centre of the image
        Vector2 spriteOrigin;

        Vector2 spritePosition;
        float rotation;

        // Background
        Texture2D backgroundTexture;
        Rectangle backgroundRectangle;


        // Shield
        Texture2D shieldTexture;
        Rectangle shieldRectangle;

        // Bullets
        List<Bullets> bullets = new List<Bullets>();
        KeyboardState pastKey;



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

            graphics.PreferredBackBufferWidth = 1250;
            graphics.PreferredBackBufferHeight = 930;

            screenRectangle = new Rectangle(
                0,
                0,
                graphics.PreferredBackBufferWidth,
                graphics.PreferredBackBufferHeight);
        }

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

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

            shieldTexture = Content.Load<Texture2D>("Shield");
            shieldRectangle = new Rectangle(517, 345, 250, 220);

            spriteTexture = Content.Load<Texture2D>("PlayerShipup");
            spritePosition = new Vector2(640, 450);

            backgroundTexture = Content.Load<Texture2D>("Background");
            backgroundRectangle = new Rectangle(0, 0, 1250, 930);

            // 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 (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            // TODO: Add your update logic here

            if (Keyboard.GetState().IsKeyDown(Keys.Space) && pastKey.IsKeyUp(Keys.Space))
                Shoot();
            pastKey = Keyboard.GetState();

            spritePosition = spriteVelocity + spritePosition;

            spriteRectangle = new Rectangle((int)spritePosition.X, (int)spritePosition.Y,
                spriteTexture.Width, spriteTexture.Height);
            spriteOrigin = new Vector2(spriteRectangle.Width / 2, spriteRectangle.Height / 2);

            if (Keyboard.GetState().IsKeyDown(Keys.Right)) rotation += 0.025f;
            if (Keyboard.GetState().IsKeyDown(Keys.Left)) rotation -= 0.025f;

            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                spriteVelocity.X = (float)Math.Cos(rotation) * tangentialVelocity;
                spriteVelocity.Y = (float)Math.Sin(rotation) * tangentialVelocity;
            }
            else if (Vector2.Zero != spriteVelocity)
            {
                float i = spriteVelocity.X;
                float j = spriteVelocity.Y;

                spriteVelocity.X = i -= friction * i;
                spriteVelocity.Y = j -= friction * j;


                base.Update(gameTime);

            }
        }

        public void UpdateBullets()
        {
            foreach (Bullets bullet in bullets)
            {
                bullet.position += bullet.velocity;
                if (Vector2.Distance(bullet.position, spritePosition) > 100)
                    bullet.isVisible = false;
            }
            for (int i = 0; i < bullets.Count; i++)
            {
                if(!bullets[i].isVisible)
                {
                    bullets.RemoveAt(i);
                    i--;


                }


            }
        }

        public void Shoot()
        {
            Bullets newBullet = new Bullets(Content.Load<Texture2D>("ball"));
            newBullet.velocity = new Vector2((float)Math.Cos(rotation),(float)Math.Sin(rotation)) * 5f + spriteVelocity;
            newBullet.position = spritePosition + newBullet.velocity * 5;
            newBullet.isVisible = true;

            if(bullets.Count() < 20)
                bullets.Add(newBullet);
        }



        /// <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();
            spriteBatch.Draw(backgroundTexture, backgroundRectangle, Color.White);
            spriteBatch.Draw(shieldTexture, shieldRectangle, Color.White);
            foreach (Bullets bullet in bullets)
                bullet.Draw(spriteBatch);
            spriteBatch.Draw(spriteTexture, spritePosition, null, Color.White, rotation, spriteOrigin, 1f, SpriteEffects.None, 0);
            spriteBatch.End();


            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }

我哪里错了?

4

2 回答 2

2

当您绘制子弹时,您将 origin 作为参数传递,但从不为其设置值。您应该在图像上找到要作为原点的点(因此,如果将子弹放在 0,0 处,则图像的一部分将位于 0,0 处)。

如果您没有为变量设置值,然后尝试将其传递给函数(如果函数需要),那么这将导致程序崩溃。

在课堂上先尝试 0,0

Vector2 origin=new Vector2(0,0);

这应该使您的代码工作,根据您的喜好调整来源。

此外,与其将这些变量公开,不如将其设为私有并在项目符号中创建 get 和 set 函数来设置它们或获取值是更好的做法。这将不可预测的访问和修改的风险降至最低

编辑:再看一次意识到虽然问题不是问题。

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();

        updateBullets();
        ...
    }

您需要在更新函数中实际调用 updateBullets,它不会被自动调用

于 2013-05-28T19:24:52.933 回答
0

我修改了你的GameUpdate方法来调用你的UpdateBullets方法,现在子弹精灵会朝一个方向移动然后消失。

我还修复了Shoot()根据船旋转方向正确引导子弹的方法。

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();

        // TODO: Add your update logic here

        if (Keyboard.GetState().IsKeyDown(Keys.Space) && pastKey.IsKeyUp(Keys.Space))
            Shoot();
        pastKey = Keyboard.GetState();

        spritePosition = spriteVelocity + spritePosition;

        spriteRectangle = new Rectangle((int)spritePosition.X, (int)spritePosition.Y,
            spriteTexture.Width, spriteTexture.Height);
        spriteOrigin = new Vector2(spriteRectangle.Width / 2, spriteRectangle.Height / 2);

        if (Keyboard.GetState().IsKeyDown(Keys.Right)) rotation += 0.025f;
        if (Keyboard.GetState().IsKeyDown(Keys.Left)) rotation -= 0.025f;

        if (Keyboard.GetState().IsKeyDown(Keys.Up))
        {
            spriteVelocity.X = (float)Math.Cos(rotation) * tangentialVelocity;
            spriteVelocity.Y = (float)Math.Sin(rotation) * tangentialVelocity;
        }
        else if (Vector2.Zero != spriteVelocity)
        {
            float i = spriteVelocity.X;
            float j = spriteVelocity.Y;

            spriteVelocity.X = i -= friction * i;
            spriteVelocity.Y = j -= friction * j;


            base.Update(gameTime);

        }
        UpdateBullets();
    }
    public void Shoot()
    {
        Bullets newBullet = new Bullets(Content.Load<Texture2D>("ball"));
        newBullet.velocity = new Vector2((float)Math.Sin(rotation), (float)Math.Cos(rotation)) * new Vector2(5f,-5f) + spriteVelocity;
        newBullet.position = spritePosition + newBullet.velocity * 5;
        newBullet.isVisible = true;

        if (bullets.Count < 20)
            bullets.Add(newBullet);
    }
于 2013-05-29T14:08:08.403 回答