我正在制作游戏,我的java程序会毫无错误地冻结。 我为我的对象做了一个Trail,所以看起来很酷。如果没有Trail,它就像魅力一样,但当我将Trail添加到对象时,它的工作时间为2-3秒,并冻结。
package myfirstgame;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
public class Trail extends GameObject{
private float alpha = 1;
private Handler handler;
private Color color;
private int width, height;
private float life;
public Trail(int x, int y, ID id, Color color, int width, int height, float life,Handler handler) {
super(x, y, id);
this.handler = handler;
this.color = color;
this.width = width;
this.height = height;
this.life = life;
}
public void tick() {
if(alpha > life){
alpha -= (life - 0.0001f);
}else {
handler.removeObject(this);
}
}
public void render(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setComposite(makeTransparent(alpha));
g.setColor(color);
g.fillRect(x, y, width, height);
g2d.setComposite(makeTransparent(1));
}
private AlphaComposite makeTransparent(float alpha){
int type = AlphaComposite.SRC_OVER;
return(AlphaComposite.getInstance(type, alpha));
}
public Rectangle getBounds() {
return null;
}
}
然后在我的Game.java中我有这个:
handler.addObject(new BasicEnemy(r.nextInt(WIDTH), r.nextInt(HEIGHT),ID.BasicEnemy, handler));
在我的BasicEnemy.java中:
handler.addObject(new Trail(x, y, ID.Trail, Color.red, 16,16, 0.01f, handler));