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

2016/11/7

Object floating above my platform and not touching

Jochends Jochends

2016/11/7

#
If i try to let my object( a person ) fall on the ground he is floating a little above my platform.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class mannetje here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class mannetje extends Actor
{
    
    private int speed = 3; //snelheid( in pixels ) waarmee het mannetje horizontaal loopt.
    private int vSpeed = 0; //V staat voor verticale speed, dus springen en vallen.
    private int acceleration = 2; //het mannetje gaat hierdoor vallen zoals bij de zwaartekracht.
    
public void act() // controleert mijn toetsenbord inputs en doe de act precies.
{
   checkKeys();
   vallen();
   checkVallen();
}

private void checkKeys()
{
if (Greenfoot.isKeyDown("left") ) // Als ik de linker keyarrow indruk gaat de afbeelding van het man-links laden en de methode moveLeft aanroepen
{
   setImage("man-links.png");
   moveLeft();
}
if (Greenfoot.isKeyDown("right") ) // Als ik de rechter keyarrow indruk gaat de afbeelding van het man-rechts laden en de methode moveRight aanroepen
{
   setImage("man-rechts.png");
   moveRight();
}
}

public void checkVallen()
{
    if(onPlatform()) {
        vSpeed = 0;
    }
    else {
        vallen();
    }
}

public boolean onPlatform()
{
    Actor under = getOneObjectAtOffset (0, getImage().getHeight() [b]/ 2[/b], Platform.class);
    return under != null;
}

public void vallen() // de speed staat nu bij de Y-as wat verticaal is
{
    setLocation (getX(), getY() + vSpeed);
    vSpeed = vSpeed + acceleration; // hierdoor gaat het mannetje realistischer vallen( zwaartekracht en niet zwevend )
}

public void moveRight() // de speed staat bij de X-as wat horizontaal is
{
    
    setLocation (getX() + speed, getY());
}

public void moveLeft()
{
    setLocation (getX() - speed, getY());
}
}
The problem seems to be @
{
    Actor under = getOneObjectAtOffset (0, getImage().getHeight() [b]/ 2[/b], Platform.class);
    return under != null;
}
The /2 i need to change to /2.5 or something but i can only choose /3 (in the floor) or /2 (above the floor)
danpost danpost

2016/11/7

#
It sounds like the image of the person has excess transparency around the real image which is causing detection of the Platform object early. Either remove the extra transparency so that each edge of the rectangular frame the image is in has at least one non-transparent pixel or just do /2-5 or /3+5 (adjust the '5' as needed).
Jochends Jochends

2016/11/8

#
Thanks, That helped!
You need to login to post a reply.