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

2018/2/12

Character slips off edge of platform

Kestroz Kestroz

2018/2/12

#
Hi everyone, recently I've gotten pretty into Greenfoot, for a school project, and I've got my basic movement done. However, when I jump on a platform it's fine however on the edge on platforms the character sprite slips off import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Character1 here. * * @author (your name) * @version (a version number or a date) */ public class Character1 extends Actor { private int vSpeed = -20; private int Speed = 4; private boolean jump; private int acceleration = 1; /** * Act - do whatever the Character1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveplayer(); checkFall(); checkAbove(); checkRightWall(); checkLeftWall(); } public void checkFall() { if(onGround()) { vSpeed = 0; } else { fall(); } } public void moveplayer() { if(Greenfoot.isKeyDown("left")) { setLocation(getX()-Speed, getY()); } if(Greenfoot.isKeyDown("right")) { setLocation(getX()+Speed, getY()); } if(Greenfoot.isKeyDown("space")) { jumpcheck(); } } public void jump() { vSpeed = -20; jump = true; fall(); } public void jumpcheck() { if(Greenfoot.isKeyDown("space") && jump == false){ jump(); } } public void fall() { setLocation(getX(), getY() + vSpeed); if (vSpeed <9) { vSpeed = vSpeed + acceleration; } jump = true; } public boolean onGround() { int characterheight = getImage().getHeight(); int findground = (int)(characterheight/2); Actor ground = getOneObjectAtOffset(0, findground, Platform.class); if (ground == null) { jump = true; return false; } else { repositioncharacter(ground); return true; } } public void repositioncharacter (Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight())/2; setLocation(getX(), newY); jump = false; } public boolean checkAbove() { int characterheight = getImage().getHeight(); int findground = (int)(characterheight/-2); Actor above = getOneObjectAtOffset(0, findground, Platform.class); if (above != null) { vSpeed = 1; bopHead(above); return true; } else { return false; } } public void bopHead (Actor above) { int aboveHeight = above.getImage().getHeight(); int newY = above.getY() +(aboveHeight + getImage().getHeight())/2; setLocation(getX(), newY); } public boolean checkRightWall() { int spriteWidth = getImage().getWidth(); int width = (int)(spriteWidth/2); Actor rightWall = getOneObjectAtOffset(width, 0, Platform.class); if (rightWall == null) { return false; } else { stopMovingRight(rightWall); return true; } } public void stopMovingRight(Actor rightWall) { int wallWidth = rightWall.getImage().getWidth(); int newX = rightWall.getX() - ((wallWidth + getImage().getWidth())/2); setLocation(newX, getY()); } public boolean checkLeftWall() { int spriteWidth = getImage().getWidth(); int width = (int)(spriteWidth/-2); Actor LeftWall = getOneObjectAtOffset(width, 0, Platform.class); if (LeftWall == null) { return false; } else { stopMovingLeft(LeftWall); return true; } } public void stopMovingLeft(Actor LeftWall) { int wallWidth = LeftWall.getImage().getWidth(); int newX = LeftWall.getX() + ((wallWidth + getImage().getWidth())/2); setLocation(newX, getY()); } }
danpost danpost

2018/2/12

#
In your 'onGround' method, you use 'getOneObjectAtOffset' checking at a zero offset along the x-axis. If you had two platforms side by side with that one x-coordinate as a buffer between them, your character would fall "between" (through) them. I should not suggest anything as I do things quite differently (you could check out my Jump and Run Demo w/Moving Platform scenario), but, maybe you can somehow improve on your collision checking (extra checking seems an option; or using a different collision method, which may require a total overhaul of the code).
Kestroz Kestroz

2018/2/13

#
Ok, thanks will make sure to check out your demo
You need to login to post a reply.