this is my actor class, when it touches Ground.class it is to stop falling, hence the CheckFall method. In the onGround method it is to detect if the Actor is touching Ground.class but when I compile the code and run the game the character falls through the Ground.class. So i checked the onGround method in the window and it said that it was false when the Actor was clearly on it ( it being the Ground). Why is this?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class SpacemarineModel1 here. * * @author (your name) * @version (a version number or a date) */ public class Spacemarinemodel1 extends SMARINE {private int Walk = (0); private static final String [] IMAGES= { "Space Marine Body2.png" , "Space Marine Body3.png" , "Space Marine Body.png" }; private static final int RATE = 15; //speed actor falls down private int vSpeed = 4; //accl of object as it falls private int acceleration = 0; /** * Act - do whatever the SpacemarineModel1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { keypress(); fall(); onGround(); } public void keypress() { //move image if((Greenfoot.isKeyDown("d"))) move(3); if((Greenfoot.isKeyDown("a"))) move(-2); //make the image animate if((Greenfoot.isKeyDown("d"))) Walk = (Walk+1)%(RATE*IMAGES.length); if (Walk%RATE==0) { setImage(IMAGES[Walk/RATE]); } if((Greenfoot.isKeyDown("a"))) Walk = (Walk+1)%(RATE*IMAGES.length); if (Walk%RATE==0) { setImage(IMAGES[Walk/RATE]); } } public void fall() { setLocation (getX() , getY() + vSpeed); vSpeed = vSpeed + acceleration; //speed incr as actor falls } public boolean onGround() { Actor under = getOneObjectAtOffset (0, getY() / 2, Ground.class); return under != null; } public void Checkfall() {//if on floor speed == 0 if(onGround ()) { vSpeed = 0; } // not on floor fall else{ fall(); } } }