Hi everyone so recently i've made a game similar to doodle jump with a jumper/object jumping to the next platform on top and if you fall of you lose. So anyway my game all works but there is one problem, I've make it that if you press on the space bar the jumper/object jumps up, ok here is the problem: if you keep holding on the space bar then the jumper will keep moving up until I let go of the space bar, how do you make it that when you press or hold on the space bar the jumper/object only jumps ones rather then keeps going up until i let go of the space bar?
Here's what's going on in my code:
Here is my action performed class where the jumper jumps up or down
static boolean gameplay=false;
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (gameplay==true){
if (jumper==true){
DoodleHeight+20;
}
if (jumper==false){
DoodleHeight=DoodleHeight-10;
}
Here I have the keys that controls the jumps
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
jumper=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
jumper=false;
}
}
how would you fix the problem? I need help and i can't figure it out.
here is my code with the flag:
import java.awt.event.*;
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;
public class DoodleJumpp extends JPanel implements ActionListener,KeyListener{
static JFrame f;
static int width=1200, height=930;
static int DoodleW=500, DoodleH=500;
static int DoodlePlatformWidth=200, DoodlePlatformHeight=400;
static int DoodlePlatformWidth1=400, DoodlePlatformHeight1=530;
static int DoodlePlatformWidth2=900, DoodlePlatformHeight2=874;
static int DoodlePlatformWidth3=345, DoodlePlatformHeight3=643;
static int DoodlePlatformWidth4=711, DoodlePlatformHeight4=957;
static boolean rightjumper,leftjumper;
static boolean jumper;
static boolean test=true;
static boolean gameplay=true;
public void paintComponent (Graphics g){
g.setColor(Color.green);
g.fillRect(DoodlePlatformWidth, DoodlePlatformHeight,200,30);
g.fillRect( DoodlePlatformWidth1, DoodlePlatformHeight1,200,30);
g.fillRect( DoodlePlatformWidth2, DoodlePlatformHeight2,200,30);
g.fillRect( DoodlePlatformWidth3, DoodlePlatformHeight3,200,30);
g.fillRect( DoodlePlatformWidth4, DoodlePlatformHeight4,200,30);
g.setColor(Color.blue);
g.fillRect(DoodleW, DoodleH,50,50);
}
public static void main(String a[]){
DoodleJumpp D = new DoodleJumpp();
f = new JFrame();
D.init();
f.add(D);
f.setSize(width,height);
f.setVisible(true);
f.repaint();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer t=new Timer(10,D);
t.start();
}
public void init (){
this.addKeyListener(this);
setFocusable(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (gameplay==true){
if (jumper==true&&test==false){
DoodlePlatformHeight=DoodlePlatformHeight+20;
DoodlePlatformHeight1=DoodlePlatformHeight1+20;
DoodlePlatformHeight2=DoodlePlatformHeight2+20;
DoodlePlatformHeight3=DoodlePlatformHeight3+20;
DoodlePlatformHeight4=DoodlePlatformHeight4+20;
}
if (jumper==false&&test==false){
DoodlePlatformHeight=DoodlePlatformHeight-10;
DoodlePlatformHeight1=DoodlePlatformHeight1-10;
DoodlePlatformHeight2=DoodlePlatformHeight2-10;
DoodlePlatformHeight3=DoodlePlatformHeight3-10;
DoodlePlatformHeight4=DoodlePlatformHeight4-10;
}
if (leftjumper==true&&test==false){
DoodleW=(DoodleW-15);
}
if (rightjumper==true&&test==false){
DoodleW=(DoodleW+15);
}
if (DoodlePlatformHeight>height){
DoodlePlatformWidth=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight=0;
}
if (DoodlePlatformHeight1>height){
DoodlePlatformWidth1=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight1=0;
}
if (DoodlePlatformHeight2>height){
DoodlePlatformWidth2=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight2=0;
}
if (DoodlePlatformHeight3>height){
DoodlePlatformWidth3=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight3=0;
}
if (DoodlePlatformHeight4>height){
DoodlePlatformWidth4=(int) Math.floor(Math.random()*1201);
DoodlePlatformHeight4=0;
}
if (DoodleH==DoodlePlatformHeight-50 && DoodleW>=DoodlePlatformWidth-30 && DoodleW<=DoodlePlatformWidth+180){
test=true;
}
if (DoodleH==DoodlePlatformHeight1-50 && DoodleW>=DoodlePlatformWidth1-30 && DoodleW<=DoodlePlatformWidth1+180){
test=true;
}
if (DoodleH==DoodlePlatformHeight2-50 && DoodleW>=DoodlePlatformWidth2-30 && DoodleW<=DoodlePlatformWidth2+180){
test=true;
}
if (DoodleH==DoodlePlatformHeight3-50 && DoodleW>=DoodlePlatformWidth3-30 && DoodleW<=DoodlePlatformWidth3+180){
test=true;
}
if (DoodleH==DoodlePlatformHeight4-50 && DoodleW>=DoodlePlatformWidth4-30 && DoodleW<=DoodlePlatformWidth4+180){
test=true;
}
f.repaint();
}
}
static boolean flag=true;
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE && flag == true){
flag = false;
jumper=true;
test=false;
}
if (e.getKeyCode()==KeyEvent.VK_A){
leftjumper=true;
}
if (e.getKeyCode()==KeyEvent.VK_D){
rightjumper=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode()==KeyEvent.VK_SPACE){
flag = true;
jumper=false;
}
if (e.getKeyCode()==KeyEvent.VK_A){
leftjumper=false;
}
if (e.getKeyCode()==KeyEvent.VK_D){
rightjumper=false;
}
}
}
The problem is that your are bringing down the character only when the key is released !
Specifically with this logic:
And then with this logic when actionPerformed
You need something similar to gravity that after a few time lapse brings down the character to the ground level
One way to do this is to use a
TimerTask
that starts as soon as the character jumps and the task on every timer tick reduces the height of the character until is 0 or the same as before it jumped.