Hi, does anyone know how to make a side-scrolling platform game? Here is my code so you can help me in context. I have 2 worlds, CharSelect and Game, I also have a projectile, a ground class with two subclasses, and 1 hero and globals class. Here is the code fo CharSelect Here is the code for Game
And here is the code for Hero
public class CharSelect extends World { /** * Constructor for objects of class CharSelect. * */ public CharSelect() { super(600, 400, 1); addObject(Globals.h, 300, 255); prepare(); } public void act() { if (Greenfoot.isKeyDown("1")) { Globals.h.changeImage(1); } else if (Greenfoot.isKeyDown("2")) { Globals.h.changeImage(2); } else if (Greenfoot.isKeyDown("3")) { Globals.h.changeImage(3); } else if (Greenfoot.isKeyDown("4")) { Globals.h.changeImage(4); } else if (Greenfoot.isKeyDown("space")) { Greenfoot.setWorld(new Game()); } } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { } }
public class Game extends World { private int speed = 7; private int vSpeed = 0; private int acceleration = 1 ; private int jumpStrenght = -6; /** * Constructor for objects of class Game. * */ public Game() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 700, 1,false); addObject(Globals.h, 50, 400); addObject(new Long(), 190, 654); addObject(new Hump(), 618, 575); } }
public class Hero extends Actor Thanks in advance! { GreenfootImage i1 = new GreenfootImage("wizard_1.png"); GreenfootImage i2 = new GreenfootImage("princess.png"); GreenfootImage i3 = new GreenfootImage("goblin_2.png"); GreenfootImage i4 = new GreenfootImage("barbarian_1.png"); int vSpeed = 0; int acceleration = 1; int speed = 7; int jumpStrenght = -6; /** * Act - do whatever the Hero wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act () { if (getWorld() instanceof CharSelect) return; checkKeys(); checkFall(); landOnTop(); fireProjectile(); } public void fireProjectile() { if(Greenfoot.isKeyDown("down")) { getWorld(). addObject(new Projectile(), getX(), getY()); Projectile projectile = new Projectile(); } } private void checkKeys() { if(Greenfoot.isKeyDown("left")) { moveLeft(); } if(Greenfoot.isKeyDown("right")) { moveRight(); } if(Greenfoot.isKeyDown("up")) { jump(); } } public void checkFall() { if (onGround()) { vSpeed = 0; } else{ fall(); } } public void jump() { vSpeed= jumpStrenght; fall(); } public boolean onGround() { Actor under = getOneObjectAtOffset( 0,getImage().getHeight()/4,Ground.class); return under !=null; } public void landOnTop() { if (isTouching(Ground.class)) { setLocation(getX(), getY() - 1); } } public void moveRight() { setLocation (getX() + speed,getY() ); } public void moveLeft() { setLocation (getX() - speed,getY()); } public void fall() { setLocation (getX(),getY()+vSpeed); vSpeed = vSpeed + acceleration; } public Hero() { setImage(i1); } public void changeImage (int x) { if (x == 1) { setImage(i1); } if (x == 2) { setImage(i2); } if (x == 3) { setImage(i3); } if (x == 4) { setImage(i4); } } }