Hello everyone! I wanted to post a method of mine I use in a game which isnt performing as well as id like. When this method is called, which is every frame rendered for x frames after a game item has left the screen, it just renders some squares from the departure point, which expand whilst also fading away. Whenever this happens it causes the screen to flicker, could anyone tell me if anything i am doing in this code could be causing this?
public void drawDeathAnims(Graphics g){
//loop through arraylist
//while looping if any are incountered which are too old delete them
for(int i = 0; i < departurePoints.size(); i++){
int[] dpt = departurePoints.get(i);
//check that it's not too old, for now just go up to 400;
if(dpt[3]>deathAnimFrames){
//delete
departurePoints.remove(i);
i--;
} else{
//if its the first time get the chainsize form last frame
if(dpt[3]==0){
dpt[4]=gameState.getLastScore()/gameState.getLastChain();
departurePoints.get(i)[4]=dpt[4];
}
//has to be centered
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
(RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
g2.setFont(new Font("SansSerif",Font.BOLD,18));
int opacity = 255-(255*dpt[3])/deathAnimFrames;
String score = ""+(dpt[4]);
int scoreX = GraphicUtils.getXPointForCenteringText(g2, score, Global.TILE_DIM);
g2.setColor(new Color(0,0,0,opacity));
g2.drawString(""+score,
GamePanel2.LEFT_BORDER+(dpt[0]*Global.TILE_DIM)+scoreX+2,
//(-dpt[3]/3)+
27+dpt[1]);
g2.setColor(new Color(255,255,255,opacity));
g2.drawString(""+score,
GamePanel2.LEFT_BORDER+(dpt[0]*Global.TILE_DIM)+scoreX,
//(-dpt[3]/3)+
25+dpt[1]);
int boxSize = Global.TILE_DIM+(dpt[3]*2);
int boxXPos = GamePanel2.LEFT_BORDER+(dpt[0]*Global.TILE_DIM)-dpt[3];
//g.setColor(Tile.getTileColor(dpt[2]));
g.drawRect(boxXPos,
(dpt[1])-dpt[3],
boxSize,
boxSize);
g.drawRect(boxXPos+1,
(dpt[1])-dpt[3]+1,
boxSize-2,
boxSize-2);
int lessOpacity = opacity-50;
if(lessOpacity < 0){
lessOpacity = 0;
}
g.setColor(new Color(255,255,255,lessOpacity));
g.drawRect(boxXPos+2,
(dpt[1])-dpt[3]+2,
boxSize-4,
boxSize-4);
g.drawRect(boxXPos+3,
(dpt[1])-dpt[3]+3,
boxSize-6,
boxSize-6);
int evenLessOpacity = opacity-50;
if(evenLessOpacity < 0){
evenLessOpacity = 0;
}
g.setColor(new Color(255,255,255,evenLessOpacity));
g.drawRect(boxXPos+4,
(dpt[1])-dpt[3]+4,
boxSize-8,
boxSize-8);
g.drawRect(boxXPos+5,
(dpt[1])-dpt[3]+5,
boxSize-10,
boxSize-10);
//increse age by one
dpt[3]++;
}
}
}
Thanks for any advice!