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

2014/4/14

Jumping on the ground

Arthur114 Arthur114

2014/4/14

#
How to make my actor so he can jump not only on the ground.class(platforms) but also on the bottom of the background thank you.
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Vilkas here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Vilkas extends Actor
{
       private GreenfootImage image1 = new GreenfootImage("wombat.png"); 
    private GreenfootImage image2 = new GreenfootImage ("wombat-left.png"); 
    private int vSpeed = 0;
    private int acceleration = 2;
    private boolean jumping;
    private int jumpStrength = 20;
     
    public void act()
    {
       checkFall();
        animate();
      CheckKeys();
      platformAbove();
      checkRightWalls();
      checkLeftWalls();
    }   
    public void CheckKeys()
    {
         if(Greenfoot.isKeyDown("a")){ 
            move(-5); 
        
           
        if(Greenfoot.isKeyDown("d")){ 
            move(5); 
        }  
        if(Greenfoot.isKeyDown("space") && jumping == false){ 
            jump(); 
        }  
    }
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
    }
     public void animate() 
    
        if((Greenfoot.isKeyDown("d"))){ 
        setImage (image1); 
    
        if((Greenfoot.isKeyDown("a"))){ 
            setImage (image2); 
        
}
public void fall()  
    setLocation(getX(),getY()+ vSpeed); 
    if(vSpeed <=9)
    {
        vSpeed = vSpeed + acceleration;
    }
    jumping = true;
public boolean onGround()
{
    int spriteHeight = getImage().getHeight();
    int yDistance = (int) (spriteHeight / 2 + 50 );
    Actor ground = getOneObjectAtOffset (0,getImage().getHeight() / 2, Ground.class);
    if(ground == null)
    {
        jumping = true;
        return false;
    }
    else
    {
        moveToGround(ground);
        return true;
    }
}
public void checkFall()
{
    if(onGround()) {
        vSpeed=0;
    }
    else {
        fall();
    }
}
public void moveToGround(Actor ground)
{
    int groundHeight = ground.getImage().getHeight();
    int newY = ground.getY() - (groundHeight + getImage().getHeight()) / 2;
     
    setLocation(getX(),newY);
    jumping = false;
}
public boolean platformAbove() 
    
        int spriteHeight = getImage().getHeight(); 
        int yDistance = (int)(spriteHeight/-2); 
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Ground.class); 
        if(ceiling != null
        
            vSpeed = 1
            bopHead(ceiling); 
            return true
        
        else 
        
            return false
        
    
    public boolean checkRightWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int) (spriteWidth/2);
         
        Actor rightWall = getOneObjectAtOffset(xDistance,0,Ground.class);
        if(rightWall == null)
        {
            return false;
        }
        else {
            stopByRightWall(rightWall);
            return true;
         
        }
    }
    public void stopByRightWall (Actor rightWall)
    {
        int wallWidth = rightWall.getImage().getWidth();
        int newX = rightWall.getX() - (wallWidth + getImage().getWidth()) / 2;
        setLocation(newX - 5, getY());
    }
    public void bopHead(Actor ceiling) 
    
        int ceilingHeight = ceiling.getImage().getHeight(); 
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2
        setLocation(getX(), newY); 
    
     public boolean checkLeftWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int) (spriteWidth/2);
         
        Actor leftWall = getOneObjectAtOffset(-xDistance,0,Ground.class);
        if(leftWall == null)
        {
            return false;
        }
        else {
            stopByleftWall(leftWall);
            return true;
         
        }
    }
    public void stopByleftWall (Actor leftWall)
    {
        int wallWidth = leftWall.getImage().getWidth();
        int newX = leftWall.getX() + (wallWidth + getImage().getWidth()) / 2;
        setLocation(newX + 5, getY());
    }
}
danpost danpost

2014/4/14

#
I believe that adding acceleration to the speed and falling should be done each act cycle regardless of the 'onGround' state (gravity does not go away just because you have found the ground -- it is just counter-balanced by the force the ground exerts in the opposite direction). Therefore, I feel that the 'boolean jumping' field should be a local variable (if used at all), not an instance field; and its value should be determined each act after falling is executed. The sign of the 'speed' field can determine where to 'stop' the actor when contacting a platform ('bopHead' and 'moveToGround'). To jump, you just need to ask 'Is there a platform at my feet or am I standing on the bottom edge of the world?'. This can be determined without having to use a boolean variable. The following should be a guideline for the vertical movement of an actor:
1
2
3
4
5
6
7
8
// add acceleration to speed and fall
// check for ground objects and, if found, adjust position (if speed is less than zero, than actor is bopping head else the actor is landing on ground -- set speed to zero after adjusting position)
// ensure actor is not going under the world (adjust position for bottom edge intrusion)
int halfHeight = getImage().getHeight()/2;
int maxY = getWorld().getHeight()-halfHeight;
if (getY() > maxY) setLocation(getX(), maxY);
// check for jumping (if on ground or at bottom edge of world)
if ((getOneObjectAtOffset(0, halfHeight+1, Ground.class) != null || getY() == maxY) && Greenfoot.isKeyDown("space")) // jump
Arthur114 Arthur114

2014/4/14

#
I'm newbie here could you explain me where to change/delete code so i can get what i need ? :/ Thanks.
Arthur114 Arthur114

2014/4/14

#
danpost wrote...
I believe that adding acceleration to the speed and falling should be done each act cycle regardless of the 'onGround' state (gravity does not go away just because you have found the ground -- it is just counter-balanced by the force the ground exerts in the opposite direction). Therefore, I feel that the 'boolean jumping' field should be a local variable (if used at all), not an instance field; and its value should be determined each act after falling is executed. The sign of the 'speed' field can determine where to 'stop' the actor when contacting a platform ('bopHead' and 'moveToGround'). To jump, you just need to ask 'Is there a platform at my feet or am I standing on the bottom edge of the world?'. This can be determined without having to use a boolean variable. The following should be a guideline for the vertical movement of an actor:
1
2
3
4
5
6
7
8
// add acceleration to speed and fall
// check for ground objects and, if found, adjust position (if speed is less than zero, than actor is bopping head else the actor is landing on ground -- set speed to zero after adjusting position)
// ensure actor is not going under the world (adjust position for bottom edge intrusion)
int halfHeight = getImage().getHeight()/2;
int maxY = getWorld().getHeight()-halfHeight;
if (getY() > maxY) setLocation(getX(), maxY);
// check for jumping (if on ground or at bottom edge of world)
if ((getOneObjectAtOffset(0, halfHeight+1, Ground.class) != null || getY() == maxY) && Greenfoot.isKeyDown("space")) // jump
Atm it's really hard to understand :(
danpost danpost

2014/4/14

#
Arthur114 wrote...
I'm newbie here could you explain me where to change/delete code so i can get what i need ? :/ Thanks.
The code is so far off from my line of thinking that it would be very difficult to just say change this and adjust that. To get close to what I was thinking, you would have to re-write most of the code within the class.
danpost danpost

2014/4/15

#
For an example, I uploaded a demo for your benefit. You can view the code while running the scenario.
You need to login to post a reply.