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

2018/6/25

Object deleting itself after reaching edge of World

dr_shu2 dr_shu2

2018/6/25

#
Hi! I'm like some help on making an object delete itself after reaching the end of the world. Here is my code so far:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Gold_Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gold_Ball extends Actor
{
    /**
     * Act - do whatever the Gold_Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
   
    public void act() 
    {
        move(4);
        if(isAtEdge()){
        //what do I do here
       
        
        }
    }    
}
Agent40 Agent40

2018/6/25

#
This might help.
if (isAtEdge())
{
     getWorld().removeObjects(getWorld().getObjects(Object.class));
}
danpost danpost

2018/6/26

#
dr_shu2 wrote...
I'm like some help on making an object delete itself after reaching the end of the world. << Code Omitted >>
Try the following at line 21:
getWorld().removeObject(this);
dr_shu2 dr_shu2

2018/6/26

#
Thank you both for your support! I'm good to go now :)
You need to login to post a reply.