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

2018/12/13

Gravity doesn't work

1
2
CoolSharkCody CoolSharkCody

2018/12/13

#
Hello everyone, I'm having a problem with making a game in Greenfoot. Whenever I hit the jump button, my character shoots straight up to the edge of the world. Any suggestions that would help me fix this problem?
danpost danpost

2018/12/13

#
CoolSharkCody wrote...
Whenever I hit the jump button, my character shoots straight up to the edge of the world. Any suggestions that would help me fix this problem?
Not without seeing what codes you are using (show class code where you have the jumping).
CoolSharkCody CoolSharkCody

2018/12/13

#
Sorry, here's my code:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class FishMan here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class FishMan extends Actor
{
    private static final int jumpStrength = 16;
    private int VSpeed = 0;
    private static final int acceleration = 2;
    private static final int speed = 7;
    /**
     * Act - do whatever the FishMan wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        handleMovement();
        checkFall();
    }
    public void setVSpeed(int speed)
    {
        VSpeed = speed;
    }
    public void moveLeft()
    {
        setLocation(getX()-speed, getY());
    }
    public void moveRight()
    {
        setLocation(getX()+speed, getY());
    }
    public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2, Background1.class);
        return under != null;
    }
    public void fall()
    {
        setLocation(getX(), getY()+VSpeed);
        VSpeed = VSpeed - acceleration;
    }
    private boolean atBottom()
    {
        return getY() >= getWorld().getHeight() - 2;
    }
    private void handleMovement() {
        if (Greenfoot.isKeyDown("left")) {
            moveLeft();
}
if (Greenfoot.isKeyDown("right")) {
    moveRight();
}
if (Greenfoot.isKeyDown("up"))
{
    if (onGround())
    jump();
}
}
private void jump()
{
    setVSpeed(-jumpStrength);
    fall();
}
private void checkFall()
{
    if (onGround()) {
        setVSpeed(0);
    }
    else {
        fall();
    }
}
}
danpost danpost

2018/12/13

#
You are subtracting gravity instead of adding it (line 44).
CoolSharkCody CoolSharkCody

2018/12/13

#
I tried that, but it still doesn't work.
danpost danpost

2018/12/13

#
CoolSharkCody wrote...
I tried that, but it still doesn't work.
I tested with the change and it seemed to work fine for me.
CoolSharkCody CoolSharkCody

2018/12/13

#
That's weird. Show me your code please.
danpost danpost

2018/12/13

#
CoolSharkCody wrote...
That's weird. Show me your code please.
All I did was change the minus sign to a plus sign in line 44.
CoolSharkCody CoolSharkCody

2018/12/13

#
And I did the same thing, but for some reason it still won't work for me.
danpost danpost

2018/12/13

#
CoolSharkCody wrote...
And I did the same thing, but for some reason it still won't work for me.
Here is my test codes: World
1
2
3
4
5
6
7
8
9
10
11
import greenfoot.*;
 
public class MyWorld extends World
{
    public MyWorld()
    {
        super(600, 400, 1);
        addObject(new Background1(), 300, 380);
        addObject(new FishMan(), 300, 330);
    }
}
Ground
1
2
3
4
5
6
7
8
9
10
11
import greenfoot.*;
 
public class Background1 extends Actor
{
    public Background1()
    {
        GreenfootImage img = new GreenfootImage(600, 40);
        img.fill();
        setImage(img);
    }
}
Jumper
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
import greenfoot.*;
 
public class FishMan extends Actor
{
    private static final int jumpStrength = 16;
    private int VSpeed = 0;
    private static final int acceleration = 2;
    private static final int speed = 7;
     
    public void act()
    {
        handleMovement();
        checkFall();
    }
     
    public void setVSpeed(int speed)
    {
        VSpeed = speed;
    }
     
    public void moveLeft()
    {
        setLocation(getX()-speed, getY());
    }
     
    public void moveRight()
    {
        setLocation(getX()+speed, getY());
    }
     
    public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2, Background1.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation(getX(), getY()+VSpeed);
        VSpeed = VSpeed + acceleration;
    }
     
    private boolean atBottom()
    {
        return getY() >= getWorld().getHeight() - 2;
    }
     
    private void handleMovement() {
        if (Greenfoot.isKeyDown("left")) {
            moveLeft();
        }
        if (Greenfoot.isKeyDown("right")) {
            moveRight();
        }
        if (Greenfoot.isKeyDown("up"))
        {
            if (onGround())
                jump();
        }
    }
     
    private void jump()
    {
        setVSpeed(-jumpStrength);
        fall();
    }
     
    private void checkFall()
    {
        if (onGround()) {
            setVSpeed(0);
        }
        else {
            fall();
        }
    }
}
CoolSharkCody CoolSharkCody

2018/12/13

#
Here’s my code for the rest of my game if you need it. MyWorld:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*;
  
public class MyWorld extends World
      private void prepare()
      {
            FishMan fish = new FishMan();
            addObject(fish, 100, 309);
            Background1 background = new Background1();
            addObject(background, 300, 50);
            TitleScreen title = new TitleScreen();
            addObject(title, 300, 200);
      public MyWorld()
      {
            super(600, 400, 1);
            prepare();
      }
}
TitleScreen:
1
2
3
4
5
6
7
8
9
10
import greenfoot.*;
  
public class TitleScreen extends Actor
{
      GifImage gifImage = new GifImage(“randomimage.gif”);
      public void act()
      {
            setImage(gifImage.getCurrentImage());
}
}
Background1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*;
  
public class Background1 extends Actor
{
     public void act()
     {
           if (Greenfoot.isKeyDown(“space”)) {
               setLocation(300, 50);
     if (isTouching(TitleScreen.class)) {
         removeTouching(TitleScreen.class);
         if (isTouching(FishMan.class)) {
            getWorld().addObject(new FishMan, 100, 309);
}
}
}
}
}
danpost danpost

2018/12/13

#
Lines 7 and 8 in your Background1 class seems silly as you place the actor there to begin with and it does not seem to move. The TitleScreen object does not seem to move either, so the same goes for lines 9 and 10. Not sure why you add a second Fishman object into the world (lines 11 and 12). Please explain the purpose of the code you placed in the act method of the Background1 class.
CoolSharkCody CoolSharkCody

2018/12/13

#
The reason why the Background1 class has all the stuff you see in it (Mainly the isTouching commands) is originally I was going to put all of it into MyWorld, but I couldn’t really work it out because I kept getting error messages.
danpost danpost

2018/12/13

#
CoolSharkCody wrote...
The reason why the Background1 class has all the stuff you see in it (Mainly the isTouching commands) is originally I was going to put all of it into MyWorld, but I couldn’t really work it out because I kept getting error messages.
Well, yes -- you cannot perform collision detection methods from a World subclass (those methods have protected access, meaning you can only do them from the class of the actor whose collision you are checking). However, you really did not give the purpose of the code. What are you intending that code to accomplish?
CoolSharkCody CoolSharkCody

2018/12/13

#
Also I added another addObject(new FishMan) command as a backup because the actors were showing up at the wrong times, and the FishMan actor was always showing up whenever the TitleScreen actor appeared, even when I changed its code in MyWorld.
There are more replies on the next page.
1
2