I came across Stickman scenario with empty code so I am trying to write the code. I want the game to be the stickman jumping over enemies.
I tried with this code block but it didn't work, the stickman will jump and come down within fractions of second.
I also tried for loop to set the location but it seems even faster.
So please how can I make the stickman jump realistically.
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 | import greenfoot.*; / * This is a stick man. Make him run and jump. * * @author * @version */ public class Stickman extends Actor { private int velocity = 0 ; private int velCount = 0 ; / * Make the stickman act. */ public void act() { run(); checkKeyPress(); } / * Method to check key press. */ public void checkKeyPress(){ int posX = getX(); int posY = getY(); if (Greenfoot.isKeyDown( "up" )){ setLocation(posX, posY- 70 ); setImage( "jump.png" ); Greenfoot.delay( 4 ); setImage( "stand.png" ); setLocation(getX(), posY); } } / * Method to make stickman run. */ public void run(){ move(velocity+ 6 ); //Starts running with 7 int posY = getY(); if (getX()+ 2 >getWorld().getWidth()){ //getWorld().addObject(new Stickman(), 0, posY); //getWorld().removeObject(this); setLocation( 0 , posY); velCount++; incVelocity(); } } /** * Method to increase velocity by 1 after five rounds. */ public void incVelocity(){ if (velCount% 5 == 0 ){ setVelocity(getVelocity()+ 1 ); } } public int getVelocity(){ return velocity;} public void setVelocity( int velocity){ this .velocity = velocity;} } |