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

2020/5/4

need help with bullet collision

scaps scaps

2020/5/4

#
I want to make it so that when the projectile hits the zombie they both disappear, however only the zombie disappears. How can I fix it. Zombie:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class green here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Green extends Actor
{
    /**
     * Act - do whatever the green wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        if(isTouching(projectile_1.class)){
            projectile_1 projectile= new projectile_1();
            getWorld().removeObject(projectile);
            getWorld().removeObject(this);
            
        }
    }    
}
Projectile:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

2020/5/4

#
Trying to remove a newly created projectile that not in the world, are we? Good luck with that. Replace lines 19 and 20 in Zombie code above with:
removeTouching(projectile_1.class);
scaps scaps

2020/5/5

#
Thanks it worked!
You need to login to post a reply.