Adding player name into list:
chatList0.Add(s);
Adding player msg into list:
chatList.Add(s);
Printing the player name & msg in different color:
for (int i = 0; i < chatList0.Count(); i++)
{
//draw system msg (contains name/time/etc)
spriteBatch.DrawString(font, chatList0[i], new Vector2(40, arr1[i]), Color.Yellow);
//check length of system msg
Vector2 offset = textEntry.GetMeasurements(font, chatList0[i]);
//draw msg after offset
spriteBatch.DrawString(font, chatList[i], new Vector2(offset.X + 40, arr1[i]), Color.White);
}
Is this correct? Are there any changes needed to optimize the code?
DrawStringcan only draw in one colour at a time, so you need to draw the 2 different bits of text separately.I suggest making a class or struct for your messages, that has
PlayerNameandMessageproperties.Then when you loop through each item to draw them, get the
PlayerName, then useSpriteFont.MeasureStringto measure how wide the string will be.Then draw the
PlayerName(and a": "or whatever) where you currently do.Then, draw the
Message, in your other colour, offset by theXproperty of the vector returned from your measurestring method.