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

2016/1/6

Cant remove Actor

Arivarasan Arivarasan

2016/1/6

#
Hello friends, This is my code for Bomb.class. When it hits object of princi.class both the objects must be removed from the world. I have my princi object removed but Bomb object keeps moving in the user interface. I m pretty sure Bomb object is gets null value after intersection as it thrown exception of Actor not found when I tried to use it after intersection with princi object. But the image keeps moving in user interface. This is my code for Bomb.class
public void act() 
    {
    this.setLocation(this.getX(),this.getY()-1);
    Actor p=getOneIntersectingObject(princi.class);
  if (p!=null) {
      getWorld().removeObject(p);
        getWorld().removeObject(this);
        return;
    }
    if (getWorld() != null && getX() <= 0)  {
        getWorld().removeObject(this);
        return;
    }

    } 
Thanks in advance.
danpost danpost

2016/1/6

#
What you have here may be an illusion. It might be that you have multiple Bomb objects on top of each other so that when one is found to intersect a princi object, they are both removed; but it appears that the Bomb is not removed because you see the one that was underneath it (or one that was underneath was removed). Show the all methods with codes related to adding Bomb objects into the world. Specify what class these codes are in (and whether they are subclasses of World, Actor, or whatever).
Arivarasan Arivarasan

2016/1/7

#
Yes! What you are saying is correct. When I pause the Scenario and drag the Bomb image I can see Multiple images. Here you Go with the code. Code for Gun:
public class gun extends Actor
{
    /**
     * Act - do whatever the gun wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        movement();
        shoot();
    }   
private void movement(){
    if(Greenfoot.isKeyDown("left")){
        move(-2);
        }
        if(Greenfoot.isKeyDown("right")){
        move(2);
        }
}
private void shoot(){
    if(Greenfoot.isKeyDown("space")){
        World w=getWorld();
        w.addObject(new bomb(),this.getX(),this.getY());
        }
}
}
Code for Bomb: This is an Updated Code Where I have not removed the Bomb Object.
public class bomb extends Actor
{
    /**
     * 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() 
    {
    this.setLocation(this.getX(),this.getY()-1);
if(this.getY()==0){
getWorld().removeObject(this);
return;
}
Actor p=getOneIntersectingObject(princi.class);
if(p!=null){
    getWorld().removeObject(p);
    getWorld().addObject(new princi(),Greenfoot.getRandomNumber(600),0);
    Shoot.i++;
    getWorld().showText("Your Score : "+Shoot.i,500,20);
return;
}
    }    
  
   
    
}
Other classes doesn't have anything to do with the Bomb class. princi code is just for moving princi object and removing when it reaches the end of the world.
public class princi extends Actor
{
    /**
     * Act - do whatever the princi wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        this.setLocation(this.getX(),this.getY()+1);
        
      remove();
    }    
   private void remove(){
      if(this.isAtEdge()){
        World w;
        w=getWorld();
        w.addObject(new princi(),Greenfoot.getRandomNumber(600),0);
        w.removeObject(this);
        }
    }
   
}
danpost danpost

2016/1/7

#
In the 'shoot' method of the 'gun' class, you are using 'isKeyDown' for the space key as the only condition for creating a bomb object. As such, if the gun is not currently moving, there will be a minimum of two bombs created at the location of the gun. It is almost impossible to press and release the key before two or more act cycles complete.; hence creating two or more bombs at the same location. You probably only want one bomb to be created for each time the space key is pressed (or released). Or, in other words, the condition is not on the current state of the space key, but in the changing of its state. So, if you save the state of the key in a field and compare it to the state on the next act cycle, you can then detect a change in state. In the gun class, add a boolean field for the state of the space key:
private boolean spaceDown;
then change your 'shoot' method to this:
public void shoot()
{
    if (spaceDown != Greenfoot.isKeyDown("space")) // detecting change in state of key
    {
        spaceDown = !spaceDown; // update saved state
        if (spaceDown) // key just pressed
        {
            World w = getWorld();
            w.addObject(new bomb(), getX(), getY());
        }
    }
}
Arivarasan Arivarasan

2016/1/8

#
Thanks! That solved my problem.
You need to login to post a reply.