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

2018/6/1

How do I make this actor, addObject of some other actor?

JOELwindows7 JOELwindows7

2018/6/1

#
I have an Actor here:
public class DoSomething extends Actor{
     //constructor
     public DoSomething(){
          getWorld().addObject(new otherObject, 100, 100);
     }
}
then I add this Actor to the world. it shows error!
java.lang.NullPointerException
, pointing that addObject method. must be that I can't do it like this. but I use getWorld() to showText("like this string", X, Y) on an Actor, no error.
getWorld().showText("Hello World", 100, 100);
what error I made? thx. point: Make this actor spawn an object like the World abstract does, but an actor did this.
danpost danpost

2018/6/1

#
Whenever you call some method or constructor to execute, you MUST include parenthesis after the block name:
getWorld().addObject(new otherObject(), 100, 100);
Notice them: new otherObject().
danpost danpost

2018/6/1

#
Also, you cannot use getWorld in a constructor unless you add the actor into the world within that constructor before calling getWorld. The actor must be added into a world before getWorld will return a World object instead of null. Override the addedToWorld(World) method so that the actor can add the other actor into the world this actor was added into:
protected void addedToWorld(World world)
{
    world.addObject(new otherObject(), 100, 100);
}
Elusieemmanuel Elusieemmanuel

2018/6/2

#
where can i write my codes please?
JOELwindows7 JOELwindows7

2018/6/2

#
danpost wrote...
Whenever you call some method or constructor to execute, you MUST include parenthesis after the block name:
getWorld().addObject(new otherObject(), 100, 100);
Notice them: new otherObject().
danpost wrote...
Also, you cannot use getWorld in a constructor unless you add the actor into the world within that constructor before calling getWorld. The actor must be added into a world before getWorld will return a World object instead of null. Override the addedToWorld(World) method so that the actor can add the other actor into the world this actor was added into:
protected void addedToWorld(World world)
{
    world.addObject(new otherObject(), 100, 100);
}
Thx for help. I will update my code later. Duh, I forget pare thesis. I will share the result whenever I do. Stay tuned.
Pyrozen Pyrozen

2018/6/4

#
Elusieemmanuel wrote...
where can i write my codes please?
You can use this feature to write code like
this
JOELwindows7 JOELwindows7

2018/6/6

#
Update: Great News! This is what I do here. . Here is Actor, DoSomething():
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class DoSomething here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DoSomething extends Actor
{
    public DoSomething(){
          //getWorld().addObject(new otherObject(), 100, 100);
     }
     protected void addedToWorld(World world)
     {
         world.addObject(new otherObject(), 100, 100);
       }
    /**
     * Act - do whatever the DoSomething 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.
        if(Greenfoot.mouseClicked(this)){
            //addedToWorld(getWorld());
        }
        
    }    
}
//Okay addedToWorld() ran automaticly by some Constructions and who is that?
//What is addedToWorld() again? let me read documentation.
Then, when I do
DoSomething dosomething = new DoSomething();
        addObject(dosomething,305,213);
into MyWorld(), so it results like:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        DoSomething dosomething = new DoSomething();
        addObject(dosomething,305,213);
    }
}
Whenever that DoSomething actor gets added into the world, it will execute the function, like you said, which because that addedToWorld() method has been overidden by mentioned actor. No error! IT WORKS!!! THANK YOU SO MUCH!!! please set this thread solved. if there is any question however, feel free. Greenfoot, Please don't detect spam this way where I have to wait 24 hours. if the the thread got expired, everyone will think helpers or programmers are ignorant!
danpost danpost

2018/6/6

#
JOELwindows7 wrote...
Greenfoot, Please don't detect spam this way where I have to wait 24 hours. if the the thread got expired, everyone will think helpers or programmers are ignorant!
They want to make sure you are not spamming or promoting another site. This only happens the first time you add a link into a post (usually).
You need to login to post a reply.