I am trying to make a simple platformer but I can not seem to get the gravity code right. Everything works, but the player falls halfway through the ground and walls, depending on how hard they collide with each other. This is what I want it to look like: and this is what sometimes happens: The code for the player class is here: And the code for the subclass of the player, the P1 is here:
Thank you so much for your advice!
import greenfoot.*; /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Actor { public int JPower = 10; public int VSpeed = 0; public int Drag = VSpeed - 2; public int keyPressed = 0; public int bounce = 0; public int eighthWidth = getImage().getWidth()/8; public int quarterWidth = getImage().getWidth()/4; public int halfWidth = getImage().getWidth()/2; public double nextLevel; public int shootdelay=0; public int mindelay=5; //set JPower public void JPower(int Power) { JPower = Power; } //checks if chacracter is on the ground public boolean touchingWallL() { Actor actor = getOneObjectAtOffset(0-quarterWidth, 0, Wall.class); return actor != null; } public boolean touchingWallR() { Actor actor = getOneObjectAtOffset(quarterWidth, 0, Wall.class); return actor != null; } public boolean onWall() { Actor actor = getOneObjectAtOffset(0, halfWidth, Wall.class); return actor != null; } public boolean belowWall() { Actor actor = getOneObjectAtOffset(0, 0-halfWidth, Wall.class); return actor != null; } /**Listed actions so far are:jump/fall, moveL, moveR, boxPhysics */ public void Action(String Action, String Char, String Key, String Key2, int AnimationFrames, int Speed) { if(Action == "jump/fall") { if(Greenfoot.isKeyDown(Key)) { keyPressed = 1; } if( JPower > 0 && keyPressed == 1 && !belowWall()) { setLocation(getX(), getY()-JPower); JPower--; }else if (onWall()){ JPower = 10; VSpeed = 0; keyPressed = 0; }else{ setLocation(getX(), getY()-VSpeed); VSpeed--; } if(belowWall()) { JPower = 0; } } if(Action == "moveL") { if(Greenfoot.isKeyDown(Key) && Greenfoot.isKeyDown(Key2) && !touchingWallL()) { setLocation(getX()-Speed*2, getY()); }else if(Greenfoot.isKeyDown(Key) && !touchingWallL()) { setLocation(getX()-Speed, getY()); } } if(Action == "moveR") { if(Greenfoot.isKeyDown(Key) && Greenfoot.isKeyDown(Key2) && !touchingWallR()) { setLocation(getX()+Speed*2, getY()); }else if(Greenfoot.isKeyDown(Key) && !touchingWallR()) { setLocation(getX()+Speed, getY()); } } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class P1 here. * * @author (your name) * @version (a version number or a date) */ public class P1 extends Player { private int level; public int shootdelay = 0; public int mindelay = 5; private Bullet bullet; /** * Act - do whatever the P1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Action("jump/fall", " ", "up", " ", 10, 0); Action("moveL", " ", "left", "shift", 10, 3); Action("moveR", " ", "right", "shift", 10, 3); Gun gun = new Gun(); if (Greenfoot.mouseClicked(null)) shoot(); Actor P1 = getOneIntersectingObject(Enemy.class); if (P1 != null) { Level5 level5 = new Level5(); Greenfoot.setWorld(level5); } Actor actor = getOneIntersectingObject(Enemy6.class); if (P1 != null) { Level6p4 level6p4 = new Level6p4(); Greenfoot.setWorld(level6p4); } } public void shoot() { MouseInfo mouse = Greenfoot.getMouseInfo(); int rot = getRotation()-10; Bullet b = new Bullet(); getWorld().addObject(b, getX(), getY()); b.turnTowards(mouse.getX(),mouse.getY()); } }