Hello
I have a problem with my character because when I hold the "up" key to jump, my character goes to the top. While I would like him to do a jump and then come back down like in real life when I hold the "up" key.
Thanks in advance to anyone who finds my error.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class Mario1 here. * * @author * @version (a version number or a date) */ public class GMario extends Mario { private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image3; private GreenfootImage image4; private int speed; private int vSpeed; private int acceleration; private int jumpStrenght; private int animTimer; public GMario() { image1 = new GreenfootImage("marioR1.png"); image2 = new GreenfootImage("marioL1.png"); image3 = new GreenfootImage("marioR2.png"); image4 = new GreenfootImage("marioL2.png"); setImage(image1); speed = 3; vSpeed = 0; acceleration = 1; jumpStrenght = 2; } /** * Act - do whatever the Mario1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkPress(); checkObstacle(); checkFall(); checkCollision(); detection(); } private void checkPress() { int dx = 0; if (Greenfoot.isKeyDown("right")) { dx++; } if (Greenfoot.isKeyDown("left")) { dx--; } if (dx != 0) { setLocation(getX()+dx*speed, getY()); checkObstacle(); animate(dx); } if (Greenfoot.isKeyDown("up")) { vSpeed = -jumpStrenght; fall(); } } private void animate(int dir) { animTimer = (animTimer+1)%20; if (dir > 0) { setImage(animTimer/10 == 0 ? image1 : image3); } else { setImage(animTimer/10 == 0 ? image2 : image4); } } public void checkObstacle() { // check above the actor while (getOneObjectAtOffset(0, -getImage().getHeight()/2-1, Solid.class) != null) { setLocation(getX(), getY()+1); } // check to left of actor while (getOneObjectAtOffset(getImage().getWidth()/2+1, 0, Solid.class) != null) { setLocation(getX()-1, getY()); } // check to right of actor while (getOneObjectAtOffset(-getImage().getWidth()/2-1, 0, Solid.class) != null) { setLocation(getX()+1, getY()); } } public void jump() { vSpeed = -jumpStrenght; fall(); } public void checkFall() { if(onSolid()) { vSpeed= 0; } else if (onPlateform()) { vSpeed= 0; } else { fall(); if (getY() == 0) { Greenfoot.stop(); } } } public boolean onSolid() { Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2, Solid.class); return under != null; } public boolean onPlateform() { Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2, Plateform.class); return under != null; } private void fall() { setLocation(getX(), getY() + vSpeed); if(vSpeed < 10) { vSpeed = vSpeed + acceleration; } } public boolean onEnemies() { Actor under = getOneObjectAtOffset(getImage().getWidth()/2, getImage().getHeight()/2, Enemies.class); if( under != null) { return true; } else { return false; } } public void detection() { int distSautX = 0; int distSautY = 20; int w = getImage().getWidth()/2; int h = getImage().getHeight()/2; List<Enemies> myListEnemies=getWorld().getObjects(Enemies.class); for (Enemies myEnemy:myListEnemies) { if ((getX()> myEnemy.getX()- myEnemy.getImage().getWidth()/2 - distSautX - w) && (getX() < myEnemy.getX() + myEnemy.getImage().getWidth()/2 + distSautX + w)) { if(getY() < myEnemy.getY() + myEnemy.getImage().getHeight()/2 + distSautY - h) { getWorld().removeObject(myEnemy); break; } //if(getY() >= myEnemy.getY() - myEnemy.getImage().getHeight()/2) //{ // getWorld().removeObject(this); // break; //} } } } public void checkCollision() { if (isTouching(Coin.class)) { removeTouching(Coin.class); World1 world1 = (World1)getWorld(); world1.addScore(1); } if (isTouching(GMushroom.class)) { removeTouching(GMushroom.class); World1 world1 = (World1)getWorld(); world1.addLife(1); } if (isTouching(RMushroom.class)) { removeTouching(RMushroom.class); World1 world1 = (World1)getWorld(); } if ( isTouching(Enemies.class) ) { World1 world1 = (World1)getWorld(); world1.addLife(-1); } } }