我試圖用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();
}
}
請將您的'bricks'類名更改爲'Bricks'。按標準編碼。 –