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

2017/2/16

addedToWorld not being called

retrosnob retrosnob

2017/2/16

#
My actor's addedToWorld method isn't being called for some reason. Any ideas?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Word here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Word extends Actor
{
    String word;
    boolean isInWorld = false;

    public Word(String word) {
        // This method is called once when the object is instantiated
        this.word = word;
        GreenfootImage img = new GreenfootImage(this.word, 20, Color.BLACK, Color.WHITE);
        setImage(img);
    }

    /**
     * Act - do whatever the Word wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        setLocation(getX(), getY() + 3);
        if (getY() > 380) {
            isInWorld = false;
            getWorld().removeObject(this);

        }
    }    

    protected void addedToWorld(World world) {
        // This method is automatically called when an object is added to the world
        System.out.println("Added to world: " + word);
        isInWorld = true;
    }

    public boolean isInWorld() {
        return isInWorld;
    }
}
danpost danpost

2017/2/16

#
retrosnob wrote...
My actor's addedToWorld method isn't being called for some reason. Any ideas? < Code Omitted >
Please show your world class code (which is where I presume you have the Word object added into the world).
You need to login to post a reply.