Hey, so im making a game for my programming class. The basic idea is to be a simple 2d old school street fighter style game, the problem I'm having happens to be with jumping. I would like to make it, so if the up key is pressed, the character will raise a predetermined amount (A constant I have) and then descend back down to the ground. I;m still rather new to greenfoot and java so please explain how you make something work please :]
Heres my current code that works for walking, but not for jumping.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.*; import java.awt.*; /** * Write a description of class char1 here. * * @author (your name) * @version (a version number or a date) */ public class char1 extends Characters { public static final double TE_SPD = 5.0 ; public static final double TE_JMP = 100.0 ; /** * Act - do whatever the char1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move1(); jump1(); } public void move1() { if (Greenfoot.isKeyDown( "right" )) { int x = ( int ) Math.round(getX() + TE_SPD); int y = ( int ) Math.round(getY()); setImage( "Char1Right.png" ); setLocation(x,y); } if (Greenfoot.isKeyDown( "left" )) { int x = ( int ) Math.round(getX() - TE_SPD); int y = ( int ) Math.round(getY()); setImage( "Char1Left.png" ); setLocation(x,y); } else { } } public void jump1() { if (Greenfoot.isKeyDown( "up" )) { jumptiming(); jumptiming(); jumptiming(); gravity(); gravity(); gravity(); } } public void jumptiming() { int x = ( int ) Math.round(getX()); int y = ( int ) Math.round(getY() + TE_JMP); setLocation(x,y); try { Thread.sleep( 500 ); } catch (InterruptedException e) { } } public void gravity() { int x = ( int ) Math.round(getX()); int y = ( int ) Math.round(getY() - TE_JMP); setLocation(x,y); try { Thread.sleep( 500 ); } catch (InterruptedException e) { } } } |