我的子弹永远不会在屏幕上画画
本文关键字:屏幕 子弹 永远 我的 | 更新日期: 2023-09-27 18:32:37
我正在XNA中制作Space Invaders副本。我有处理移动和精灵动画的 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);
}
}
}
最后,我启动了 playerShip 类,并在我的 Game1 类中Update()
/Draw()
它。
我不明白这行如果(bulletsList.Count()> 20)。它有什么作用?我的意思是,如果场上没有子弹,你就不能发射新的子弹吗?
-- 其他问题
然后你问if (bulletsSpawn.spritePos.Y == 0)
.但是位置。Y 从不为 0,它只高于 0 或低于 0。因此,将该==
更改为<=