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

2013/1/18

fallUntilGround

moobe moobe

2013/1/18

#
Hi, I've programmed a method "fallUntilGround". Here's the code:
public void fallUntilGround()
    {
        if (! onGround())
        {
            checkFall();
            fallUntilGround();
        }
    }
As you can see, the code is really simple. But when I implement it in my game, my actor "jumps" on the ground without showing me all the frames. Do you guys know where the problem is?
danpost danpost

2013/1/18

#
You are calling the method from within itself. This, in effect, will perform the complete jump in one frame.
moobe moobe

2013/1/18

#
Found the problem on my own: I have to write something with "Greenfoot", for example: Greenfoot.delay(1); But why? I can't understand the problem -.-
moobe moobe

2013/1/18

#
@danpost: Thanks for your help! The way with the "Greenfoot delay" works now, but then I can only move one actor at time. How would you solve this problem?
danpost danpost

2013/1/18

#
You know, in all the scenarios I have written, I have never used the 'delay' method from the Greenfoot class API. It is because I have never found a purposeful use for it. Are you required to use this particular method, or any method from the Greenfoot class? No, I take that back. I did use it in my 'Private Messaging' scenario while reading in the UserInfo information; this was to allow other (background) processes a chance to run their course because of the time required to read in the data.
moobe moobe

2013/1/18

#
I think so.. Otherwise it will execute the fall in one frame. My idea: I want to make my actor fall until he stays on the ground, because after that starts a dialogue between two actors. So my actor has to fall until he stays on ground, so that I can add balloons to the world ;) So is this the best idea now?
danpost danpost

2013/1/18

#
Remove line 6 from the 'fallUntilGround' code above. Then call the 'fallUntilGround' method from the 'act' method of that class:
public void act()
{
    fallUntilGround();
    // rest of act code
}
The 'act' method is an empty method inherited from the Actor class. It is executed once every frame while the actor is in the world. BTW, the World class also has one and is executed every frame while the world is the active world.
moobe moobe

2013/1/18

#
I think this won't work, because the "fallUntilGround" method has to be used only at a single moment in the whole game. Check out my game I've released. The method is called when Snowman reaches the point in which he shall talk to Santa.
danpost danpost

2013/1/18

#
You already have gravity implemented for the snowman. You should not need do any more, unless I do not understand exactly what you are trying to do here.
moobe moobe

2013/1/18

#
OK, it doesn't matter, it works fine since my third post. But it isn't a nice style ;)
 if (level==2 && getX() == getWorld().getWidth()/2)
        {
            fallUntilGround();  

//Here comes the dialogue now

Sprech1 sprech1 = new Sprech1();
            getWorld().addObject(sprech1, 564, 330);
            while (!"space".equals(Greenfoot.getKey()))
            {
                Greenfoot.delay(1);
            }
            getWorld().removeObject(sprech1);
            Sprech2 sprech2 = new Sprech2();
            getWorld().addObject(sprech2, 586, 208);            
            while (!"space".equals(Greenfoot.getKey()))
            {
                Greenfoot.delay(1);
            }
            getWorld().removeObject(sprech2);
            Sprech3 sprech3 = new Sprech3();
            getWorld().addObject(sprech3, 569, 282);            
            while (!"space".equals(Greenfoot.getKey()))
            {
                Greenfoot.delay(1);
            }
            getWorld().removeObject(sprech3);
            Sprech4 sprech4 = new Sprech4();
            getWorld().addObject(sprech4, 638, 270 );            
            while (!"space".equals(Greenfoot.getKey()))
            {
                Greenfoot.delay(1);
            }
            getWorld().removeObject(sprech4);
            Level2 lev = (Level2) getWorld();
            lev.moveSantaRight();
            setLocation ( getX() + 1, getY() );
This didn't work, because the Snowman falls in only one frame then. But when I add "Greenfoot.delay(1);" in the fallUntilGround-method, it works the way I want to. But never mind, it's OK now :)
You need to login to post a reply.