My character previously jumped but now he just falls. I've played around with timers in order to get him to jump and stay in the air for a while but he instantly fell when leaving the ground. Pls help.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* This is the character in which the player controls.
* Within this class, most of the interactive features can be found.
* This class contains code which reacts to the "BadGuy" class and the "Bullet" Class.
*/
public class Main2 extends Character
{
GifImage Idle = new GifImage("Frame0.gif"); // Grabs GIF files and plays them when certain buttons are pressed.
GifImage WalkRight = new GifImage("Walk0.gif");
GifImage WalkLeft = new GifImage("WalkL0.gif");
GifImage Fire = new GifImage("FireFrameRight.gif");
GifImage FireLeft = new GifImage("FireFrameLeft.gif");
GifImage Jump = new GifImage("Jump.gif");
private GreenfootImage[] images=new GreenfootImage[27];
private int jeda=10,num=0,deltax=30,speed=10;
private int timer;
private int vSpeed = 10;
private int acceleration = 10;
private int jumpStrength = 18;
private int jtimer = 0;
//public GreenfootImage image = getImage();
/**
* After "Run" is clicked, the 'checkKeys' function constantly checks for what keys are being pressed
* and if the key pressed has a corresponding function to it.
*/
public void act() //Event Loop, this is a loop as it's constantly checking for something to happen.
{
for(int i=0;i<1;i++) {
setImage(Idle.getCurrentImage()); // Changes the image of the actor in order to create an animation.
}
timer++;
jtimer++;
fall();
checkFall();
checkKeys();
}
/**
* Whenever key 'A' is held, the actor will move left. When holding 'Shift' whilst also holding 'A' this will increase speed. This will also play an animation.
* Whenever key 'D' is held, the actor will move right. When holding 'Shift' whilst also holding 'D' this will increase speed. This will also play an animation.
* Whenever the space bar is pressed, the actor will jump and then fall after a certain period of time.
* Whenever key 'E' is pressed, a bullet will spawn on the right and an animation will play.
* Whenever key 'Q' is pressed, a bullet will spawn on the left and an animation will play.
*/
public void checkKeys()
{
if (Greenfoot.isKeyDown("e")) //Trigger Function
{
for(int i=0;i<1;i++) {
setImage(Fire.getCurrentImage());
}
shoot();
}
if (jtimer > 0)
{
if (Greenfoot.isKeyDown("space") ) //Trigger Function
{
for(int i=0;i<1;i++) {
setImage(Jump.getCurrentImage());
jump();
jtimer++;
}
if (jtimer > 0)
{
jtimer = 0;
}
}
}
if ( Greenfoot.isKeyDown("d") ) //This is a trigger function because it responds to user input.
{
for(int i=0;i<8;i++) {
setImage(WalkRight.getCurrentImage());
move(1);
}
}
if ( Greenfoot.isKeyDown("a")) //Trigger Function
{
for(int i=0;i<8;i++) {
setImage(WalkLeft.getCurrentImage());
move(-1);
}
}
if (Greenfoot.isKeyDown ("shift") && Greenfoot.isKeyDown("d")) //Trigger Function
{
move (3);
}
if (Greenfoot.isKeyDown ("shift") && Greenfoot.isKeyDown("a")) //Trigger Function
{
move (-3);
}
if (Greenfoot.isKeyDown ("q")) //Trigger Function
{
for(int i=0;i<1;i++) {
setImage(FireLeft.getCurrentImage());
}
shootleft();
}
}
/**
* This allows the player to run and jump at the same time. This will also play an animation when jumping.
* After the jump has been activated after a certain period of time, it will begin to fall.
* When the spacebar is pressed the actor's x and y coordinates will increase creating a jumping animation.
*/
public void jump()
{
vSpeed = jumpStrength;
fall();
setImage(images[0]);
}
/**
* When pressing "Q", this will add a bullet into the world which will travel in the left direction.
*/
public void shootleft()
{
if (timer > 30){
getWorld() .addObject(new LeftBullet(), getX(), getY());
timer = 0;
}
}
/**
* Event Loop, constantly checking for if the character is on the "Ground" class.
*/
public boolean onGround()
{
// Actor under = getOneObjectAtOffset( 0, getImage().getHeight() / 2-8, GroundMain.class);
Actor under = getOneIntersectingObject(GroundMain.class);
return under != null;
}
/**
* Event Loop, constantly checking for if the characters X and Y coordinates to check if the character is in the air.
* If true, the character will fall and stop falling when touching "Ground" class.
*/
public void checkFall()
{
if(onGround()) {
vSpeed = 0;
}
else {
fall();
}
}
public void shoot()
{
if (timer > 30){
getWorld() .addObject(new Bullet(), getX(), getY());
timer = 0;
}
}
/**
* When the player is in the air, the x and y coordinates will decrease and an animation will play.
*/
public void fall()
{
setLocation (getX(), getY() + vSpeed);
vSpeed = vSpeed + acceleration;
}
}

