Hi I’m in quite some problem at the moment, I currently have a class “Ship” with subclasses “Player” & “Enemy” both of them are in the List “Ships”
What I’m trying to do is extract the “Player thePlayer” from the list of “Ships”
I’ve tried using List.Find() without any succes, is it just me using it wrong or is it the wrong way of dealing with this problem?
for (int i = 0; i < HomingBullets.Count; i++)
{
HomingBullet h = HomingBullets.ElementAt(i);
Player thePlayer = ??? List.Find ???
h.Update(gameTime, thePlayer.position);
}
the h.Update takes the two arguments (gameTime & Vector2)
I’m trying to extract the Vector2 using the code above.
Keep in mind I’ve only used C# since the start of this year although I have a few years of experience of programming in various languages.
The simplest solution would be to add a readonly property to the
Shipclass:Then you could do
The cast to
Playercould possibly go if you can use that object as a generalShipinstead of the more specificPlayer(I am assuming thatshipListis aList<Ship>, right?).Other solutions (such as using the
iskeyword, or the equivalent LINQshipList.OfType<Player>().Single()will work just as well, but checking for runtime type will be much slower than theIsPlayerproperty check.