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

2013/1/1

Falling

BradH BradH

2013/1/1

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

2013/1/2

#
Actor under = getOneObjectAtOffset (0, getY() / 2, Ground.class);
Say your Spacemarine is at (100, 100). It is going to be checking for the Ground.class at (100, 150). That being, its X position, and its Y position plus half its Y position.
BradH BradH

2013/1/2

#
Why then doesnt the space marine stop falling once it hits the Ground (which is Ground.class)? Are you saying that I should change the coordinates where it is going to be checking for the Ground.class?
vonmeth vonmeth

2013/1/2

#
Yes, you probably want to be checking at the bottom of your actor's image, not in wildly different places depending on your actor's current Y position. Remember it is checking at an offset of your actor's current position. This below code will check at the bottom of your actor's image.
getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class);
Your actor's position is based off the center of the object.
BradH BradH

2013/1/2

#
If the ground is straight across the bottom of the world, the Y coordinate would be a contant so then there would just be just one Y value it would have to look for. The only thing that would change is the location on the x-axis when the spacemarine walks across the ground. So as an example would getX() , 396 work then. Or am I comepletely wrong, haha
vonmeth vonmeth

2013/1/2

#
You are checking at an offset of your current position, so it is relative to your actor's location, not to an absolute location. With your original code: If you are at x100, y200, it will be looking at x100, y300. If you are at x100, y300, it will be looking at x100, y450. If you are at x100, y500, it will be looking at x100, y750. If you are at x100, y600, it will by looking at x100, y900. If you are at x10, y10, it will be checking at x10, y15. With example code getX() , 396: If you are at x100, y200, it will be looking at x200, y596. If you are at x100, y300, it will be looking at x200, y696. If you are at x100, y500, it will be looking at x200, y896. If you are at x100, y600, it will by looking at x200, y996. If you are at x10, y10, it will be checking at x20, y406.
BradH BradH

2013/1/2

#
Ok, I got it thanks for your time.
vonmeth vonmeth

2013/1/2

#
Of course, glad to be of help. =)
vonmeth vonmeth

2013/1/2

#
Oh, and you might need to be calling the Checkfall() method somewhere.
BradH BradH

2013/1/3

#
Hey, sorry to bother you again but I have another question, tonight I have continued making changes to this class to allow it to jump once space is pressed. I have made a jump method.
public void jump()
{
vSpeed = -16;

fall();

}
     public void fall()
        {
          setLocation (getX() , getY() + vSpeed);
          vSpeed = vSpeed;  
          //speed incr as actor falls
          
        }
I went on youtube and found the tutorial on jumping and falling, and it has the vSpeed = 0, acceleration = 4 and in the fall method the vSpeed = vSpeed + acceleration, so the longer it falls the greater vSpeed gets. As you can see in my code above I did not do that, I made vSpeed = 4 and I do not have an acceleration. So my problem would be how can I get my vSpeed to decrease in the jump method so my actor does not just stay in the air, but falls back down. I know that I could just make an acceleration int and make my vSpeed = 0, but when I did that my actor would go up in the air and when it came back down it would fall through the ground so I just thought that doing it that way would not work. So to shorten this up if I made my vSpeed = 0 and an acceleration int = 4 is it possible to make the actor not fall through the ground, or is there a better way to do this with the code I have?
vonmeth vonmeth

2013/1/3

#
I know that I could just make an acceleration int and make my vSpeed = 0, but when I did that my actor would go up in the air and when it came back down it would fall through the ground so I just thought that doing it that way would not work.
Sounds like you are the right course here. Are you sure your checkFall method is being called in the act method, so vSpeed is being set to 0, and not to call the fall method in the act method (since the checkFall method will do that for you if you are not on the ground).
BradH BradH

2013/1/3

#
The weird thing is that the actor does not fall thorugh everytime, the check fall method is in the act method. The actor stays on the ground the problem is when it comes back down from jumping. It might just be a simple error in the class so i will keep looking.
You need to login to post a reply.