调用以下函数时游戏停止工作:
错误是:shotmanager对象不会被传递,并且总是为null!我正在按照教程完成他的工作!
我明确地发起了它。
这里有一些编码
namespace SpaceShip
{
class Enemy: Sprite
{
public shotmanager shotmanager;
private double timesincelastshot;
private const int timedelay = 1;
private Vector2 pos;
public Enemy(Texture2D Text, Vector2 VEC, Rectangle REC, shotmanager shotmanager)
: base(Text, VEC, REC)
{
shipspeed = 300;
this.shotmanager = shotmanager;
}
public override void Update(KeyboardState keyboard, GameTime gameTime)
{
var random = new Random();
if (Velocity == Vector2.Zero)
{
var direction = random.Next(2);
Velocity = new Vector2(direction == 0 ? -1 : 1, 0);
}
else if (gameTime.ElapsedGameTime.Seconds % 2 == 0)
{
if (random.Next(15) == 0)
Velocity = new Vector2(-velocity.X, velocity.Y);
}
timesincelastshot += gameTime.ElapsedGameTime.TotalSeconds;
if (timesincelastshot > timedelay)
{
if (random.Next(2) == 0)
pos = calculateposition();
shotmanager.fireenemyshot(pos);
timesincelastshot = 0;
}
base.Update(keyboard, gameTime);
}
private Vector2 calculateposition()
{
return VEC + new Vector2(TEXT.Width/2, TEXT.Height/2);
}
}
}
namespace SpaceShip
{
public class shotmanager
{
private Shot shot;
public Texture2D shottexture;
private Rectangle bounds;
private List<Shot> shotgroup = new List<Shot>();
//public shooting shot;
Vector2 vec;
public shotmanager(Texture2D shottexture, Rectangle bounds)
{
// TODO: Complete member initialization
this.shottexture = shottexture;
this.bounds = bounds;
}
public void fireenemyshot(Vector2 shotposition)
{
var inflatebounds = bounds;
vec = shotposition;
inflatebounds.Inflate(10, 10);
shot.Velocity = new Vector2(0, 1);
shotgroup.Add(shot);
shot = new Shot(shottexture, shotposition, inflatebounds);
}
public void draw(SpriteBatch spriteBatch)
{
foreach (var i in shotgroup)
shot.draw(spriteBatch);
}
public void Update(KeyboardState keyboard, GameTime gameTime)
{
foreach (var i in shotgroup)
shot.Update(keyboard, gameTime);
}
}
}
namespace SpaceShip
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Sprite background;
shipclass spaceship;
//Sprite spaceship;
SpriteFont score;
EnemyManger enemy;
Texture2D shottexture;
public shotmanager shotmanager;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <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.
var alienship = Content.Load<Texture2D>("flying_saucer_2");
Texture2D spaceshiptexture;
spriteBatch = new SpriteBatch(GraphicsDevice);
spaceshiptexture = Content.Load<Texture2D>("1358114942_kspaceduel");
var positionx = (graphics.GraphicsDevice.Viewport.Width - spaceshiptexture.Width) / 2;
var positiony = (graphics.GraphicsDevice.Viewport.Height - spaceshiptexture.Height );
var ship = new Rectangle(0, graphics.GraphicsDevice.Viewport.Height - spaceshiptexture.Height -150 , graphics.GraphicsDevice.Viewport.Width, spaceshiptexture.Height+150);
background = new Sprite(Content.Load<Texture2D>("large_space_1920x1200"), Vector2.Zero, graphics.GraphicsDevice.Viewport.Bounds);
spaceship = new shipclass(spaceshiptexture, new Vector2(positionx, positiony), ship);
score = Content.Load<SpriteFont>("SpriteFont1");
shottexture = Content.Load<Texture2D>("64px-SpaceInvadersLaserDepiction");
shotmanager = new shotmanager(shottexture, graphics.GraphicsDevice.Viewport.Bounds);
enemy = new EnemyManger(alienship, graphics.GraphicsDevice.Viewport.Bounds, shotmanager);
答案 0 :(得分:2)
public void fireenemyshot(Vector2 shotposition)
{
var inflatebounds = bounds;
vec = shotposition;
inflatebounds.Inflate(10, 10);
// CULPRIT HERE
shot.Velocity = new Vector2(0, 1);
shotgroup.Add(shot);
// THIS IS DONE TOO LATE.
shot = new Shot(shottexture, shotposition, inflatebounds);
}
问题在于您尝试在Velocity
上设置shot
,但在此之前您从未实例化过它。您在尝试使用它之后将其实例化。
编辑 - 仅供参考 您从没关系,我看到了它的设定位置。pos
传入此方法的Enemy
也从未设置过。它是一个结构,所以它应该被初始化,但是,你永远不会给它一个值。