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

2019/4/13

Jumping, falling and platform problem

AniSusi AniSusi

2019/4/13

#
I coded jumping and falling sucessfully but as soon as I started doing the code for the platform, nothing worked anymore. The player doesn't fall or jump properly and now my Scenario stopped working at all. I don't know what I did wrong. I'd really appreciate some help! here's the code I have so far for the player: public class player extends Actor { private final int GRAVITY=1; private final int SPEED=5; private int speed1; public player() { speed1=0; } public void act() { fall(); if(Greenfoot.isKeyDown("w") && isOnSolidGround()) { jump(); } move(); } public void fall() { setLocation(getX(),getY()+speed1); if(isOnSolidGround()) { speed1=0; while(isOnSolidGround()) { setLocation(getX(),getY()-1); } setLocation(getX(),getY()+1); } else if(speed1<0 && bumpHead()) { speed1=0; while(bumpHead()) { setLocation(getX(),getY()+1); } } else { speed1+=GRAVITY; } } public void jump() { speed1=-15; } public void move() { int y=getY(); int x=getX(); if(Greenfoot.isKeyDown("a") && canMoveLeft()) { x-=SPEED; } if(Greenfoot.isKeyDown("d") && canMoveRight()) { x+=SPEED; } setLocation(x,y); } public boolean isOnSolidGround() { boolean isOnGround=false; if(getY()>getWorld().getHeight()-2) { isOnGround=true; } int imageWith=getImage().getWidth(); int imageHeight=getImage().getHeight(); if(getOneObjectAtOffset(imageWith/-2,imageHeight/2,platform.class)!=null || getOneObjectAtOffset(imageWith/ 2,imageHeight/2,platform.class)!=null) { isOnGround=true; } return isOnGround; } public boolean bumpHead() { boolean bumpedHead=false; int imageWith=getImage().getWidth(); int imageHeight=getImage().getHeight(); if(getOneObjectAtOffset(imageWith/-2,imageHeight/-2,platform.class)!=null || getOneObjectAtOffset(imageWith/ 2,imageHeight/-2,platform.class)!=null) { bumpedHead=true; } return bumpedHead; } public boolean canMoveLeft() { boolean canMoveLeft=true; int imageWith=getImage().getWidth(); int imageHeight=getImage().getHeight(); if(getOneObjectAtOffset(imageWith/-2-SPEED,imageHeight/-2 ,platform.class)!=null || getOneObjectAtOffset(imageWith/-2-SPEED,imageHeight/ 2-1,platform.class)!=null) { canMoveLeft=false; } return canMoveLeft; } public boolean canMoveRight() { boolean canMoveRight=true; int imageWith=getImage().getWidth(); int imageHeight=getImage().getHeight(); if(getOneObjectAtOffset(imageWith/ 2+SPEED,imageHeight/-2 ,platform.class)!=null || getOneObjectAtOffset(imageWith/ 2+SPEED,imageHeight/ 2-1,platform.class)!=null) { canMoveRight=false; } return canMoveRight; } }
AniSusi AniSusi

2019/4/14

#
I also tried this methode without success : public class player extends Actor { private int vSpeed=0; private int speed=1; private int jump=-15; public void act() { moveAround(); checkFall(); } private void fall() { setLocation(getX(),getY()+vSpeed); vSpeed=vSpeed+speed; } public void moveAround() { if(Greenfoot.isKeyDown("d")) { move(5); } if(Greenfoot.isKeyDown("a")) { move(-5); } if(Greenfoot.isKeyDown("w") && (onGround()==true)) { vSpeed=jump; fall(); } } boolean onGround() { Actor under=getOneObjectAtOffset(0,getImage().getHeight()/2,platform.class); return under !=null; } public void checkFall() { if(onGround()==false) { fall(); } if(onGround()==true) { vSpeed=0; } } }
danpost danpost

2019/4/14

#
AniSusi wrote...
I also tried this methode without success : << Code Omitted >>
Maybe my Jump and Run Demo w/Moving Platform scenario can help.
AniSusi AniSusi

2019/4/14

#
Thank you, but I'd rather improve the code I already have.
Super_Hippo Super_Hippo

2019/4/14

#
What exactly is happening and how is it different from what you want it to do? Also, please use code-tags. It makes reading the code so much easier.
AniSusi AniSusi

2019/4/14

#
code-tags? Alright I'll find out later what that is... All I want my character to do is to jump and fall on platforms, but the 2 codes I provided earlier don't work. Nothing happens in the scenario at all. And I don't understand @danpost 's code, so... idk what your question was but here's the answer.
Super_Hippo Super_Hippo

2019/4/14

#
Nothing happens in the scenario at all.
If nothing happens, then you should check the constructor of your active world subclass. For code-tags, click on "code" below the box you write your message in and paste your code there. You can also follow the link below that where it says "Need help with embedding? click here!".
AniSusi AniSusi

2019/4/17

#
thank you! I know whats wrong now, kind of. Jumping and falling works with the first code I had and the scenario runs fine, however if I put a platform in the world, everything freezes again, so the code for the platform is the problem.
AniSusi AniSusi

2019/4/17

#
actually nevermind, it works perfectly now for some reason... maybe the platform was just too big? well anyway, thank you all!
You need to login to post a reply.