This site requires JavaScript, please enable it in your browser!
Greenfoot back
JoyBajwa
JoyBajwa wrote ...

2017/12/19

how do i make this character jump

JoyBajwa JoyBajwa

2017/12/19

#
this is the character and i want it to walk on a certain object and also make him jump. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Nose extends Characters { GifImage myStanding = new GifImage("Nuser.gif"); GifImage myRight = new GifImage("RightNuser.gif"); GifImage myLeft = new GifImage("LeftNuser.gif"); private int gravity; private int vSpeed = 0; private int acceleration = 2; private boolean jumping; private int jumpStrength = 20; public void act(){ setImage(myStanding.getCurrentImage()); forward(); backward(); gravity--; setLocation(getX(), getY() - gravity); //checkForJump(); //checkForJump4(); } public void forward(){ if(Greenfoot.isKeyDown("d")){ setLocation (getX ()+4 , getY() ); setImage(myRight.getCurrentImage()); } } public void backward(){ if(Greenfoot.isKeyDown("a")){ setLocation (getX ()-4 , getY() ); setImage(myLeft.getCurrentImage()); } } /** private void checkForJump() { if(Greenfoot.isKeyDown("w")){ gravity = 10; // this will make the character jump } }**/ /**private void checkForJump2() { Actor a = getOneIntersectingObject(flatblock.class); if (a != null) { gravity = 1; // this will make the character jump } } private void checkForJump4() { if(isTouching(flatblock.class)){ gravity = 1; // this will make the character jump } } }**/ }
danpost danpost

2017/12/19

#
It is so difficult to control all aspects of jumping when the codes are split up among so many different methods. Also, the state of the actor actually in the midst of jumping is not something that is useful from one act to the next. Gravity should generally always be a constant value (and remain active) and the vertical speed is what should change (it could be just a naming misrepresentation on your part -- however, you do show a 'vspeed' field as well which does not appear to be used anywhere in the class). The way I would control an actor that jumps and runs is portrayed in my Jump and Run Demo w/Moving Platform scenario. EDIT: The linked scenario is now updated for HTML 5 use.
JoyBajwa JoyBajwa

2017/12/20

#
I am new to programing and I want to make the character jump and i want to learn how you made your game so may please request to see the code of you game so I can learn from my mistakes. Ill also upload my game after its finished.
danpost danpost

2017/12/20

#
JoyBajwa wrote...
I am new to programing and I want to make the character jump and i want to learn how you made your game so may please request to see the code of you game so I can learn from my mistakes. Ill also upload my game after its finished.
You can click the buttons along the bottom of the window while running the scenario to view the codes used.
You need to login to post a reply.