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

2016/9/30

Problems with falling and getObjectAtOffset

Tritos Tritos

2016/9/30

#
Hey Greenfoot community, I have recently started "programming" with Greenfoot 3.0.2 (Java version 1.8.0_66). I am not very good yet so please keep that in mind. I tried to write a simple "Jump 'n' Run" type of game with basic gravity and collision detection. However I have run into some major problems. First of all let me add my code: World Generation:
public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 900, 1); 
        create();
    }
    
    public void create()
    {
        Player1 p1 = new Player1();
        addObject(p1, 450, 50);
        
       
        for(int k = 25; k <= 1000; k = k + 50)
        {
            Ground g1 = new Ground();
            addObject(g1, k, 875);
        }
    }
Player Falling Code:
int vSpeed = 0;
    int gravity;
    int speed = 10;
    int jumpHeight = 20;
    int fallAcceleration = 2;
    
    boolean collision;
    int collisionLength;
    
    public void act() 
    {
        checkKeys();
        checkFall();
    }   
    
    public void checkFall()
    {
        if(!isTouching(Ground.class))
        {
            fall();
        }
    }
   
    public void fall()
    {  
        collision = false;
        for(int i = 0; i <= vSpeed; i = i++)
        {
            if(!getObjectsAtOffset(getX(), getY() + i, Ground.class).isEmpty()) 
            {
                collision = true;
                collisionLength = i;
            }
        }
        
        if(collision)
        {
            setLocation(getX(), getY() + collisionLength);
        }
        else
        {      
            setLocation(getX(),getY()+vSpeed);
            vSpeed = vSpeed + fallAcceleration;
        }
    }
My problems seem to have something to do with "getObjectAtOffset()". The Method detected Ground in midair and stopped the character. I had the "Game" on a USB stick and used it on my pc at home where even more problems came into play. The character stopped falling at all and after I tried to reset the programm would just load very long until I closed it. Also in my Player1.class it says "Next Error" in the top left corner, but nothing happens if I press the button. Many Thanks for helping Tritos
danpost danpost

2016/9/30

#
The increment for the 'for' statement on line 27 seems strange. Try either:
for (int i=0; i<=vSpeed; i++)
// or
for (int (int i=0; i<=vSpeed; i=i+1)
If the problem still persists, make sure your image for the player does not have excessive transparency at its bottom edge. Any transparent part of an image is still part of the image and is considered an intersecting area.
Tritos Tritos

2016/10/1

#
Hey danpost, thanks for the help. The first version actually fixed my problem. I also see the problem now. The " i= i++" instead of "i++" probably comes from experimenting with values and I forgot to remove the "i=". However I noe have a different problem. The player falls into the ground and then seems to "glitch around".
danpost danpost

2016/10/1

#
There are two thing you should do when ground is detected. The first is to set the vertical speed value to zero; the other is to reposition the player so that it is above the platform (presuming that the player falls in a downward direction into the platform).
Tritos Tritos

2016/10/1

#
Ok, I changed the "if (collision part)" so that it also sets the vSpeed to 0. However it now does a weird stop mid-air, then keeps on falling and glitches into the platform like before...
Super_Hippo Super_Hippo

2016/10/1

#
Use this line after line 32: (or at the end of this if-block, you added a new line there now)
break;
Tritos Tritos

2016/10/1

#
Thanks for the help. I added the line but it only made thinks worse.... ;) I can now walk mid-air on the left half of the level but when I enter the right half I fall and get stuck in the ground again....
Super_Hippo Super_Hippo

2016/10/1

#
Try this:
public void fall()
{
    for (int i=1; i<vSpeed+1; i++)
    {
        setLocation(getX(), getY()+1);
        if (isTouching(Ground.class))
        {
            setLocation(getX(), getY()-1);
            vSpeed = 0;
            return;
        }
    }
    vSpeed += fallAcceleration;
}
You should be able to just call the 'fall' method from the act method and remove the 'checkFall' method. (Maybe rename it, if you want.)
Tritos Tritos

2016/10/1

#
Thank you very much. That solution works perfectly and is exactly what I searched for!
You need to login to post a reply.