jframe - Java Game actionPerformed questions -
hi i've made game similar doodle jump jumper/object jumping next platform on top , if fall of lose. anyway game works there 1 problem, i've make if press on space bar jumper/object jumps up, ok here problem: if keep holding on space bar jumper keep moving until let go of space bar, how make when press or hold on space bar jumper/object jumps ones rather keeps going until let go of space bar?
here's what's going on in code:
here action performed class jumper jumps 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 have keys controls 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 fix problem? need , can't figure out.
here code 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 bringing down character when key released !
specifically logic:
@override public void keyreleased(keyevent e) { // todo auto-generated method stub if (e.getkeycode()==keyevent.vk_space){ jumper=false; } }
and logic when actionperformed
if (jumper==false){ doodleheight=doodleheight-10; }
you need similar gravity after few time lapse brings down character ground level
one way use timertask
starts character jumps , task on every timer tick reduces height of character until 0 or same before jumped.
Comments
Post a Comment