ArrayList remove(object) causes flickering problem in rendering
in
Programming Questions
•
7 months ago
Hi, all
Thanks in advance, I am having some custom class object stored in an ArrayList. When I click any of them, it will fade away and when alpha reaches 0, it will remove itself from the ArrayList using remove(this). However, if I click them in order, the moment when it is removed causing the rendering of the next object in ArrayList to flicker a bit. It almost seems like the ArrayList lost the next object for a brief moment so it wasn't updated and drawn to the screen. How do I go around and keep that flickering from happening?
In the code below, if you click starting with the bottom-right one and go clockwise, you will see the next object flicker. I am using Processing 2.0b8 on PC, and it happens with or without P2D
*just test it this on 1.5.1, also does it.
*just test it this on a Mac, also does it.
Thanks in advance, I am having some custom class object stored in an ArrayList. When I click any of them, it will fade away and when alpha reaches 0, it will remove itself from the ArrayList using remove(this). However, if I click them in order, the moment when it is removed causing the rendering of the next object in ArrayList to flicker a bit. It almost seems like the ArrayList lost the next object for a brief moment so it wasn't updated and drawn to the screen. How do I go around and keep that flickering from happening?
In the code below, if you click starting with the bottom-right one and go clockwise, you will see the next object flicker. I am using Processing 2.0b8 on PC, and it happens with or without P2D
*just test it this on 1.5.1, also does it.
*just test it this on a Mac, also does it.
- ArrayList<enemy> enemies;
- void setup() {
- size(300,300, P2D);
- enemies= new ArrayList<enemy>();
- float a = PI/4; //create four enemies, PI/2 away, starting bottom-right corner
- for (int i=0; i<4; i++) {
- enemy temp = new enemy(width/2+100*cos(a), height/2+100*sin(a));
- enemies.add(temp);
- a+=PI/2;
- }
- }
- void draw() {
- background(0);
- if (!enemies.isEmpty()) {
- for (int i=0; i<enemies.size(); i++) {
- enemy temp= enemies.get(i);
- temp.update();
- }
- }
- }
- class enemy {
- PVector pos;
- float size;
- boolean death;
- int alpha;//alpha for all the color in the drawing of the enemy
- int type;//enemy type
- enemy(float _x, float _y) {
- pos=new PVector(_x, _y);
- size=60;
- death = false;
- alpha = 255;
- }
- void update() {
- if (death) {//if dead, fade away
- alpha-=5;
- if (alpha<=0) {
- enemies.remove(this);
- }
- }
- if (mousePressed) {
- if (mouseX>pos.x-size/2 && mouseX<pos.x+size/2) {
- if (mouseY>pos.y-size/2 && mouseY<pos.y+size/2) {
- damage();
- }
- }
- }
- //draw the enemy
- pushMatrix();
- translate(pos.x, pos.y);
- fill(211, 127, 28, alpha);
- ellipse(0, 0, size, size);
- popMatrix();
- }
- void damage() {
- death = true;
- }
- }
1