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

2018/4/13

Hoe to add an object into the World?

TomazVDSN TomazVDSN

2018/4/13

#
Hi guys. I have been out of the loop for many months I hope to learn Greenfoot again. I created a World with a single object names karate1 in it.
public class Dojo extends World{
    public Dojo() {    
        super(550, 400, 1); 
        prepare();
    }
    
    public void prepare() {
        Karate1 k1 = new Karate1();
        addObject(k1, 225,250);
    }
}
Then under the object class, I wrote the following code:
public class Karate1 extends Actor {
    public void act() {
       if (Greenfoot.isKeyDown("e")) {
           getWorld().removeObject(this);
       }       
    }
}
So far so good. When I run the code and press the key "e" down the object is removed from the world. Then I created a second object Karate2 and wrote the following code:
public class Karate2 extends Actor{
    public void act(){        
        if (Greenfoot.isKeyDown("c")){
            Karate2 k2 = new Karate2();
            getWorld().addObject(k2,225, 250);            
        }
    }    
}
It doesn't reposnd I was expecting the object Karate2 be added to the world. Is there anyone that could give some directions?
danpost danpost

2018/4/13

#
An Actor subclass act method only executes for actors created from the class that are already in the world. Just writing an act method in a class that extends Actor does not automatically have the code in the method run. Move the act method of the Karate2 class to your Dojo class, removing 'getWorld().' from line 5.
danpost danpost

2018/4/13

#
You will probably then find that multiple Karate2 objects will be created quickly. To limit to only one Karate2 object in the world at a time, change the if statement to:
if (Greenfoot.isKeyDown("c") && getObjects(Karate2.class).isEmpty()){
TomazVDSN TomazVDSN

2018/4/14

#
danpost, thank you !
TomazVDSN TomazVDSN

2018/4/16

#
Hi, danpost. I want to addObject "a WaterMelon" to the world programmatically, where should I put the method? The code below is not working.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Karate fighter will kick watermelons
 * 
 * @author Tom
 * @version 1.0
 */
public class Dojo extends World
{

    long initialTime = System.currentTimeMillis();
    long elapsedTime = 0;

    public Dojo()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        
        addObject(new Standing(), 250, 270);
        
        WaterMelon w1 = new WaterMelon();
        addObject(w1, 5, 180);
        
        WaterMelon w2 = new WaterMelon();
        addObject(w2, 5, 250);
    }
    
    public void Act(){
        addMelonToWorld(5000); 
    }
   
    public void addMelonToWorld(long elipsedTimeSet)
    {
        elapsedTime = System.currentTimeMillis() - initialTime;
        if(elapsedTime >elipsedTimeSet)
        {      
            WaterMelon wm = new WaterMelon();
            addObject(wm, 5, 330);
            
            elapsedTime =0;
            initialTime = System.currentTimeMillis();
        }
    }
}
You need to login to post a reply.