So I'm trying to make it so that my player can't move through blocks of dirt. It works when the dirt is to his right, but he passes through if it's to his left. Any ideas?
P.s I'm a total beginner with programming!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Actor { public int dirtInInventory=0; public int playerHeight = getImage().getHeight(); public int playerWidth = getImage().getWidth(); /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { fall(); checkDirt(); move(); dig(); } public void fall() { setLocation(getX(),getY()+4); } public void checkDirt() { Actor dirtDown = getOneIntersectingObject(Dirt.class); if(dirtDown!=null){ setLocation(getX(),getY()-4); } Actor dirtLeft = getOneObjectAtOffset(0,-25,Dirt.class); if(dirtLeft!=null){ move(3); } Actor dirtRight = getOneObjectAtOffset(0,25,Dirt.class); if(dirtRight!=null){ move(-3); } } public void move() { if(Greenfoot.isKeyDown("right")) { setLocation(getX()+3,getY()); } if(Greenfoot.isKeyDown("left")){ setLocation(getX()-3,getY()); } } public void dig() { if(Greenfoot.isKeyDown("d")){ Actor dirtRight2 = getOneObjectAtOffset(25,0,Dirt.class); if(dirtRight2!=null) { getWorld().removeObject(dirtRight2); dirtInInventory++; } } if(Greenfoot.isKeyDown("a")){ Actor dirtLeft2 = getOneObjectAtOffset(-25,0,Dirt.class); if(dirtLeft2!=null) { getWorld().removeObject(dirtLeft2); dirtInInventory++; } } }}