im a beginner in coding and i wanted to make a simple game where you just need to go right and left and jump on plattforms and added some physics.
all it does is to make my cube fall when it is not touching the wall but instead it just doesnt fall at all which makes no sense because it doesnt touch any walls. i checked the boolean and it says it is touching the wall but actually it doesnt. here is my wall class: and here my myworld: I hope you can help me
import greenfoot.*; import java.util.Set; public class Spieler extends Actor { int red = Greenfoot.getRandomNumber(255); // random part for red int green = Greenfoot.getRandomNumber(255); // random part for green int blue = Greenfoot.getRandomNumber(255); int vSpeed = 0; int acceleration = 1; public void act() { checkFall(); spieler(); control(); } public void spieler() { GreenfootImage image; image = new GreenfootImage(16, 16); Color color; color = new Color(red, green, blue); image.setColor(color); image.fill(); setImage(image); } public void control() { if(Greenfoot.isKeyDown("s")) { setLocation(getX(), getY()+2); } if(Greenfoot.isKeyDown("d")) { setLocation(getX()+2, getY()); } if(Greenfoot.isKeyDown("a")) { setLocation(getX()-2, getY()); } } public void checkFall() { if(onGround()) { vSpeed = 0; } else { fall(); } } public boolean onGround() { Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 + 2, Wall.class); return under != null; } public void fall() { setLocation (getX(), getY() + vSpeed); vSpeed += acceleration; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Wall extends Actor { public void act() { image(); } public void image() { setImage("wall.png"); getImage().scale(30, 30); } }
import greenfoot.*; public class MyWorld extends World { public MyWorld() { super(1200, 800, 1); background(); player(); wall(); } public void background() { setBackground("cave.png"); getBackground().scale(1900, 800); } public void player() { addObject(new Spieler(), 50, 50); } public void wall() { addObject(new Wall(), 500, 200); addObject(new Wall(), 700, 400); addObject(new Wall(), 500, 600); addObject(new Wall(), 700, 790); } }