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

2011/11/18

How to make a Gravity for the actor??

TopInPut TopInPut

2011/11/18

#
With the Gravity the Actor should move just on an objekt under him. How to make that?
giordanno92 giordanno92

2011/11/20

#
With the method from Greenfoot's library called getOneObjectAtOffset(); Michael Kölling has some video tutorials showing how to do so. But the idea is simple, create one class for the Actor and another class for the Ground (which I'll call Ground) you can create a boolean method named onGround() to test if the actor you wanna use is on Ground (true) or not (false). It will look like this:
1
2
3
4
5
public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 + 2, Ground.class);
        return under != null;
    }
If he is not on ground, you would want he to fall, so create another method for him to fall. vSpeed is the speed that he falls, and it's increase by the value of acceleration, which can be 1 or 2
1
2
3
4
5
public void fall()
    {
        setLocation (getX(), getY() + vSpeed);
        vSpeed += acceleration;
    }
Now to finish you just need a method to check if he's falling
1
2
3
4
5
6
7
8
9
public void checkFall()
    {
        if (onGround()) {
            vSpeed = 0;
        }
        else {
            fall();
        }
    }
I really recommend you to watch Michael Kölling's tutorial so you can know how to this better
delmar delmar

2011/11/20

#
The video that giordanno92 mentions is here: http://www.youtube.com/user/18km?ob=5#p/u/10/VAHDrJ069sI
TopInPut TopInPut

2011/11/20

#
Thx all.;)
BradH BradH

2013/1/1

#
Do you guys know which tutorial, is it one of the joy of code videos?
BradH BradH

2013/1/1

#
Never mind I found it
CassWin CassWin

2014/2/20

#
can any1 giv me the link of the video?? plzzz
You need to login to post a reply.