例外:迭代期间并发修改:'_GrowableList'的Instance(length:17) [英] Exception: Concurrent modification during iteration: Instance(length:17) of '_GrowableList'

查看:111
本文介绍了例外:迭代期间并发修改:'_GrowableList'的Instance(length:17)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做游戏。在// ERROR ZONE中称为Exception。我不明白类游戏中有List区域。

  void spawnt(){
var rnd = new Random();
var rnd2 = new Random();
rnd = rnd.nextInt(100);
if(rnd2.nextBool())
rnd2 = rnd2.nextInt(100);
else
rnd2 = -rnd2.nextInt(100);
var x = flippy.position [ x] + lastx + sw + rnd;
var y = rnd2 * pomer-100-1800 * pomer-168 * pomer;
Rectangle vrch = new Rectangle( img / tube1.png,sw,sh,{ x:x, y:y, z:0});
Rectangle spodek = new Rectangle( img / tube2.png,sw,sh,{ x:x, y:sh + mezeray * pomer + y, z:0}));

vrch.onCollision((_){
checkover();
});

spodek.onCollision((_){
checkover();
});
game.addObject(spodek);
game.addObject(vrch);
var cp = new Checkpoint(sw,mezeray,{ x:x + sw, y:y + sh, z:0});
cp.onCollision((e){
scoore ++;
querySelector(#scoore)。text = SCOORE:$ {scoore};
听起来[1]。 play();
//此错误区域
spawnt();
});
game.addObject(cp);

rnd = new Random();
rnd = rnd.nextInt(300);
rnd2 = new Random();
rnd2 = rnd2.nextInt(300);
var rnd3 = new Random();
rnd3 = new Random();
rnd3 = rnd3.nextInt(10);
抢w;
if(rnd3 == 0 || rnd3 == 1 || rnd3 == 2){
w = new Grab( img / cigarette.png,600 * pomer,418 * pomer,{ x:x + sw + rnd, y:sh + mezeray * pomer + y-mezeray * pomer + rnd2-168 * pomer, z:0});
w.scale(0.2);
w.onCollision((c){
smokescreen = true;
smoketimer.cancel();
smoketimer = new Timer(const Duration(milliseconds:10000),(){
smokescreen = false;
});
kucky.play();
if(csong)
cig.play();
csong = false ;
});
game.addObject(w);
}
else if(rnd3 == 3 || rnd3 == 4){
w = new Grab( img / whiskey.png,162 * pomer,398 * pomer,{ x:x + sw + rnd, y:sh + mezeray * pomer + y-mezeray * pomer + rnd2-168 * pomer, z:0});
w.scale(0.2);
w.onCollision((c){
AudioElement pr = new AudioElement( sounds / yes。$ {koncovka});
pr.play();
威士忌++;
if(威士忌> = 10&&&威士忌< 20){
speedotoceni = 30;
zhoupni();
}
else if(威士忌> = 20&&< 30){
speedotoceni = 25;
zhoupni();
}
else if(whiskey> = 30&&威士忌< 40) {
speedotoceni = 20;
zhoupni();
}
else if(威士忌> = 40&&威士忌<50){
speedotoceni = 15;
zhoupni();
}
else if(威士忌> = 50&&威士忌< 60){
speedotoceni = 10;
zhoupni();
}
else if(威士忌> = 60&&&&&&& amp;威士忌< 70){
speedotoceni = 5;
zhoupni();
}
else if(whiskey> = 70&& whiskey< 80){
speedotoceni = 1;
zhoupni();
}
querySel ector(#scoore)。text = SCOORE:$ {scoore};
});
game.addObject(w);
}
else if(rnd3 == 9){
w = new Grab( img / jatra.png,539 * pomer,521 * pomer,{ x:x + sw + rnd, y:sh + mezeray * pomer + y-mezeray * pomer + rnd2-168 * pomer, z:0});
w.scale(0.1);
w.onCollision((c){
AudioElement pr = new AudioElement( sounds / yes。$ {koncovka});
pr.play();
jatra ++;
document.cookie = jatra = $ {jatra}; expires = Thu,24 Dec 4000 00:00:00 GMT; path = /;
querySelector(#jatracount)。text = jatra .toString();
});
game.addObject(w);
}
lastx = lastx + 300 * pomer + rnd;
}

class Game.dart

  import'dart:html'; 
导入 dart:math;
导入 dart:async;
导入 Entity.dart;
导入 Gravity.dart;
导入 Rectangle.dart;
导入 Camera.dart;
导入 Checkpoint.dart;

类游戏{
CanvasElement canvas;
重力引力;
List< Entity>实体=新的List();
List< Rectangle>矩形= new List();
相机相机;
var leftSpace = 0;
var topSpace = 0;
var _onCollision;
var _over =(_){};
var _render =(_){};
num _lastFrame = 0;
bool isOver = false;


游戏(this.canvas,this.gravity){
window.animationFrame.then((e){this.render(e);});
}

void addEntity(Entity entita){
this.entities.add(entita);
}

void addObject(Rectangleangle){
this.rectangles.add(rectangle);
}

void render(num delta){
num fps = 1000 /(delta-this._lastFrame);
this._lastFrame = delta;
CanvasRenderingContext2D ctx = this.canvas.context2D;
ctx.clearRect(0,0,this.canvas.width + this.leftSpace.abs(),this.canvas.height + this.topSpace.abs());
this._render(true);
this.rectangles.forEach((s){
ctx.fillStyle = s.color;
if(s.hasColor()){
ctx.fillStyle = s.color ;
ctx.fillRect(s.position [ x]-this.leftSpace,s.position [ y]-this.topSpace,s.width,s.height);
}
else {
ctx.drawImageScaled(s.texture,s.position [ x]-this.leftSpace,s.position [ y]-this.topSpace,s.width,s.height );
}
});
this.entities.forEach((e){
e.move(fps);
e.power [ y] + = this.gravity.operate(e)*(60 / fps);
ctx.save();
ctx.translate(e.position [ x]-this.leftSpace + e.width / 2,e.position [ y]- this.topSpace + e.height / 2);
ctx.rotate(e.rotation * PI / 180);
ctx.drawImageScaled(e.img,-(e.width / 2),- (e.height / 2),e.width,e.height);
ctx.restore();
});
crashResponse();
checkCollisions();
if(!this.camera.fixed [ x])
this.leftSpace = this.camera.target.position [ x]-this.canvas.width / 2 + this.camera .target.width / 2;
else
this.leftSpace = this.camera.fixedPos [ x];

if(!this.camera.fixed [ y])
this.topSpace = this.camera.target.position [ y]-this.canvas.height / 2 + this.camera.target.height / 2;
else
this.topSpace = this.camera.fixedPos [ y];
this._render(false);
ctx.closePath();
window.animationFrame.then((e){this.render(e);});
}

onCollision(e){
this._onCollision = e;
}

void crashResponse(){
this.rectangles.forEach((s){
num x1 = s.position [ x];
num y1 = s.position [ y];
num z1 = s.position [ z];
num w1 = s.width;
num h1 = s。高度;

this.entities.forEach((e){
num rot = e.rotation *(PI / 180);
num x2 = e.position [ x ];
num y2 = e.position [ y];
num z2 = e.position [ z];
num w2 = e.width;
num h2 = e.height;
if(x2> = x1-w2& x2 <= x1 + w1& y2> y1-h2&& y2 s.collisionResponse(e);
e.collision(s);
s.collision(e);
}
} );
});
}

void checkCollisions(){
List< Entity> kolize = new List();
for(此实体中的实体e1){
num x1 = e1.position [ x];
num y1 = e1.position [ y];
num z1 = e1.position [ z];
num w1 = e1.width;
num h1 = e1.height;

for(此实体中的实体e2){
if(e1 == e2 ||(kolize.indexOf(e1)!= -1&& kolize.indexOf(e2 )!= -1))继续;
num x2 = e2.position [ x];
num y2 = e2.position [ y];
num z2 = e2.position [ z];
num w2 = e2.width;
num h2 = e2.height;
if(x2> = x1-w2&& x2 <= x1 + w1& y2> y1-h2& y2< y1 + h1& z1 == z2){
kolize.add(e1);
kolize.add(e2);
}
}
}
}

void over(){
if(!this.isOver)
this。 _过度();
this.isOver = true;
}

onOver(e){
this._over = e;
}

onRender(f){
this._render = f;
}
}

未捕获错误:迭代期间并发修改:Instance(length :17)的 _GrowableList。
堆栈跟踪:
#0 List.forEach(dart:core-patch / growable_array.dart:241)



#1 Game.collisionResponse( FlappyBird / web / classes / Game.dart:83:28)



#2 Game.render(FlappyBird / web / classes / Game.dart:62:22)



#3 Game.render。 (FlappyBird / web / classes / Game.dart:75:47)



#4 _rootRunUnary(dart:async / zone.dart:717)



#5 _RootZone.runUnary(dart:async / zone.dart:854)



#6 _Future._propagateToListeners.handleValueCallback(dart: async / future_impl.dart:439)



#7 _Future._propagateToListeners(dart:async / future_impl.dart:522)



#8 _Future._complete(dart:async / future_impl.dart:303)



#9 _SyncCompleter.complete(dart:async / future_impl.dart:44 )



#10 Window.animationFrame。 (文件:/// E:/b/build/slave/dartium-win-full-stable/build/src/build/Release/obj/global_intermediate/blink/bindings/dart/dart/html/Window.dart:62 )



例外:迭代期间并发修改:'_GrowableList'的Instance(length:17)。
未定义(未定义:0:0)



够了吗?
有人知道如何修理吗?
感谢您的回答。 :)

解决方案

此错误表示您正在迭代期间从集合中添加或删除对象。不允许这样做,因为添加或删除项目会改变集合的大小并弄乱后续迭代。



该错误很可能来自您尚未发布的其中一行

  s.collisionResponse(e)的来源; 
e.collision(s);
s.collision(e);

我假设这些调用之一是在执行期间从实体列表或矩形列表中删除一项forEach,这是不允许的。取而代之的是,您必须重组代码以删除列表迭代之外的项目,也许使用removeWhere方法。



请考虑以下尝试从列表中删除奇数的操作:

  var list = [1、2、3、4、5]; 
list.forEach((e){
if(e%2 == 1)
list.remove(e);
});

这将失败,并发修改异常,因为我们尝试在列表迭代期间删除项目。 / p>

但是,在列表迭代期间,我们可以标记要删除的项目,然后使用removeWhere在批量操作中将其删除:



< pre class = lang-dart prettyprint-override> var list = [1、2、3、4、5];
var toRemove = [];

list.forEach((e){
if(e%2 == 1)
toRemove.add(e);
});

list.removeWhere((e)=> toRemove.contains(e));


I make game. In //ERROR ZONE is called Exception. I don't understand it. Class game has List recrangles.

void spawnt(){
var rnd = new Random();
var rnd2 = new Random();
rnd = rnd.nextInt(100);
if(rnd2.nextBool())
  rnd2 = rnd2.nextInt(100);
else
  rnd2 = -rnd2.nextInt(100);
var x = flappy.position["x"]+lastx+sw+rnd;
var y = rnd2*pomer-100-1800*pomer-168*pomer;
Rectangle vrch = new Rectangle("img/tube1.png", sw, sh, {"x":x, "y":y, "z":0});
Rectangle spodek = new Rectangle("img/tube2.png", sw, sh, {"x":x, "y":sh+mezeray*pomer+y, "z":0});

vrch.onCollision((_){
  checkover();
});

spodek.onCollision((_){
  checkover();
});
game.addObject(spodek);
game.addObject(vrch);
var cp = new Checkpoint(sw, mezeray, {"x":x+sw, "y":y+sh, "z":0});
cp.onCollision((e){
  scoore++;
  querySelector("#scoore").text="SCOORE: ${scoore}";
  sounds[1].play();
  //THIS IS ERROR ZONE
  spawnt();
});
game.addObject(cp);

rnd = new Random();
rnd = rnd.nextInt(300);
rnd2 = new Random();
rnd2 = rnd2.nextInt(300);
var rnd3 = new Random();
rnd3 = new Random();
rnd3 = rnd3.nextInt(10);
Grab w;
if(rnd3 == 0 || rnd3 == 1 || rnd3 == 2){
  w = new Grab("img/cigarette.png", 600*pomer, 418*pomer, {"x":x+sw+rnd,"y":sh+mezeray*pomer+y-mezeray*pomer+rnd2-168*pomer, "z":0});
  w.scale(0.2);
  w.onCollision((c){
    smokescreen = true;
    smoketimer.cancel();
    smoketimer = new Timer(const Duration(milliseconds: 10000), (){
      smokescreen = false;
    });
    kucky.play();
    if(csong)
      cig.play();
    csong = false;
  });
  game.addObject(w);
}
else if(rnd3 == 3 || rnd3 == 4){
  w = new Grab("img/whiskey.png", 162*pomer, 398*pomer, {"x":x+sw+rnd,"y":sh+mezeray*pomer+y-mezeray*pomer+rnd2-168*pomer, "z":0});
  w.scale(0.2);
  w.onCollision((c){
    AudioElement pr = new AudioElement("sounds/yes.${koncovka}");
    pr.play();
    whiskey++;
    if(whiskey>=10 && whiskey<20){
      speedotoceni = 30;
      zhoupni();
    }
    else if(whiskey>=20 && whiskey<30){
      speedotoceni = 25;
      zhoupni();
    }
    else if(whiskey>=30 && whiskey<40){
      speedotoceni = 20;
      zhoupni();
    }
    else if(whiskey>=40 && whiskey<50){
      speedotoceni = 15;
      zhoupni();
    }
    else if(whiskey>=50 && whiskey<60){
     speedotoceni = 10;
      zhoupni();
    }
    else if(whiskey>=60 && whiskey<70){
      speedotoceni = 5;
      zhoupni();
    }
    else if(whiskey>=70 && whiskey<80){
      speedotoceni = 1;
      zhoupni();
    }
    querySelector("#scoore").text="SCOORE: ${scoore}";
  });
  game.addObject(w);
}
else if(rnd3 == 9){
  w = new Grab("img/jatra.png", 539*pomer, 521*pomer, {"x":x+sw+rnd,"y":sh+mezeray*pomer+y-mezeray*pomer+rnd2-168*pomer, "z":0});
  w.scale(0.1);
  w.onCollision((c){
    AudioElement pr = new AudioElement("sounds/yes.${koncovka}");
    pr.play();
    jatra++;
    document.cookie = "jatra=${jatra}; expires=Thu, 24 Dec 4000 00:00:00 GMT; path=/";
    querySelector("#jatracount").text = jatra.toString();
  });
  game.addObject(w);
 }
lastx = lastx+300*pomer+rnd;
}

class Game.dart

  import 'dart:html';
  import 'dart:math';
  import 'dart:async';
  import 'Entity.dart';
  import 'Gravity.dart';
  import 'Rectangle.dart';
  import 'Camera.dart';
  import 'Checkpoint.dart';

  class Game {
    CanvasElement canvas;
    Gravity gravity;
    List<Entity> entities = new List();
    List<Rectangle> rectangles = new List();
    Camera camera;
    var leftSpace = 0;
    var topSpace = 0;
    var _onCollision;
    var _over = (_){};
    var _render = (_){};
    num _lastFrame = 0;
    bool isOver = false;


Game(this.canvas, this.gravity){
  window.animationFrame.then((e){this.render(e);});
}

void addEntity(Entity entita){
  this.entities.add(entita);
}

void addObject(Rectangle rectangle){
  this.rectangles.add(rectangle);
}

void render(num delta){
  num fps = 1000/(delta-this._lastFrame);
  this._lastFrame = delta;
  CanvasRenderingContext2D ctx = this.canvas.context2D;
  ctx.clearRect(0, 0, this.canvas.width+this.leftSpace.abs(), this.canvas.height+this.topSpace.abs());
  this._render(true);
  this.rectangles.forEach((s){
    ctx.fillStyle=s.color;
    if(s.hasColor()){
      ctx.fillStyle=s.color;
      ctx.fillRect(s.position["x"]-this.leftSpace,s.position["y"]-this.topSpace,s.width,s.height);
    }
    else{
      ctx.drawImageScaled(s.texture, s.position["x"]-this.leftSpace,s.position["y"]-this.topSpace,s.width,s.height);
    }
  });
  this.entities.forEach((e){
    e.move(fps);
    e.power["y"] += this.gravity.operate(e)*(60/fps);
    ctx.save(); 
    ctx.translate(e.position["x"]-this.leftSpace+e.width/2, e.position["y"]-this.topSpace+e.height/2);
    ctx.rotate(e.rotation* PI/180);
    ctx.drawImageScaled(e.img, -(e.width/2), -(e.height/2), e.width, e.height);
    ctx.restore(); 
  });
  collisionResponse();
  checkCollisions();
  if(!this.camera.fixed["x"])
    this.leftSpace = this.camera.target.position["x"]-this.canvas.width/2+this.camera.target.width/2;
  else
    this.leftSpace = this.camera.fixedPos["x"];

  if(!this.camera.fixed["y"])
    this.topSpace = this.camera.target.position["y"]-this.canvas.height/2+this.camera.target.height/2;
  else
    this.topSpace = this.camera.fixedPos["y"];
  this._render(false);
  ctx.closePath();
  window.animationFrame.then((e){this.render(e);});
}

void onCollision(e){
  this._onCollision = e;
}

void collisionResponse(){
  this.rectangles.forEach((s){
    num x1 = s.position["x"];
    num y1 = s.position["y"];
    num z1 = s.position["z"];
    num w1 = s.width;
    num h1 = s.height;

    this.entities.forEach((e){
      num rot = e.rotation * (PI / 180);
      num x2 = e.position["x"];
      num y2 = e.position["y"];
      num z2 = e.position["z"];
      num w2 = e.width;
      num h2 = e.height;
      if(x2>=x1-w2 && x2<=x1+w1 && y2>y1-h2 && y2<y1+h1 && z1==z2){
        s.collisionResponse(e);
        e.collision(s);
        s.collision(e);
      }
    });
  });
}

void checkCollisions(){
  List<Entity> kolize = new List();
  for(Entity e1 in this.entities){
    num x1 = e1.position["x"];
    num y1 = e1.position["y"];
    num z1 = e1.position["z"];
    num w1 = e1.width;
    num h1 = e1.height;

    for(Entity e2 in this.entities){
      if(e1 == e2 || (kolize.indexOf(e1) != -1 && kolize.indexOf(e2) != -1)) continue;
      num x2 = e2.position["x"];
      num y2 = e2.position["y"];
      num z2 = e2.position["z"];
      num w2 = e2.width;
      num h2 = e2.height;
      if(x2>=x1-w2 && x2<=x1+w1 && y2>y1-h2 && y2<y1+h1 && z1==z2){
        kolize.add(e1);
        kolize.add(e2);
      }
    }
  }
}

void over(){
  if(!this.isOver)
    this._over();
  this.isOver = true;
}

void onOver(e){
  this._over = e;
}

void onRender(f){
  this._render = f;
}
}

Uncaught Error: Concurrent modification during iteration: Instance(length:17) of '_GrowableList'. Stack Trace: #0 List.forEach (dart:core-patch/growable_array.dart:241)

#1 Game.collisionResponse (FlappyBird/web/classes/Game.dart:83:28)

#2 Game.render (FlappyBird/web/classes/Game.dart:62:22)

#3 Game.render. (FlappyBird/web/classes/Game.dart:75:47)

#4 _rootRunUnary (dart:async/zone.dart:717)

#5 _RootZone.runUnary (dart:async/zone.dart:854)

#6 _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:439)

#7 _Future._propagateToListeners (dart:async/future_impl.dart:522)

#8 _Future._complete (dart:async/future_impl.dart:303)

#9 _SyncCompleter.complete (dart:async/future_impl.dart:44)

#10 Window.animationFrame. (file:///E:/b/build/slave/dartium-win-full-stable/build/src/build/Release/obj/global_intermediate/blink/bindings/dart/dart/html/Window.dart:62)

Exception: Concurrent modification during iteration: Instance(length:17) of '_GrowableList'. undefined (undefined:0:0)

Is it enough? Does somebody know how to repair it? Thanks for answer. :)

解决方案

This error means that you are adding or removing objects from a collection during iteration. This is not allowed since adding or removing items will change the collection size and mess up subsequent iteration.

The error is likely from one of these lines which you haven't posted the source for:

 s.collisionResponse(e);
 e.collision(s);
 s.collision(e);

I assume one of these calls is removing an item from either the entities list or the rectangles list, during the forEach, which is not allowed. Instead you must restructure the code to remove the items outside of the list iteration, perhaps using the removeWhere method.

Consider the following attempt to remove odd numbers from a list:

var list = [1, 2, 3, 4, 5];
list.forEach( (e) {
 if(e % 2 == 1) 
   list.remove(e);
});

This will fail with a concurrent modification exception since we attempt to remove the items during the list iteration.

However, during the list iteration we can mark items for removal and then remove them in a bulk operation with removeWhere:

var list = [1, 2, 3, 4, 5];
var toRemove = [];

list.forEach( (e) {
 if(e % 2 == 1) 
   toRemove.add(e);
});

list.removeWhere( (e) => toRemove.contains(e));

这篇关于例外:迭代期间并发修改:'_GrowableList'的Instance(length:17)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆