using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
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;
namespace WindowsGame3
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D ship, bullet, alien;
Texture2D[] aliens = new Texture2D[20];
Texture2D[] bullets = new Texture2D[10];
Vector2 shipPos = new Vector2(200, 540);
Vector2 alienPos = new Vector2(200, 400);
Vector2 bulletPos = new Vector2(200, 450);
Vector2[] aliensPos = new Vector2[20];
Vector2[] bulletsPos = new Vector2[10];
int alienCount = 0;
int bulletCount = 0;
KeyboardState state = Keyboard.GetState();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 400;
graphics.PreferredBackBufferHeight = 600;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
alien = Content.Load<Texture2D>("alien");
ship = Content.Load<Texture2D>("ship");
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 4; y++)
{
aliens[alienCount] = Content.Load<Texture2D>("alien");
aliensPos[alienCount].X = 100 + (x * 40);
aliensPos[alienCount].Y = 20 + (y * 40);
alienCount++;
}
}
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (state.IsKeyDown(Keys.Right) && shipPos.X < 360)
{
shipPos.X += 5;
}
if (state.IsKeyDown(Keys.Left) && shipPos.X > 0)
{
shipPos.X -= 5;
}
if (state.IsKeyDown(Keys.Space))
{
bullets[bulletCount] = Content.Load<Texture2D>("bullet");
bulletsPos[bulletCount].X = shipPos.X;
bulletsPos[bulletCount].Y = shipPos.Y;
bulletCount++;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(ship, shipPos, Color.White);
spriteBatch.Draw(alien, alienPos, Color.White);
for (int alienCount = 0; alienCount < 20; alienCount++)
{
spriteBatch.Draw(aliens[alienCount], aliensPos[alienCount], Color.White);
}
for (int bulletCount = 0; bulletCount < 10; bulletCount++)
{
spriteBatch.Draw(bullets[bulletCount], bulletsPos[bulletCount], Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
我想在船隻位置創建一個子彈並將該子彈存儲在一個數組中,但在繪製方法中,當我從數組中繪製子彈時出現錯誤消息此方法對於這個參數不接受null。 參數名稱:紋理xna方法不接受null
你傳遞一個參數爲空。這是不允許的。 –
看...有...在代碼中有很多錯誤,我不知道從哪裏開始。我只能希望它是一個編輯過的代碼片段。 – LightStriker
我可以看到你對XNA和編程很新。雖然能夠製作自己的視頻遊戲的魅力非常令人興奮,但我認真地建議您在處理這類問題之前,先學習一些關於C#的基本教程,並學習一些編程原則,或者您將頭靠在牆上數週。 –