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

2014/10/29

Next level if certain coordinates

1
2
Super_Hippo Super_Hippo

2014/10/31

#
@Alwin: Well, I don't believe you can do something like this. A method is not an object which you can create with the 'new' keyword. @Slofstra: You probably don't have a reference to the objects player1 and player2. Instead, you just call the methods on the classes which isn't possible like this as you see. Save a field in your world for each player and when creating the objects, you set them to the fields. Then you can call the methods on the specified objects.
Player1 player1;
Player1 player2;

//... when creating

player1 = new Player1();
player2 = new Player2();

addObject(player1, /* x */, /* y */);
addObject(player2, /* x */, /* y */);


//in the act method, use what danpost posted
danpost danpost

2014/10/31

#
When I suggested the way to code it, I used 'player1' and 'player2' as references to the Player1 and Player2 objects. They can be stored in fields as suggested above by Super_Hippo; or, you can get the references on the fly with something like this before executing the 'if' statement::
Player1 player1 = (Player1)getObjects(Player1.class).get(0);
Player2 player2 = (Player2)getObjects(Player2.class).get(0);
However, either way you go, both players must be in the world when you execute the 'if' statement. So, the following checks must be performed as well before checking if the actors are at the finish area:
if (!getObjects(Player1.class).isEmpty() && !getObjects(Player2.class).isEmpty())
before acquiring the references, if using my two lines above; or with
if (player1.getWorld() != null && player2.getWorld() != null)
if using the way suggested by Super_Hippo.
Alwin_Gerrits Alwin_Gerrits

2014/10/31

#
Ow right, I read over that part... so yeah it should be new player like suggested above then... Do you have your answere with that Slofstra?
M_Slofstra M_Slofstra

2014/10/31

#
I feel real stupid now. I should've practiced first with greenfoot before diving into deep. This is wayyyy over my head. But now I've come so far with my game, I must succeed! I think that once everything works, I can study the code, and learn that way. So I want to ask a big favor.. Could you please tell me what code I should place in my World.class and what code in my player.classes? As soon as the game is finished (or at least a part of) I will put the game online! I actually forgot to tell you guys what kind of game I'm making. It's kinda like Thomas Was Alone. You're are 2 shapes, who must help eachother to the end off the level. One shape is taller than the other, the other is wider. (Let's say 2x1 and 1x2)
danpost danpost

2014/11/1

#
All parts needed are all given above in my last posting along with that given in my fourth posting in this discussion thread. The earlier posting had three steps involved. The first two of these were straight-forward. The last one goes along with the code in the latter posting in your world class. Have the act method in your world class (if you do not currently have one, add one as a 'public void act()') call a method called 'checkFinish' and add the following method to the same class:
public void checkFinish()
{
    if (getObjects(Player1.class).isEmpty() || getObjects(Player2.class).isEmpty()) return;
    // the above line exits the method if either of the two players are not in the world
    Player1 player1 = (Player1)getObjects(Player1.class).get(0);
    Player2 player2 = (Player2)getObjects(Player2.class).get(0);
    // the previous two lines get references to the two players
    if (player1.atFinish() && player2.atFinish()) Greenfoot.setWorld(new Level2());
    // this last line sets the next level active if both players are at finish area
}
Alwin_Gerrits Alwin_Gerrits

2014/11/1

#
Jumping into the deep really isn't a bad thing to do. I did so myself and got prety far with my game as well. Just ask for help on the forum and usually you get some prety good responds. Gotto hand it to people like danpost, davmac and Hippo. They really help you out when you need it.
M_Slofstra M_Slofstra

2014/11/6

#
Thanks for your help guys, I finally managed to upload the first version, after days without internet. This is the URL to my game: http://www.greenfoot.org/scenarios/12469 You're right, Alwin_Gerrits. But it's kinda strange that teachers won't help much, so I'm really happy that you guys exist :p
You need to login to post a reply.
1
2