This site requires JavaScript, please enable it in your browser!
Greenfoot back
akilino
akilino wrote ...

2014/5/1

Removing Bomb and Bad when they intersect

akilino akilino

2014/5/1

#
I placed in game a bomb, for when the badguy intersects, it would both disappear, and an animation would occured. Problem is, is a,ways giving me an error, game is always crashing. I wrote this in the Bomb.class import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class BOmb here. * * @author (your name) * @version (a version number or a date) */ public class Bomb extends badGeral { /** * Act - do whatever the BOmb wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { explodeBad(); } public void explodeBad() { Actor bad3 = getOneIntersectingObject(Bad3.class); pacWorld pacworld = (pacWorld)getWorld(); if(bad3 != null) { getWorld().removeObject(this); getWorld().removeObject(bad3); } } } and it gives me this: java.lang.NullPointerException at Bomb.explodeBad(Bomb.java:27) at Bomb.act(Bomb.java:17) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Bomb.explodeBad(Bomb.java:27) at Bomb.act(Bomb.java:17) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) It seems that it is accusing the removeObject(bad3). What's the problem?
davmac davmac

2014/5/1

#
Please use code tags when you post code. The problem is these lines:
getWorld().removeObject(this);
getWorld().removeObject(bad3);
You are removing 'this' object from the world, then on the next line you are calling 'getWorld()' which now returns null. If you change the order of these two lines it should be fine, I think.
akilino akilino

2014/5/1

#
davmac wrote...
Please use code tags when you post code. The problem is these lines:
getWorld().removeObject(this);
getWorld().removeObject(bad3);
You are removing 'this' object from the world, then on the next line you are calling 'getWorld()' which now returns null. If you change the order of these two lines it should be fine, I think.
The bad is removed, but the bomb remains there... Why?
davmac davmac

2014/5/1

#
Please post your updated code (use code tags! :)
akilino akilino

2014/5/1

#
public void explodeBad() // method from the Bomb.class { Actor bad3 = getOneIntersectingObject(Bad3.class); // if is intersecting bad3 pacWorld pacworld = (pacWorld)getWorld(); // if(bad3 != null) // if bomb intersects with bad3 then... { pacworld.removeObject(bad3); // goes to the pacworld, and removes bad3 pacworld.removeObject(this); // removes the bomb } } if i got these 2 lines: pacworld.removeObject(this); // removes the bomb pacworld.removeObject(bad3); // goes to the pacworld, and removes bad3 the bad is removed, and the bomb remains there. if I put these 2 lines: pacworld.removeObject(bad3); // goes to the pacworld, and removes bad3 pacworld.removeObject(this); // removes the bomb the bad is removed, and the bomb remains there. if I put only this line: pacworld.removeObject(this); // removes the bomb the bomb is removed.
danpost danpost

2014/5/1

#
Could it be that you have multiple bombs at the same location and it only appears that the bomb is not being removed?
akilino akilino

2014/5/1

#
danpost wrote...
Is it possible that you multiple bombs at the same location and it only appears that the bomb is not being removed?
indeed it has. But.. the bomb is only placed when i press the "b" button. here's the code: // this method is at the pac.class, which is the class that drops the bomb. public void getBonus() { Actor bonus = getOneIntersectingObject(Bonus.class); // if is intersecting bonus... pacWorld pacworld = (pacWorld)getWorld(); // in order to have access to bonus class pacImage = getImage(); // saving image with name "pacImage" Bomb bomb = new Bomb(); // calling bomb ( this might be the problem ) if(bonus != null) //if intersects bonus { marca = 1; //marca will be 1 contadorTempo = 0; // contadorTempo will be 0 pacworld.removeObject(bonus); // removes bonus from scenario pacImage.setTransparency(100); //image is set to transparency 100 contadorBombas = 0; // bombCounter is set to 0 } if(Greenfoot.isKeyDown("b") && marca == 1) // is key "b" is down, and pac did intersect // with bonus, then { pacworld.addObject(bomb, getX(), getY()); //adds the bomb contadorBombas++; //increases bombCounter by 1 } if(contadorTempo == 100) //if timeCounter == 100, then set transparency // back to normal { pacImage.setTransparency(255); } } I don't know why, but it keeps adding 2 bombs at the same place, instead of one! How to work this out?
davmac davmac

2014/5/1

#
Please, use code tags when you post code. I gave a link explaining how to do this earlier. It makes it so much easier to point out parts of your code (discuss the line numbers) and also to copy-and-paste your code.
danpost danpost

2014/5/2

#
I think you need to reset 'marca' back to zero somewhere at the place where you increment the bomb counter in the pac class.
akilino akilino

2014/5/2

#
davmac wrote...
Please, use code tags when you post code. I gave a link explaining how to do this earlier. It makes it so much easier to point out parts of your code (discuss the line numbers) and also to copy-and-paste your code.
Sorry davmac, I thought that what you wanted to say with that, to understand better the code, was the comment. Now I noticed that it is to present the code in a better way to understand. My apologies.
You need to login to post a reply.