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

2017/2/10

so i want to add an object into the world in a specific location once

Ruv25808 Ruv25808

2017/2/10

#
Hi. I tried to use the class constructor but it keeps throwing the error that the actor isn't in the world. BTW I'm trying to make a ring of asteroids around the actor. Any suggestions? :D
Nosson1459 Nosson1459

2017/2/10

#
if you would post some code it would make things a lot easier
Super_Hippo Super_Hippo

2017/2/10

#
The constructor is executed before the object is added to the world. Maybe this is doing what you want.
protected void addedToWorld(World w)
{
    int x=getX(), y=getY(), num=10;
    Asteroid a;
    for (int i=0; i<num; i++)
    {
        a = new Asteroid();
        w.addObject(a, x, y);
        a.setRotation((int)(i*360/num));
        a.move(20);
        a.setRotation(0);
    }
}
Nosson1459 Nosson1459

2017/2/10

#
What are you trying to do? Be more specific. If you want to add an object into the world in a specific x and y put the following in your world:
addObject(new ActorName(), x, y);
If you want to add something each time this actor is added. like for instance you want that every time you add a tree an apple will get added to the world also then make a method in this actor like this:
public void addedToWorld(World world)
{
    world.addObject(...
}
I didn't see the other two posts until after I posted mine and the page got reloaded. (There are two people that use the user Nosson1459, this has been mentioned in the past)
You need to login to post a reply.