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

2014/9/28

How do I make it so when my bullet hits the sides of my world it is removed and that i can fire more than on at once

DarkSoulDemon DarkSoulDemon

2014/9/28

#
This is the code for the bullet thank you very much
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move();
       // This calls on the method code so that it works
       Dead();
       // This calls on the method code so that it works
    }
    public void move()
    {
      move(15); // this moves the Bullet straingh at the speed of 15
    }    
    
    private void Dead()
    {
        Actor  Rock = getOneIntersectingObject(Rock.class);
         if(Rock !=null)
         {
          World myWorld = getWorld();  
          myWorld.removeObject (Rock); 
           Start start = (Start)myWorld;
          Counter counter = start.getCounter();
          counter.addScore();
          // This code means that when the enemy is killed it adds 1 to the score.
          myWorld.removeObject(this);
             // This code means that when the the Bullet hits the Rock the Rock will disapre
        }
     } 
}
    

danpost danpost

2014/9/28

#
Your 'Dead' method already has code to remove the object on one condition. By refactoring this method, we can apply two condiions to remove the object:
private void Dead()
{
    if (eatsRock() || atWorldEdge()) getWorld().removeObject(this);
}
These two new methods, 'eatsRock' and 'atWorldEdge', should return a boolean value indicating that this object should be removed from the world -- that is, a rock was eaten or the edge of the world was arrived at. For example, the code used previously within your 'Dead' method should go in the 'eatsRock' method (with some modifications) like so:
private boolean eatsRock()
{
    Actor rock = getOneIntersectingObject(Rock.class);
    if (rock != null)
    {
        getWorld().removeObject(rock);
        ((Start)getWorld()).getCounter().addScore();
        return true;
    }
    return false;
}
The 'atWorldEdge' method should similarly check for this actor being at an edge of the world and returning the proper boolean value for the determined state.
DarkSoulDemon DarkSoulDemon

2014/9/28

#
I have entered the code in but it comes up with an error with the at worlds edge were i need to make a method what would this method look like
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move();
       // This calls on the method code so that it works
       Dead();
       // This calls on the method code so that it works
    }
    public void move()
    {
      move(15); // this moves the Bullet straingh at the speed of 15
    }    
    
//     private void Dead()
//     {
//         Actor  Rock = getOneIntersectingObject(Rock.class);
//          if(Rock !=null)
//          {
//           World myWorld = getWorld();  
//           myWorld.removeObject (Rock); 
//            Start start = (Start)myWorld;
//           Counter counter = start.getCounter();
//           counter.addScore();
//           // This code means that when the enemy is killed it adds 1 to the score.
//           myWorld.removeObject(this);
//              // This code means that when the the Bullet hits the Rock the Rock will disapre
//         }
//      } 
     private void Dead()  
    {  
        if (eatsRock() || atWorldEdge()) getWorld().removeObject(this);  
    }  
    private boolean eatsRock()  
    {  
      Actor rock = getOneIntersectingObject(Rock.class);  
       if (rock != null)  
       {  
        getWorld().removeObject(rock);  
        ((Start)getWorld()).getCounter().addScore();  
        return true;  
      }  
      return false;  
    }  
}
    

davmac davmac

2014/9/28

#
If you are using the latest version of Greenfoot, you can use the isAtEdge method instead.
DarkSoulDemon DarkSoulDemon

2014/9/28

#
this comes up cannot find symbol - method isAtEdge
DarkSoulDemon DarkSoulDemon

2014/9/28

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move();
       // This calls on the method code so that it works
       Dead();
       // This calls on the method code so that it works
    }
    public void move()
    {
      move(15); // this moves the Bullet straingh at the speed of 15
    }    
    
//     private void Dead()
//     {
//         Actor  Rock = getOneIntersectingObject(Rock.class);
//          if(Rock !=null)
//          {
//           World myWorld = getWorld();  
//           myWorld.removeObject (Rock); 
//            Start start = (Start)myWorld;
//           Counter counter = start.getCounter();
//           counter.addScore();
//           // This code means that when the enemy is killed it adds 1 to the score.
//           myWorld.removeObject(this);
//              // This code means that when the the Bullet hits the Rock the Rock will disapre
//         }
//      } 
     private void Dead()  
    {  
        if (eatsRock() ||  isAtEdge()) getWorld().removeObject(this);  
    }  
    private boolean eatsRock()  
    {  
      Actor rock = getOneIntersectingObject(Rock.class);  
       if (rock != null)  
       {  
        getWorld().removeObject(rock);  
        ((Start)getWorld()).getCounter().addScore();  
        return true;  
      }  
      return false;  
    }  
}
    

DarkSoulDemon DarkSoulDemon

2014/9/28

#
what should i do
Super_Hippo Super_Hippo

2014/9/28

#
What version of Greenfoot do you use?
DarkSoulDemon DarkSoulDemon

2014/9/28

#
greenfoot version 2.3.0
davmac davmac

2014/9/28

#
davmac wrote...
If you are using the latest version of Greenfoot, you can use the isAtEdge method instead.
Latest version. Which is not 2.3.0.
DarkSoulDemon DarkSoulDemon

2014/9/28

#
okay so what do i use
DarkSoulDemon DarkSoulDemon

2014/9/28

#
what code do i use instead of atWorldsEdge
Super_Hippo Super_Hippo

2014/9/28

#
You can either write this method yourself or you update to the latest version where it is already implemented.
danpost danpost

2014/9/28

#
You can still use 'isAtEdge', but you will have to create the code for the method and add it to the class yourself. Obviously, it should have a return type of boolean and you will need to put a check in it for each of the four edges of the world. HINT: check for the 'x' or 'y' values that represent the edges of the world.
You need to login to post a reply.