所以我有一個包含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 關注
- 193 瀏覽
添加回答
舉報
0/150
提交
取消