我的子弹永远不会在屏幕上画画?

时间:2014-11-29 16:05:01

标签: c# xna

我正在XNA中制作太空侵略者副本。我有我的playerShip类来处理移动和精灵动画。今天我决定让我的船开枪。我创建了一个类来保存我的子弹逻辑并在我的playShip类中初始化/更新它们。所以我的问题是,当我构建和调试时,我按下键进行拍摄(在我的情况下,X),但子弹从未在屏幕上绘制。我在Draw()来电时设置了一个断点,但没有例外。这意味着代码从未执行过。这是我的船类:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;

namespace SpaceInvaders
{
    class playerShip
    {
        public static Texture2D playerShipTex, bulletsTex;
        public static Rectangle playerShipHitBox;
        public static Vector2 playerShipPos = new Vector2(369, 546), playerShipOrigin;
        int playerShipCurrentFrame = 1, playerShipFrameWidth = 62, playerShipFrameHeight = 86;
        public float spriteTimer = 0f, spriteInterval = 100f, bulletDelay;
        public List<blasterLasers> bulletsList;

        public playerShip()
        {
            bulletsList = new List<blasterLasers>();
            bulletDelay = 20f;
        }

        public void LoadContent(ContentManager Content)
        {
            playerShipTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\playerShip\\playerShipSpriteSheet");
            bulletsTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\playerShip\\playerShipBlaster");
        }

        public void Update(GameTime gameTime)
        {
            playerShipHitBox = new Rectangle(playerShipCurrentFrame * playerShipFrameWidth, 0, playerShipFrameWidth, playerShipFrameHeight);
            playerShipOrigin = new Vector2(playerShipHitBox.X / 2, playerShipHitBox.Y / 2);

            MouseState mouseState = Mouse.GetState();
            KeyboardState keyboardState = Keyboard.GetState();

            spriteTimer += (float)gameTime.ElapsedGameTime.Milliseconds;
            if (spriteTimer > spriteInterval)
            {
                playerShipCurrentFrame++;
                spriteTimer = 0f;
            }

            if (playerShipCurrentFrame == 2)
            {
                playerShipCurrentFrame = 0;
            }

            playerShipHitBox = new Rectangle(playerShipCurrentFrame * playerShipFrameWidth, 0, playerShipFrameWidth, playerShipFrameHeight);
            playerShipOrigin = new Vector2(playerShipHitBox.Width / 2, playerShipHitBox.Height / 2);

            if (keyboardState.IsKeyDown(Keys.X))
            {
                Shoot();
            }

            UpdateBullets();

            if (keyboardState.IsKeyDown(Keys.Right))
            {
                playerShipPos.X += 3;
            }

            if (keyboardState.IsKeyDown(Keys.Left))
            {
                playerShipPos.X -= 3;
            }

            if (keyboardState.IsKeyDown(Keys.Down))
            {
                playerShipPos.Y += 3;
            }

            if (keyboardState.IsKeyDown(Keys.Up))
            {
                playerShipPos.Y -= 3;
            }

            if (playerShipPos.X <= 0)
            {
                playerShipPos.X = 0;
            }

            if (playerShipPos.X + playerShipTex.Width >= 1110)
            {
                playerShipPos.X = 1110 - playerShipTex.Width;
            }

            if (playerShipPos.Y <= 48)
            {
                playerShipPos.Y = 48;
            }

            if (playerShipPos.Y + playerShipTex.Height >= Game1.screenHeight)
            {
                playerShipPos.Y = Game1.screenHeight - playerShipTex.Height;
            }
        }

        public void Shoot()
        {
            if (bulletDelay >= 0)
            {
                bulletDelay--;
            }

            if (bulletDelay <= 0)
            {
                blasterLasers newBullet = new blasterLasers(bulletsTex);
                newBullet.spritePos = new Vector2(playerShipPos.X + 14 - newBullet.spriteTex.Width / 2, playerShipPos.Y);
                newBullet.isVisible = true;

                if (bulletsList.Count() > 20)
                {
                    bulletsList.Add(newBullet);
                }
            }

            if (bulletDelay == 0)
            {
                bulletDelay = 20;
            }
        }

        public void UpdateBullets()
        {
            foreach (blasterLasers bulletsSpawn in bulletsList)
            {
                bulletsSpawn.spritePos.Y = bulletsSpawn.spritePos.Y - bulletsSpawn.spriteSpeed;
                if (bulletsSpawn.spritePos.Y == 0)
                {
                    bulletsSpawn.isVisible = false;
                }
            }

            for (int i = 0; i < bulletsList.Count(); i++)
            {
                if (!bulletsList[i].isVisible)
                {
                    bulletsList.RemoveAt(i);
                    i--;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(playerShipTex, playerShipPos, playerShipHitBox, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            foreach (blasterLasers bulletsSpawn in bulletsList)
            {
                bulletsSpawn.Draw(spriteBatch);
            }
        }
    }
}

这是我的爆破课:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;

namespace SpaceInvaders
{
    public class blasterLasers
    {
        public Rectangle boundingBox;
        public Texture2D spriteTex;
        public Vector2 spriteOrigin, spritePos;
        public bool isVisible;
        public float spriteSpeed;

        public blasterLasers(Texture2D newSpriteTex)
        {
            spriteSpeed = 10f;
            spriteTex = newSpriteTex;
            isVisible = false;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(spriteTex, spritePos, Color.White);
        }
    }
}

最后我在我的Game1课程中将playerShip课程和Update() / Draw()初始化。

1 个答案:

答案 0 :(得分:1)

我不理解这一行if(bulletsList.Count()&gt; 20)。它有什么作用?我的意思是,如果场上已经没有子弹,你就不能发射新的子弹了吗?

- 其他问题

然后你问if (bulletsSpawn.spritePos.Y == 0)。但是position.Y从不为0,它只在0以上或者在0以下。所以将==更改为<=