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

2015/5/6

Switching the world after pressing "enter"

Gratz Gratz

2015/5/6

#
Hey guys! Iam new to programming and i want to create a little Mario game. So basically i have a Startscreen named "Start_Screen" with a little Cursor blinking. Now i want the player to press "enter" to load the first Level. The 1st Level is another World. So i tried this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (Greenfoot.isKeyDown("enter"))
        {
 
            startLvL1();
 
        }
 
 
    public void startLvL1 ()
    {
         
        Greenfoot.setWorld(new LvL_1());
                            
    }
But if iam going to run it by this way there will be no reaction to my enter press. The world doesnt switch! This Code was inserted on the code to the Start_Screen. Is this a mistake? I searched for an error a long time but i couldnt find one. Help me please :)
danpost danpost

2015/5/6

#
The code given from line 1 through line 6 appears to be misplaced. For that code to be continuously run to detect the keystroke, it needs to be within an 'act' method (provided by the World class; but not added to your class template). Refer to the World class documentation for information on that method
Gratz Gratz

2015/5/6

#
Thank you for replay. I read the link you sent in but i didnt got where the problem ist :( I think you mean that the act void is like a loop, and this doesnt work with the isKeyPress method?
Gratz Gratz

2015/5/6

#
Got another Idea, maybe i need to use this code with an actor and not with the world iam actually in?
danpost danpost

2015/5/6

#
The code you gave above does not appear to be within a method. The world class provides an 'act' method for performing actions and checks you need to do repeatedly (like checking for a keystroke -- it could happen at any one of a gazillion instances):
1
2
3
4
public void act()
{
    // your lines 1 through 6 here
}
Place your code checking for the keystroke inside an 'act' method (the method is pre-defined with no implementation in the World class and what you are doing is giving it some implementation -- or changing what it does; this is called overriding a method).
Gratz Gratz

2015/5/6

#
Oh man i solved it! The Problem was that i used the setWorld command in the code of a world! Now i created an invisible actor who spawn and waits until i press enter to switch the world =) Not the best solution but iam happ yunil there nothing better that works
Gratz Gratz

2015/5/6

#
Ah that! So i can remove my actor wherei have an act Method and create one for the world? Will try it!
Gratz Gratz

2015/5/6

#
Yup its doing work now! Thank you for helping me =)
danpost danpost

2015/5/6

#
Your welcome.
You need to login to post a reply.