我试图使用C#和XNA框架构建一个分组游戏。我把砖放在一个单独的类中,并创建了一个可以加载10块砖的数组。但是,当我运行游戏时,实际上没有砖块出现,我似乎无法找到错误,因为Visual Studio没有标记任何内容。
砖类:
class bricks
{
Texture2D brickimg;
Rectangle rectangle;
int[] brickXPos = new int[50];
int[] brickYPos = new int[50];
public bricks(Texture2D NewTexture, Rectangle newRectangle)
{
brickimg = NewTexture;
rectangle = newRectangle;
}
public void Initialize()
{
for (int i = 0; i < 50; i++)
{
brickXPos[i] = 300 + i * 60;
brickYPos[i] = 100;
}
}
public void Update()
{
}
public void Draw(SpriteBatch spritebatch)
{
for (int i = 0; i < 50; i++)
{
spritebatch.Draw(brickimg, new Vector2(brickXPos[i], brickYPos[i]), rectangle, Color.White);
}
}
}
主程序:
/// <summary>
/// This is the main type for your game
/// </summary>
public class Breakout : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D BackgroundImg;
bricks bricks;
public Breakout()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1024;
graphics.PreferredBackBufferHeight = 768;
graphics.ApplyChanges();
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.
spriteBatch = new SpriteBatch(GraphicsDevice);
BackgroundImg = Content.Load<Texture2D>("starfield");
bricks = new bricks(Content.Load<Texture2D>("redbrick"),new Rectangle());
// 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 (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <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);
// TODO: Add your drawing code here
base.Draw(gameTime);
spriteBatch.Begin();
spriteBatch.Draw(BackgroundImg, Vector2.Zero, Color.White);
bricks.Draw(spriteBatch);
spriteBatch.End();
}
}
答案 0 :(得分:0)
您永远不会初始化砖块对象
您需要在某处调用bricks.Initialise();
函数(最好在Breakout
类的初始化函数中)。
我还建议您使用更清洁的代码标准
类名通常以大写字母开头,即:Bricks
而不是bricks
,在您的情况下,类和类的实例具有相同的名称(bricks
),这可能会造成混淆甚至应该创建一个编译错误(我想?)
(brick
类的构造函数中的'NewTexture'变量也会受到错误命名的影响,可能更应该命名为newTexture
,特别是由于Rect被称为newRectangle
, - 但这不是一个代码审查网站,所以现在离开这个问题!)。
答案 1 :(得分:0)
尝试将null
传递到您的砖类中的sourceRectangle
参数spriteBatch.Draw
。我认为你传入的矩形并不好。
spritebatch.Draw(brickimg, new Vector2(brickXPos[i], brickYPos[i]), null, Color.White);