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
M_Slofstra M_Slofstra

2014/10/29

#
Hi everybody, I guess my title says it all. I need a code for this, but I can't figure it out myself. To be short, this is my question. If between certain coordinates then: next level If not, stay in same level
Super_Hippo Super_Hippo

2014/10/29

#
if (getX()>/*...*/&&getX()</*...*/&&getY() /*...*/&&getY()</*...*/)
{ //add the coordinates ↑
    Greenfoot.setWorld(new Level(level+1)); //or however you do it
}
M_Slofstra M_Slofstra

2014/10/29

#
Thank you for the very fast responce, Super_Hippo! Unfortunately, your solution didn't work for me. I may have made a mistake, for i am only a newbie. This is your code, after editing:
public void finish()
{    if (getX()>861 &&getX()<963 &&getY()>100 &&getY()<200)  
 //add the coordinates ↑  
Greenfoot.setWorld(new Level2()); //or however you do it  
} I'm pretty sure the coordinates are right. When my Actor reaches the box, it doesn't go to the next level. Nothing happens. Can you help me?
Super_Hippo Super_Hippo

2014/10/29

#
Do you call this method from the act-method?
M_Slofstra M_Slofstra

2014/10/30

#
Now it works, thanks alot for your help! Now I have one final question. I have 2 actors who have to be between the same coordinates, before going to the next level. How do I fix that? Now only one actor has to be in the coordinates.
danpost danpost

2014/10/30

#
If you want to add more conditions to go to the next level, just add more conditions (do not make a new 'if' statement; but, add the conditions to the one you already have).
M_Slofstra M_Slofstra

2014/10/30

#
danpost, I know that I have to add more conditions, but I don't know how to add conditions about another actor. I tried several things, with wild guessing, but it didn't work. This is the code I tried. I've added this code to my Player1, without the stuff about Actor.Player2. I used the same code for my Player2. How can I add them in one code, that both players have to be in the same coordinates?
public void act()
{
 getDirection();
move();
if (getX()>861 &&getX()<963 &&getY()>100 &&getY()<200 &&(Actor.Player2 (getX()>861 &&getX()<963 &&getY()>100 &&getY()<200)))
           
//add the coordinates ↑  
Greenfoot.setWorld(new Level1()); //or however you do it  
        
}
danpost danpost

2014/10/30

#
Ok, I understand your dilemma. Since you are working with two different actors, you need to place the code in a location with more scope (one that can encompass both players). The logical location would be in your world class, since both players would be in the world. Therefore you need an act method in your world class that calls a method in the same class that performs the checks and responds to the results. This means that you will remove those checks from the classes of the players. Attempt what I have suggested and if you run into issues, they can be discussed.
danpost danpost

2014/10/30

#
It may be more logical for you to have 'public boolean' methods in the classes of your players to return the state of being at the locations required and have the method in your world class call them.
M_Slofstra M_Slofstra

2014/10/31

#
To be honest, I have no idea how to do this. I've watched many videos now, and I still don't know how to do this. Teachers at my school say I should ask the internet how to do it (I'm 19, for the record). So here I am again.. Can you give me an example of how it works to let players do something in the world class? I guess I can figure it out how do it with the coordinates.
danpost danpost

2014/10/31

#
First, change the act method of your players to just this:
public void act()
{
    getDirection();
    move();
}
Now, add to both player classes the following method
public boolean atFinish()
{
    return getX() > 861 && getX() < 963 && getY() > 100 && getY() < 200;
}
Now the world act method can use something like this:
if (player1.atFinish() && player2.atFinish()) Greenfoot.setLevel(new Level2());
M_Slofstra M_Slofstra

2014/10/31

#
I've copied everything above, and now I get the error: Non static method atFinish() cannot be referenced from a static context. What should I do now?
Alwin_Gerrits Alwin_Gerrits

2014/10/31

#
Try this:
atFinish finish = new atFinish()
After that piece of code replace the use of atFinish by finish So if you have a statement like this:
if(atFinish())
Simply replace it by this:
if(finish())
If I'm correct that should fix it. What you're doing if you use this is creating a new method 'atFinish' which is now called 'finish'. So when calling it because you want it to return it's boolean value you call 'finish' instead of 'atFinish'
Alwin_Gerrits Alwin_Gerrits

2014/10/31

#
Obviously if people know Greenfoot better feel free to correct me :)
M_Slofstra M_Slofstra

2014/10/31

#
Alwin_Gerrits, where should I place your code (atFinish finish = new atFinish() )?
There are more replies on the next page.
1
2