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

2016/11/8

Can't jump

Jochends Jochends

2016/11/8

#
I tried alot of coding but can't let my object jump.. I'm a newb on this so can someone tell me my mistakes and how to fix them? ( showing the correct code to see where i'm wrong )
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.
    private int gravity;
    public boolean onPlatform;
    
public void act() // controleert mijn toetsenbord inputs en doe de act precies.
{
   gravity--;
   checkForJump();
   checkKeys();
   vallen();
   checkVallen();
}

private void checkForJump()
{
    if (onPlatform && Greenfoot.isKeyDown("space"))
    gravity = 20; // this will make the character jump
}

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() /* De plaats waar het object zich gaat plaatsen */
{
    Actor under = getOneObjectAtOffset (0, getImage().getHeight() / 2-6, 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());
}
}
Thx
Super_Hippo Super_Hippo

2016/11/8

#
I think you have to explain what gravity and acceleration should do. I mean, gravity is an acceleration. Also, gravity is never used when falling. It is only decreased every act cycle and set back to 20 when it starts (should start) to jump, it doesn't affect anything as it is right now.
Jochends Jochends

2016/11/9

#
I can jump now, but if i run the world he is vibrating a few inches above the platform and after the jump he comes fast down( still above the platform ). Seems i am making a mess of it but want to learn what i'm doing wrong. Am i using to much code now or where am i doing it wrong? I'm very new @ this.
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 = 1; //het mannetje gaat hierdoor vallen zoals bij de zwaartekracht.
    private boolean jumping;
    private int jumpStrength = 16;
    
public void act() // controleert mijn toetsenbord inputs en doe de act precies.
{
   checkKeys();
   vallen();
   checkVallen();
}

public 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();
}

if (Greenfoot.isKeyDown("space") && jumping == false){
     jump();
}
}

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

public boolean onGround() /* De plaats waar het object zich gaat plaatsen */
{
    Actor ground = getOneObjectAtOffset (0, getImage().getHeight() / 2-6, Platform.class);
    if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
}

public void moveToGround(Actor ground)
{
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
        jumping = false;
}
    
public void vallen() // de speed staat nu bij de Y-as wat verticaal is
{
        setLocation(getX(), getY() + vSpeed);
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + acceleration; // hierdoor gaat het mannetje realistischer vallen( zwaartekracht en niet zwevend )
        }
        jumping = true;
}

public void jump(){
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        vallen();
}

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

public void moveLeft()
{
    setLocation (getX() - speed, getY());
}
}
danpost danpost

2016/11/9

#
You should probably not call 'vallen' in the 'jump method. It is already being called by the 'act' method immediately after calling 'checkKeys' which will call 'jump', if needed. Also, you should be able to just set 'vSpeed' to '-jumpSpeed' at line 88 (instead of adjusting by that amount); the value of 'vSpeed' should be zero (or very close to it) when on ground and able to jump. Finally, at line 72, you use the full ground image height in the placement of the actor; however, it should only be half that ( 'groundHeight/2' ).
Jochends Jochends

2016/11/10

#
Hi danpost, Thanks for the reply. I can now jump, but i am still floating some inches above the platform. I noticed that if i change (line 72)
int newY = ground.getY() - (groundHeight[b]/2[/b] + getImage().getHeight())/3;
to
int newY = ground.getY() - (groundHeight[b]/7[/b] + getImage().getHeight())/3;
he isn't floating anymore, but then i also can't jump anymore. How does it come i need to change the /2 to /7 for not floating? Also on (line 88) he doesn't recognize "-jumpSpeed" or did u meant the following?
public void jump(){
        vSpeed = -jumpStrength;
        jumping = true;
}
Thanks in advance
danpost danpost

2016/11/10

#
Jochends wrote...
Also on (line 88) he doesn't recognize "-jumpSpeed" or did u meant the following?
public void jump(){
        vSpeed = -jumpStrength;
        jumping = true;
}
That is exactly what I meant.
How does it come i need to change the /2 to /7 for not floating?
Remove the parenthesis from around what is currently the subtrahend. You do not want to subtract half the height of the jumper -- you want to add it.
Jochends Jochends

2016/11/10

#
Omg i found my big mistake.. It's maybe good to share this with others.. I found a platform(ground) image where he stands on and made it transparant( because it had a white background ). But their was still a few inches of transparency above the ground that i didnt cut in my program. So he was just standing on the 'invisble' part of that image. Thanks anyway for the improvement of my code
You need to login to post a reply.