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

2013/8/2

How can I make my actor jump?

Kaiwalya Kaiwalya

2013/8/2

#
I am making a game in which I want the actor to jump can anyone help please.
danpost danpost

2013/8/2

#
First, you will need an instance int field to hold the current vertical change in location (speed). This field should be used to continuously change the vertical location of the actor. Then, you will need to continuously add a small change to that field to create the effect of gravity. Next, you need to set the condition(s) for stopping the fall; whether it be checking for its location being at the bottom edge or its image intersecting another that represents an obstacle. Finally, you need to make sure that the checks are made at intervals no greater than the height of the actor and any obstacle it may encounter or it may be possible that the actor will pass right over the obstacle. Also, when bottom edge or obstacle is encountered, the vertical speed should be zeroed.
Kaiwalya Kaiwalya

2013/8/2

#
Can you give me the codes????? Well first part I understood and I try to modify and this is what I got. if(Greenfoot.isKeyDown("up"){ setLocation(getX(),getY()-5); } well because of this the actor move upwards all i want is to make it move downward.
danpost danpost

2013/8/2

#
Basically, in pseudo-code:
// with instance field
int vspeed = 0;
// move vertically (rise or fall)
vspeed++; // increase speed due to gravity
setLocation(getX(), getY()+vspeed); // move vertically
// check for obstacle or edge and jumping
if (intersecting(whatever) || atEdge())
{
    setLocation(wherever); // place actor above or below obstacle or edge as appropriate
    vspeed = 0; // stop vertical movement
    if (Greenfoot.isKeyDown("up") &&
         hasObstacleOrEdgeBelow())
             vspeed = -10; // adjust as needed (for jump start)
}
Lines 7 and 11 can be broken into two sets of checks each, but make sure that what needs to be done in each case is taken care of. The difference in the y-coordinates of the locations of the actor and the obstacle (or edge) can be used to determine if the obstacle or edge is above or below the actor; and that can be used to properly place of the actor.
Kaiwalya Kaiwalya

2013/8/3

#
Its not working.
danpost danpost

2013/8/3

#
Show what code you are trying to use so someone can try to help fix it
Kaiwalya Kaiwalya

2013/8/3

#
Well I dont want the check obstacle part .
danpost danpost

2013/8/3

#
Then, more basically, it would be:
// with instance field
int vspeed = 0;
// move vertically (rise or fall)
vspeed++; // increase speed due to gravity
setLocation(getX(), getY()+vspeed); // move vertically
// check for edge and jumping
if (atEdge())
{
    setLocation(wherever); // adjust location of actor
    vspeed = 0; // stop vertical movement
    if (Greenfoot.isKeyDown("up"))
         vspeed = -10; // adjust as needed (for jump start)
}
BTW, there are plenty of scenarios with open source that have gravity implemented within them. I have several, myself -- Scrolling SuperWorld, Toll-Tally Mad, and Flight Trajectories Demo.
Kaiwalya Kaiwalya

2013/8/4

#
Its saying that cannot find symbol-method atEdge() .
danpost danpost

2013/8/4

#
danpost wrote...
Basically, in pseudo-code
Pseudo-code is not precise code you would use. You have to either create the method that the line calls or replace the line with code that performs that action (or check) required.
You need to login to post a reply.