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

2017/11/22

Double Jump

DionWezz DionWezz

2017/11/22

#
Hey guys, i am trying to make my actor double jump. Now i succeed in letting it jump multiple times, but for some reason it cant jump if iam on a platform I think it has something to do with the following bit of code
1
2
3
4
5
6
7
if(onPlatform()){
            vSpeed = 0;
              
            GreenfootImage platformImage = under.getImage();
            int topOfPlatform = under.getY() - platformImage.getHeight()/2;
            setLocation(getX(), topOfPlatform - groundLevel);
        }
I have tried many thins, but without succes, can some help me? Here is the full code of the actor
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class player2 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class player1 extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 1;
    private int groundLevel = getImage().getHeight() / 2;
    private int height = 0;
    private int collision = 150;
    private int sideWidth = getImage().getWidth() / 2;
    private int jumps = 0;
    private int jumpCooldown = 0;
    private boolean jumping = false;
    private World myWorld;
    int worldHeight;
    int worldWidth;
     
    public void addedToWorld(World myWorld) {
        this.myWorld = myWorld;
        this.worldHeight = myWorld.getHeight();
        this.worldWidth = myWorld.getWidth();
    }
    public void act()
    {
        checkKeys();
        checkFall();
 
    }
    public boolean onPlatform(){
        Actor under = getOneObjectAtOffset(0, groundLevel + 5 , platform2.class);
        return under != null;
    }
    public void checkFall(){
        Actor under = getOneObjectAtOffset(0, groundLevel +5 , platform2.class);
        Actor platformAbove = getOneObjectAtOffset(0, - (groundLevel +5) , platform2.class);
        Actor platformToRight = getOneObjectAtOffset(sideWidth + 5, 0 , platform2.class);
        Actor platformToLeft = getOneObjectAtOffset(-(sideWidth + 5), 0 , platform2.class);
        Actor playerToLeft = getOneIntersectingObject(player2.class);
        if(onPlatform()){
            vSpeed = 0;
              
            GreenfootImage platformImage = under.getImage();
            int topOfPlatform = under.getY() - platformImage.getHeight()/2;
            setLocation(getX(), topOfPlatform - groundLevel);
        }
        
        
        else if(getY() >= worldHeight - groundLevel) {
            vSpeed = 0;
            setLocation(275, 550);
        }
        else {
            acceleration = 1;
            fall();
        }
         if (getX() >= 1299) {
            setLocation(sideWidth,getY());
        }
        if (getX() <= 0) {
            setLocation((1300 -(sideWidth +3)),getY());
        }
        if (platformAbove != null){
            if (vSpeed < 0) {
              vSpeed = 0;
              GreenfootImage platformImage = platformAbove.getImage();
              int bottomOfPlatform = platformAbove.getY() + platformImage.getHeight() / 2;
              setLocation(getX(), bottomOfPlatform + groundLevel);
            }
             
        }
        if (platformToRight !=null) {
          setLocation(getX() - 1, getY()) ;  
        }
         if (platformToLeft !=null) {
          setLocation(getX() + 1, getY()) ;  
        }
         
        if (playerToLeft !=null) {
            jumpToSideRight(); 
        }
    }
    private void jumpToSideRight() {
        vSpeed = -17;
        setLocation (getX()+ collision, getY()+ vSpeed);
        vSpeed = vSpeed + acceleration;
         
    }
    private void jumpToSideLeft() {
        vSpeed = -17;
        setLocation (getX()- collision, getY()+ vSpeed);
        vSpeed = vSpeed + acceleration;
         
    }
    private void checkKeys() {
        if (Greenfoot.isKeyDown("a")) {
            moveLeft();
            setImage("player1_left.png");
             
        }
        if (Greenfoot.isKeyDown("d")) {
            moveRight();
            setImage("player1_right.png");
        }
        if (Greenfoot.isKeyDown("w") && jumping == false) {
            jumps ++;
            jumping = true;
            jump();           
        }
        if (jumping == true && !Greenfoot.isKeyDown("up")) {
            jumping = false;
        }
    }
    public void jump() {
        vSpeed = -17;
    }
    public void moveRight() {
        setLocation (getX() + speed, getY());
    }
    public void moveLeft() {
        setLocation (getX() - speed, getY());
    }
    public void fall() {
        setLocation (getX(), getY()+ vSpeed);
        vSpeed = vSpeed + acceleration;
 
    }
}
danpost danpost

2017/11/22

#
You are not using the "up" key for jumping (see line 116).
DionWezz DionWezz

2017/11/22

#
danpost wrote...
You are not using the "up" key for jumping (see line 116).
i did fix this now, but am still only able to jump while i am in the air, i think it have something to do with it that if i stand on a platform it teleports me the whole time to the top of it. Sorry for bad englisch ;)
danpost danpost

2017/11/22

#
Try the following for line 46:
1
if(onPlatform() && jumping == false){
DionWezz DionWezz

2017/11/22

#
danpost wrote...
Try the following for line 46:
1
if(onPlatform() && jumping == false){
i tried that already, it works except for 1 thing, the player falls through all platforms after that
danpost danpost

2017/11/22

#
DionWezz wrote...
i tried that already, it works except for 1 thing, the player falls through all platforms after that
Oh -- I did not see the 'else' blocks starting at lines 55 and 59. The same condition will probably have to be applied to them also. Try replacing lines 46 through 62 with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (jumping == false){
    if(onPlatform()){
        vSpeed = 0;
        GreenfootImage platformImage = under.getImage();
        int topOfPlatform = under.getY() - platformImage.getHeight()/2;
        setLocation(getX(), topOfPlatform - groundLevel);
    }
    else if(getY() >= worldHeight - groundLevel) {
        vSpeed = 0;
        setLocation(275, 550);
    }
    else {
        acceleration = 1;
        fall();
    }
}
DionWezz DionWezz

2017/11/22

#
danpost wrote...
DionWezz wrote...
i tried that already, it works except for 1 thing, the player falls through all platforms after that
Oh -- I did not see the 'else' blocks starting at lines 55 and 59. The same condition will probably have to be applied to them also. Try replacing lines 46 through 62 with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (jumping == false){
    if(onPlatform()){
        vSpeed = 0;
        GreenfootImage platformImage = under.getImage();
        int topOfPlatform = under.getY() - platformImage.getHeight()/2;
        setLocation(getX(), topOfPlatform - groundLevel);
    }
    else if(getY() >= worldHeight - groundLevel) {
        vSpeed = 0;
        setLocation(275, 550);
    }
    else {
        acceleration = 1;
        fall();
    }
}
Hey danpost, it did not solve the problem entirely, but with some minor tweaks i made it! I wanna thank u for all your help, without you i would't be able to solve the problem!
You need to login to post a reply.