Hello! I'm trying to build a stick figure platformer game, but whenever I try to program a double jump function, my player just loses his ability to jump entirely. Here's the code for the actor class:
I believe the problem is in the if onPlatform == true section
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Runner here. * * @author (your name) * @version (a version number or a date) */ public class Runner extends Actor { private int vSpeed = 0; private int acceleration = 1; private int jumpHeight = -13; private boolean hasStarted = false; private int jumpCount = 0; private GreenfootImage run1 = new GreenfootImage("rsz_frame_1.png"); //run1.scale(run1.getWidth() / 2, run1.getHeight() / 2); private GreenfootImage run2 = new GreenfootImage("rsz_frame_2.png"); private GreenfootImage run3 = new GreenfootImage("rsz_frame_3.png"); private GreenfootImage run4 = new GreenfootImage("rsz_frame_4.png"); private int animationCounter = 0; private int frame = 1; //ADDED CODE private boolean jumped = false; public Runner() { //getImage().scale(getImage().getWidth() / 2,getImage().getHeight() / 2); } /** * Act - do whatever the Runner wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { while (hasStarted == false) { if (Greenfoot.isKeyDown("w")) { hasStarted = true; } } moveAround(); checkFalling(); if (onPlatform() == true) { move(-3); if (Greenfoot.isKeyDown("space")) { vSpeed = jumpHeight; jumped = true; fall(); } } if (onPlatform() == false) { move(-3); if (Greenfoot.isKeyDown("space") && jumped == true) { vSpeed = jumpHeight + 7; fall(); jumped = false; } } if (getY() > 510) { Greenfoot.stop(); } animationCounter++; } public void moveAround() { if (Greenfoot.isKeyDown("d")){ move(7); if (animationCounter % 4 == 0) { animateRight(); } } if (Greenfoot.isKeyDown("a")){ move(-7); } } private void fall() { setLocation(getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } public boolean onPlatform() { Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class); if (under != null) { return true; } else { return false; } } public void checkFalling() { if (onPlatform() == false) { fall(); } } public void animateRight() { if (frame == 1) { setImage(run1); } else if (frame == 2) { setImage(run2); } else if (frame == 3) { setImage(run3); } else if (frame == 4) { setImage(run4); frame = 1; return; } frame++; } }