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

2014/9/17

Does anyone know how to Remove the bullet when it gets to end of world

coder04 coder04

2014/9/17

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

/**
 * Write a description of class Shot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shot extends Mover
{
/    private int life
    
    /**
     * Act - do whatever the Shot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move(10.0);
       life--;
       if (life == 0)
       {
           getWorld().removeObject(this);
    }
}
}
danpost danpost

2014/9/17

#
You already have one condition for removing the actor. Since you want to add more, it seems logical to add any extra conditions to the 'if' statement on line 21:
if (life == 0 || /* one or more added conditions */)
Now, whatever you put there must represent a boolean value or a combination of boolean values. That is, you can string multiple conditions together. There are four edges to the world and, therefore, four more conditions for removing the actor. Just add those conditions using the OR, '||', conditional operator.
coder04 coder04

2014/9/17

#
I want the bullet to dissapear at the edge of the map but it keeps staying in the corner of the map


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

/**
 * Write a description of class Shot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shot extends Mover
{
    
    /**
     * Act - do whatever the Shot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move(10.0);
       {
           getWorld().removeObject(this);
       }
    }
}
FROZENbender FROZENbender

2014/9/17

#
this should help: paste this method below your act method
public void remove()
{
    if(getX() >= getWorld().getWidth() -1)
    getWorld().removeObject(this);
    else if(getX() <= 1)
    getWorld().removeObject(this);
    else if(getY() >= getWorld().getHeight() -1)
    getWorld().removeObject(this);
    else if(getY() <= 1)
    getWorld().removeObject(this);       
}
coder04 coder04

2014/9/17

#
The bullet dissapears to quick
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shot extends Mover
{
    
    /**
     * Act - do whatever the Shot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move(10.0);
       {
           getWorld().removeObject(this);
      }
    }
    public void remove()  
  {  
    if(getX() >= getWorld().getWidth() -1)  
    getWorld().removeObject(this);  
    else if(getX() <= 1)  
    getWorld().removeObject(this);  
    else if(getY() >= getWorld().getHeight() -1)  
    getWorld().removeObject(this);  
    else if(getY() <= 1)  
    getWorld().removeObject(this);         
  }  
}
danpost danpost

2014/9/17

#
coder04 wrote...
I want the bullet to dissapear at the edge of the map but it keeps staying in the corner of the map < Code Omitted >
Your 'if' statement totally disappeared. I would like to know what happened to it.
coder04 coder04

2014/9/17

#
I added || but did not work
danpost danpost

2014/9/17

#
coder04 wrote...
I added || but did not work
Please show what you tried that did not work.
coder04 coder04

2014/9/17

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
/** 
 * Write a description of class Shot here. 
 *  
 * @author (your name)  
 * @version (a version number or a date) 
 */  
public class Shot extends Mover  
{  
   private int life;  
      
    /** 
     * Act - do whatever the Shot wants to do. This method is called whenever 
     * the 'Act' or 'Run' button gets pressed in the environment. 
     */  
    public void act()   
    {  
       move(10.0);  
       life--;  
       if (life == 0 ||)  
       {  
           getWorld().removeObject(this);  
    }  
}  
}  
danpost danpost

2014/9/17

#
The conditional operator is used to string multiple conditions together. It must have a condition (or, a boolean value) on both sides of it; not just on one side.
coder04 coder04

2014/9/17

#
so what do i add to make it dissapear at end of the world
danpost danpost

2014/9/17

#
Just describe the state when it is at an edge of the world. For example: if the actor is at the top edge, then its y-location would be zero. Therefore, one condition would be 'getY() == 0'. String that and the other three conditions as instructed above within the 'if' argument.
coder04 coder04

2014/9/17

#
can u tell me the code please and where to add it
danpost danpost

2014/9/17

#
I believe you have all the pieces and the instruction. It is now up to you to put it together. You will not learn anything by code being just given to you.
coder04 coder04

2014/9/18

#
K
You need to login to post a reply.