Components build up in memory, how can i destroy them?
Hello!
I have a problem.. maybe it磗 simple to fix, but i'm kind of a newbie...
I'm trying to make a 2D shooting game..
The problem i'm having is that whenever the user "shoots", a bullet object is created, but i noticed that the bullets never disappear from memory...
This is the Bullet class:
publicclass Bulletextends Component{
BufferedImage img;
int x;
int y;
int direction;
/** Creates a new instance of Bullet */
public Bullet(String name){
try{
img = ImageIO.read(new File("./"+name));
}catch (IOException e){
e.printStackTrace();
}
}
publicvoid paint(Graphics g){
g.drawImage(img, 0, 0,null);
}
}
Here is where the bullet is created:
publicvoid keyPressed(KeyEvent ke){
if(ke.getKeyCode()==ke.VK_A){
if(!fireIsPressed){
Bullet bull =new Bullet("bala.png");
bull.setSize(25,15);
bull.setLocation(mc.x+20,mc.y+20);
bull.x=mc.x+20;
bull.y=mc.y+20;
cp.add(bull);
cp.setComponentZOrder(bull,0);
BulletMovementController bmv =new BulletMovementController(bull);
bmv.start();
bull.paint(bull.getGraphics());
}
fireIsPressed=true;
}
AND here is the Thread that controls the movement:
publicclass BulletMovementControllerextends Thread{
Bullet b;
public BulletMovementController(Bullet bull){
this.b=bull;
}
publicvoid run(){
while(b.x>=0 && b.x<=480){
b.x+=3;
b.setLocation(b.x,b.y);
b.repaint(b.x,b.y,25,15);
try{
sleep(5);
}catch (InterruptedException ex){
ex.printStackTrace();
}
}
b.img=null;
}
}
as you can see i tried assigning null value to the image from the bullet object.. but that doesnt work..
can someone help me please?
thanks in advance
[3736 byte] By [
Leon945a] at [2007-11-26 18:01:59]

# 1
1. you set the image to null not the bullet.
2. you probably don't want one thread per bullet, or object in your game, have a thread to deal with the game mechanics and the base thread to draw the items and respond to your gui events. Use an array, list, vector or whatever to hold the objects and go through it to paint it after each move (and delay increment is done).
Good luck with your game.
This might be able to help you a bit (but I think it still has issues being loaded properly if you're behind a corporate firewall)
http://sourceforge.net/projects/javoids/
# 4
Yes...
i just realized that, and i have managed to fix the problem.
The way i found out is because looking at the task manager in windows i could see the memory usage go up.
i fixed it by creating the bullets directly into an ArrayList and removing them from there once they were done.
this is the code:
here the bullet is created:
if(ke.getKeyCode()==ke.VK_A){
if(!fireIsPressed){
bullets.add(new Bullet("bala.png",1,true));//bullets is an ArrayList
Bullet bull = (Bullet) bullets.get(bullets.size()-1);
bull.setSize(25,15);
bull.setLocation(mc.x+20,mc.y+20);
bull.x=mc.x+20;
bull.y=mc.y+20;
cp.add(bull);
cp.setComponentZOrder(bull,0);
bull.paint(bull.getGraphics());
}
fireIsPressed=true;
}
and this is the thread that controlls the bullets.
public void run(){
while(1==1){
if(bullets.size()>0){
for(int i=0;i<bullets.size();i++){
Bullet b = (Bullet) bullets.get(i);
if(b.direction==1){
b.x+=3;
}else if(b.direction == 3){
b.x-=3;
}
b.setLocation(b.x,b.y);
b.repaint(b.x,b.y,25,15);
if(b.x><=0 || b.x>=480){
b.img=null;
b.repaint();
cp.remove(b);
bullets.remove(b);
}
}
}
try {
sleep(3);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
thanks you guys for your help!