メイン クラスには、「エンティティ」オブジェクトのリストがあります。エンティティは、プレイヤーや敵に必要なすべてのデータを使用して作成したクラスです。
List<Entity> Listentities = new List<Entity>();
さらに下には、現在の状態ではいくつかの問題がある Gravity メソッドがあります。
public void Gravity(GameTime gameTime)
{
foreach(Entity entity in Listentities)
{
entity.entityPosition.X += 1;
}
}
ここでやろうとしていることは非常に簡単です。リスト内のすべてのエンティティを取得して、メソッドが呼び出されるたびにそのposition.Xを1単位下に移動しようとしています。ただし、ゲームを実行するたびに、「オブジェクト参照がオブジェクトのインスタンスに設定されていません」という行が表示されます
entity.entityPosition.X += 1;
私が間違っていることを知る必要があります。私はすぐに学んでいますが、私はまだかなり新しいです。現在、エンティティ「player1」のみがリストに追加されています。入力することで、このエラーを簡単に修正できます
player1.entityPosition.X += 1;
しかし、それはプレイヤーにのみ影響します。
編集:エンティティのコンストラクターを含めています:
public Entity(string entityName, Texture2D EntityAppearance, int EntityMaxHealth, int SpriteHeight, int SpriteWidth, int CurrentFrame, int AnimationCall, int EntityAttack, int EntityStr, int EntityDex, int EntityLuk, int EntityDefense, bool EntityIsMe )
{
this.entityName = entityName;
this.EntityAppearance = EntityAppearance;
this.EntityMaxHealth = EntityMaxHealth;
this.SpriteHeight = SpriteHeight;
this.SpriteWidth = SpriteWidth;
this.CurrentFrame = CurrentFrame;
this.AnimationCall = AnimationCall;
this.EntityAttack = EntityAttack;
this.EntityLuk = EntityLuk;
this.EntityDex = EntityDex;
this.EntityStr = EntityStr;
this.EntityDefense = EntityDefense;
this.EntityIsMe = EntityIsMe;
}
game1 の loadcontent() メソッド。
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
camera = new Camera(GraphicsDevice.Viewport);
player1 = new Entity("player1", Content.Load<Texture2D>("PlaceHolder"), 100, 100, 100, 0, 0, 3, 1, 1, 1, 0, true);
player1.EntityPosition = new Vector2(0, 0);
player1.EntityBox = new Rectangle((int)player1.EntityPosition.X, (int)player1.EntityPosition.Y, player1.SpriteWidth, player1.SpriteHeight);
player1.Origin = new Vector2();
spritefont = Content.Load<SpriteFont>("SpriteFont1");
}
私のEntityクラスの上部にあるentityPosition変数。
public Vector2 entityPosition;
public Vector2 EntityPosition
{
get { return entityPosition; }
set { entityPosition = value; }
}
リストの初期化:
List<Entity> Listentities = new List<Entity>();
これは game1.cs の上部にあります
player1 エンティティを Listentities に追加するコード:
protected override void Initialize()
{
InitGraphicsMode(1280, 720, false);
Listentities.Add(player1);
base.Initialize();
}
もちろんGame1で。
XNA GS 4.0を使用していることにも言及する必要があったかもしれません