所以我有一个包含379个元素的列表,我想删除其中的最后8个元素我正在使用List<Point> points= new List<Point>();
...
points.RemoveRange(points.Count-8,8);但它抛出ArgumentOutOfRangeException:需要非负数。参数名称:索引所以我有清单的课看起来像这样上课要点:namespace XanMan.NET{ class Point { protected Texture texture; protected RectangleShape body; protected Vector2f position; public const uint point = 10; public Point(Vector2f position) { texture = new Texture("point.png"); body = new RectangleShape(new Vector2f(10, 10)) { Texture = texture, Position = position }; } protected Point() { } public void Draw(RenderWindow window) { window.Draw(body); } }}和program.cs与主要class Program{ static RenderWindow window; static GAMESTATE gamestate; static Map map; static Menu menu; static void Main(string[] args) { window = new RenderWindow(new VideoMode(1280, 780), "XanMan.NET"); gamestate = GAMESTATE.mainmenu; menu = new Menu(gamestate); map = new Map(); PointsMap mapOfPoints = new PointsMap(); window.Closed += Window_Closed; window.KeyReleased += menu.Update; while (window.IsOpen) { window.DispatchEvents(); window.Clear(); map.Draw(window); mapOfPoints.Draw(window); window.Display(); } } private static void Window_Closed(object sender, EventArgs e) => window.Close();}我已经删除了PointsMap中的一些循环,但是您已经知道发生了什么。
1 回答
凤凰求蛊
TA贡献1825条经验 获得超4个赞
您的绘制函数假设removerange索引和offset是8的倍数。
情况似乎并非如此:points.Count%8!=0。
您可以使用下面的代码来处理其余的代码。
public void Draw(RenderWindow window)
{
var n = 8;
var index = Math.Max(0, points.Count - n);
var offset = Math.Min(points.Count, n);
points.RemoveRange(index, offset);
foreach (Point point in points)
{
point.Draw(window);
}
}
- 1 回答
- 0 关注
- 168 浏览
添加回答
举报
0/150
提交
取消