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

2018/7/22

how to move an actor from world to world

rookiecoder rookiecoder

2018/7/22

#
Forgive give me my limited coding knowledge. I am taking a class and need to make a game where an actor named "Dave" needs to move to different worlds. I am unsure on how to make "Dave" be able to move to a separate world when he hits a selected part of the screen. (If I explained something poorly please ask for clarification) Thanks in advance!!!
Super_Hippo Super_Hippo

2018/7/22

#
You can either pass the Dave object to the world and add that object there or you can have a static reference to the Dave object, so you have access to it from anywhere and can add it to any world.
rookiecoder rookiecoder

2018/7/22

#
I get that but what would the syntax look like for that
Super_Hippo Super_Hippo

2018/7/23

#
Option 1:
//World1
private Dave dave;

//adding it
dave = new Dave();
addObject(dave, <x-coordinate>, <y-coordinate>);

//changing world
Greenfoot.setWorld(new World2(dave));
//World2
private Dave dave;
public World2(Dave dave)
{
    <super(…);>
    this.dave = dave;
    addObject(dave, <x-coordinate>, <y-coordinate>);
}
Option 2:
//World1
private static Dave dave;

public static Dave getDave() {return dave;}

//adding it
dave = new Dave();
addObject(dave, <x-coordinate>, <y-coordinate>);

//changing world
Greenfoot.setWorld(new World2());
//World2
addObject(World1.getDave(), <x-coordinate>, <y-coordinate>);
You need to login to post a reply.