如何获取具有特定值的数组元素

时间:2013-05-01 08:41:45

标签: c# xna

我需要得到一个2D数组的元素,其中有一个bool为真。 这是唯一一个有这个布尔值的人。

public void getCurrentTile()
{
    for (int x = 0; x < 75; x++)
    {
        for (int y = 0; y < 75; y++)
        {
            if (((Tile)grid[y, x]).lit)
            {
                current = (Tile)grid[y, x];
            }
        }
    }
}

这是我目前的代码。它似乎无法正常工作。它仅在一个瓷砖点亮时返回电流。当另一个图块亮起时,它不会返回图块。

瓷砖代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
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 SimMedieval
{
    public class Tile
    {
        public int size;
        public String path;
        public Texture2D texture;
        public bool lit = false;
        public bool lightable;
        MouseState mouseState;
        public System.Drawing.Color color;
        public int posX;
        public int posY;

        public Tile(int s, String texturepath, bool light)
        {
            size = s;
            path = texturepath;
            lightable = light;

        }

        public void load(Game game, Game1 game1, System.Drawing.Color colour)
        {
            color = colour;
            texture = game.Content.Load<Texture2D>(path);
            game1.tiles.Add(this);
        }
        public void render(SpriteBatch sprites,int x, int y, Camera cam, Tile[,] grid)
        {
            mouseState = Mouse.GetState();
            float camPosX = (x * size) + cam.posX;
            float camPosY = (y * size) + cam.posY;
            if (-20 < camPosX && camPosX < 960 && -20 < camPosY && camPosY < 640)
            {
                if (new Microsoft.Xna.Framework.Rectangle(x, y, 1, 1).Contains((mouseState.X / size) - ((int)Math.Round(cam.posX) / size), (mouseState.Y / size) - ((int)Math.Round(cam.posY) / size)))
                {
                    lit = true;
                    grid[y, x] = this;
                    sprites.Draw(texture, new Vector2((x * size) + cam.posX, (y * size) + cam.posY), Microsoft.Xna.Framework.Color.Coral);
                }
                else
                {
                    lit = false;
                    grid[y, x] = this;
                    sprites.Draw(texture, new Vector2((x * size) + cam.posX, (y * size) + cam.posY), Microsoft.Xna.Framework.Color.White);
                }
            }
        }

        public void spawn(int x, int y, Tile[,] grid)
        {
            grid[y, x] = this;
            posX = x;
            posY = y;
        }


    }


}

2 个答案:

答案 0 :(得分:2)

我应该这样做,我想:

public Tile getCurrentTile()
{
    for (int x = 0; x < 75; x++)
        for (int y = 0; y < 75; y++)
            if (((Tile)grid[y, x]).lit)
                return (Tile)grid[y, x];

    return null; // Return null if not found.
}

不是在搜索功能中设置current,而是让它返回找到的项目更清楚,或者null如果无法找到它。

然后你的调用代码看起来更像是这样:

var item = getCurrentTile();

if (item != null)
    current = item;
else
    // Code to handle no current tile being found.

或者(更简单)你可以使用Linq:

Tile item = grid.Cast<Tile>().FirstOrDefault(cell => cell.lit);

if (item != null)
    current = item;
else
    // Code to handle no current tile being found.

这很有效,因为它会在找到第一个项目后立即停止迭代。

如果您想要发现异常,请执行以下操作:

Tile item = grid.Cast<Tile>().First(cell => cell.lit);

一旦找到项目,这也将停止迭代。这是我使用的,因为它是最有效的,它还表达了其中一个必须匹配谓词的想法。这意味着您知道返回的item将永远不会为null - 但是,如果元素的 none 与谓词匹配,它当然会抛出异常。

但是,如果你想在没有找到或多于一个的情况下抛出异常,你可以这样做:

Tile item = grid.Cast<Tile>().Single(cell => cell.lit);

这将始终遍历所有元素,因为它是检查多个元素是否与谓词匹配的唯一方法。

答案 1 :(得分:0)

您可以使用Linq查找所需的项目。但首先你需要将2D数组转换为IEnumerable<T>但是如何?如果您的grid包含Tile的对象,请尝试以下简单的一行解决方案。

Tile current= (from Tile item in array
                   where item!=null && item.lit select item).FirstOrDefault();
相关问题