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

2012/5/18

How do you make a method to check if on ground?

kevinchaoo kevinchaoo

2012/5/18

#
Im having trouble with my jumping game my actor that jumps, everytime i hold the space key it just skyrockets up instead of only jumping once. my other problem is that my actor falls through the ground, and does not stay on the ground here is the code im using: im new so go easy on me import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * A small demo of a jumping movement. * * @author M K�lling * @version 1.0 */ public class Hobbes extends Actor { private int speed = 7; // running speed (sideways) private int vSpeed = 0; private int acceleration = 2; private int jumpStrength = 12; private boolean jumping = false; /** * Check keyboard input and act accordingly */ public void act() { checkKeys(); checkFall(); if (Greenfoot.isKeyDown("space")&& jumping==false ) { jump(); } } private void checkKeys() { if (Greenfoot.isKeyDown("left") ) { moveLeft(); } if (Greenfoot.isKeyDown("right") ) { moveRight(); } if (Greenfoot.isKeyDown("space") ) { jump(); } } public void jump() { jumping = true; vSpeed = -jumpStrength; fall(); } public void checkFall() { if(onGround()) { vSpeed = 0; jumping = false; } else { fall(); } } public boolean onGround() { Ground under = null; int counter = 1; int max = vSpeed; //int variance; while (counter <= max && under == null) { under = (Ground)getOneObjectAtOffset ( 0, getImage().getHeight() / 2 + counter, Ground.class); counter++; } // If there is a platform, correct Pengu's height so that he always lands right on the platform // This avoids a wierd floating effect that was present otherwise if (under != null) { int newY; newY = under.getY() - (under.getImage().getHeight()/2) - ((getImage().getHeight() / 2))-1; setLocation(getX(), newY); } return under != null; } public void fall() { setLocation ( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } public void moveRight() { setLocation ( getX() + speed, getY() ); } public void moveLeft() { setLocation ( getX() - speed, getY() ); }
danpost danpost

2012/5/18

#
Since you have a check for 'space' to jump in the 'act' method, you can remove the check for 'space' to jump in the 'checkKeys()' method.
kevinchaoo kevinchaoo

2012/5/18

#
Thanks, still having trouble checking if my actor is on the floor of my game any help with that?
kevinchaoo kevinchaoo

2012/5/18

#
still need help! !
kartikitrak kartikitrak

2012/5/18

#
Kevin, for some reason if you're character is falling too fast, Greenfoot is unable to detect the collision. I have that problem with my mario game right now. The only solution is to slow the fall speed. To fix the skyrocketing part just add this:
if (Greenfoot.isKeyDown("space") && onGround() ) 
{ 
jump();
 }
Basically this will let you ONLY jump when your character is on the ground, assuming that you've done the collision detection properly.
danpost danpost

2012/5/18

#
The best solution is to loop the move. Let us say you have 'speed = 10;' then:
for (int i = 0; i < speed; i++) {
    // code to move 1 pixel or cell
    // check for obstacles, and if found, move back 1 pixel or cell
}
It will process the loop ten times, but each time check for obstacles. The net result on that one act cycle will be either the actor moving 10 pixels or cells, or moving up to the object and stopping.
kartikitrak kartikitrak

2012/5/18

#
Hmm. I might try that on my game. Never thought of it. Also, danpost. Not sure if you've seen my discussion. But I'm having a bit of a problem. Could you check it out? http://www.greenfoot.org/topics/find/9137#post_9137
davmac davmac

2012/5/18

#
kevinchaoo: first, please use tags around your code. I have tried your code and don't see any problem with the actor falling through the ground. Perhaps the problem is that you are using something that isn't a "Ground" as the ground? It's hard to say without seeing the rest of your scenario. The 'flying' problem is covered in the jumping tutorial video. kartikitrak:
Kevin, for some reason if you're character is falling too fast, Greenfoot is unable to detect the collision.
That is also covered by the jumping tutorial video. kevinchao's code above already incorporates a solution to it - see the 'while' loop in the onGround() method. danpost:
The best solution is to loop the move
The existing while loop essentially does this.
davmac davmac

2012/5/18

#
You need to login to post a reply.