foreach in foreach有2个不同的词典

时间:2012-06-01 14:09:12

标签: c# xna

我想在客户端上绘制多个玩家。它在没有foreach旋转的情况下工作正常,但是当我添加foreach旋转时,它会更新两个玩家的旋转,而不是旋转所针对的玩家。

foreach (var kvp in positions)
{
    foreach (var kvr in rotations)
    {
        // draw player
        spriteBatch.Draw(texture, kvp.Value, null, Color.White, 
        kvr.Value, new Vector2(texture.Width / 2, texture.Height / 2), 1f, 
        SpriteEffects.None, 1f);
    }
}

每个玩家都有一个唯一的ID,我将其作为键存储在两个词典中。 有没有办法可以将这些词典结合起来以达到正确的结果?

现在Dictonary的位置包含(long,Vector2(x,y))其中long是唯一的用户ID 并且旋转包含(long,float),其中long是与位置相同的用户ID,float是旋转。

编辑: 这样可以正常工作,但不会在设置时更新旋转。

foreach (var kvp in positions)
{
    // draw player
    spriteBatch.Draw(texture, kvp.Value, null, Color.White, 1f, 
    new Vector2(texture.Width / 2, texture.Height / 2), 1f, 
    SpriteEffects.None, 1f);
}

2 个答案:

答案 0 :(得分:2)

为什么不创建一个包含播放器所有信息的Player类。

class Player
{
    public Vector2 Position { get; set; }
    public float Rotation { get; set; }
}

并且只使用循环保留一个单词的字典,在这里我将向您展示一个非常简单的游戏类,它可以吸引5名玩家进行轮换,希望它会有所帮助:)

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Dictionary<int, Player> players;
Texture2D texture;

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
}

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);
    texture = Content.Load<Texture2D>("player");

    Random rnd = new Random();

    players = new Dictionary<int, Player>()
        {
            // Add 5 new players, their location is random and so is their rotation.
            {0,  new Player() { Position = new Vector2(rnd.Next(1024),rnd.Next(768)), Rotation = rnd.Next(360), Size = new Vector2(texture.Width, texture.Height)}}, 
            {1,  new Player() { Position = new Vector2(rnd.Next(1024),rnd.Next(768)), Rotation = rnd.Next(360), Size = new Vector2(texture.Width, texture.Height)}}, 
            {2,  new Player() { Position = new Vector2(rnd.Next(1024),rnd.Next(768)), Rotation = rnd.Next(360), Size = new Vector2(texture.Width, texture.Height)}}, 
            {3,  new Player() { Position = new Vector2(rnd.Next(1024),rnd.Next(768)), Rotation = rnd.Next(360), Size = new Vector2(texture.Width, texture.Height)}}, 
            {4,  new Player() { Position = new Vector2(rnd.Next(1024),rnd.Next(768)), Rotation = rnd.Next(360), Size = new Vector2(texture.Width, texture.Height)}}, 
        };
}

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();
    for (int i = 0; i < players.Count; i++)
    {
        Player targetPlayer = players[i];
        spriteBatch.Draw(texture, targetPlayer.PlayerRectangle, null, Color.White, targetPlayer.Rotation, targetPlayer.Centre, SpriteEffects.None, 0.0f);
    }
    spriteBatch.End();

    base.Draw(gameTime);
}
}

class Player
{
Rectangle playerRectangle;
Vector2 position;
Vector2 size;
Vector2 center;

public Vector2 Position { get { return position; } set { position = value; UpdateRectangleAndCentre(); } }
public Vector2 Size { get { return size; } set { size = value; UpdateRectangleAndCentre(); } }
public float Rotation { get; set; }
public Rectangle PlayerRectangle { get { return playerRectangle; } }
public Vector2 Centre { get { return center; } }

void UpdateRectangleAndCentre()
{
    playerRectangle = new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y);
    center = new Vector2(size.X / 2, size.Y / 2);
}
}

答案 1 :(得分:0)

不是那么简单:

 foreach (var kvp in positions)
        {
            kvr = rotations[kvp.Key];
            // draw player
            spriteBatch.Draw(texture, kvp.Value, null, Color.White, 
            kvr.Value, new Vector2(texture.Width / 2, texture.Height / 2), 1f, 
            SpriteEffects.None, 1f);
        }