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

2024/8/8

Issues with platform detection

Cyatri Cyatri

2024/8/8

#
When I move a platform, if the player jumps on a moving platform it will phase through the platform, and sometimes when a platform is hit from the side the player phases through the platform. Otherwise, it seems completely fine. Any thoughts on a solution?
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Player here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int dir = 2;
    private boolean inTheAir = false;
    private int jumpHeight = 10;
    private int walkSpeed = 5;
    private double fallSpeed = 0.4;
    private double deltaX = 0;
    private double deltaY = 0;
    private int groundHeight = getImage().getHeight()/2;
    private int sideWidth = getImage().getWidth()/2;
    private World myWorld;
    int worldHeight;
    int worldWidth;
    int xPos;
    int yPos;
    private String[] fireboyRight = {"fireboyD.png", "fireboyD1.png", "fireboyD2.png"};
    int currentImage;
    int currentImageU;
    private GreenfootImage myImage = getImage();
 
    public void addedToWorld(World myWorld)
    {
        this.myWorld = myWorld;
        this.worldHeight = myWorld.getHeight();
        this.worldWidth = myWorld.getWidth();
    }
    public void act()
    {
        if(inTheAir)
        {
            fall();
        }
        else
        {
            getCommand();
        }
        checkCollision();
        move();
        animateOnMove();
    }
    private void run(String direction)
    {
        if(direction == "left")
        {
            deltaX = walkSpeed * -1;
        }
        else
        {
            deltaX = walkSpeed;
        }
    }
    private void stop()
    {
        deltaX = 0;
    }
    private void jump()
    {
        deltaY -= jumpHeight;
        inTheAir = true;
    }
    private void fall()
    {
        deltaY += fallSpeed;
    }
    private void move()
    {
        double newX = getX() + deltaX;
        double newY = getY() + deltaY;
         
        Actor platformBelow = getOneObjectAtOffset((int)deltaX,groundHeight+14 + (int) deltaY, Platform.class);
        Actor platformAbove = getOneObjectAtOffset((int)deltaX,groundHeight-5 + (int) deltaY-60, Platform.class);
        Actor platformToLeft = getOneObjectAtOffset(sideWidth+5 + (int) deltaX,(int) deltaY,Platform.class);
        Actor platformToRight = getOneObjectAtOffset(sideWidth-5 + (int)deltaX,(int) deltaY,Platform.class);
        if(platformBelow!=null)
        {
            deltaY=0;
            inTheAir = false;
            newY = platformBelow.getY()-60;
        }
        else if(getY()>=worldHeight - groundHeight)
        {
            deltaY=0;
            inTheAir = false;
             
        }
        else
        {
            inTheAir = true;
        }
        if(platformAbove!=null)
        {
            if(deltaY<0)
            {
                deltaY=0;
                GreenfootImage platformImage = platformAbove.getImage();
                int bottomOfPlatform = platformAbove.getY()+platformImage.getHeight()/2;
                newY = bottomOfPlatform + groundHeight;
                 
            }
        }
        if(getX()<=sideWidth)
        {
            deltaX = Math.abs(deltaX);
        }
        if(getX()>=worldWidth-sideWidth)
        {
            deltaX = Math.abs(deltaX) * -1;
        }
        if(platformToRight!=null)
        {
            deltaX = 0;
        }
        if(platformToLeft!=null)
        {
            deltaX = 0;
        }
        setLocation((int)newX,(int)newY);
    }
    private void getCommand()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            run("left");
        }
        else if(Greenfoot.isKeyDown("right"))
        {
            run("right");
        }
        else
        {
            stop();
        }
         
         
        if(Greenfoot.isKeyDown("up"))
        {
            jump();
        }
         
    }
    private void checkCollision()
    {
        if(isTouching(Fire.class))
        {
            removeTouching(Player2.class);
        }
    }
     
    private void animateOnMove()
    {
        if(getX()!=xPos)
        {
            currentImage++;
            if(currentImage>=fireboyRight.length * 4)
            {
                currentImage=0;
            }
             
             
            GreenfootImage newImage = new GreenfootImage(fireboyRight[currentImage/4]);
            if(getX()<xPos)
            {
                newImage.mirrorHorizontally();
            }
             
            setImage(newImage);
            myImage=getImage();
        }
        else
        {
            setImage("Fireboy.png");
        }
        xPos = getX();
        yPos = getY();
    }
}
danpost danpost

2024/8/12

#
You are trying to use double values to track the coordinates of the actor more precisely. However, a lot of accuracy is lost with lines 80 and 81. You are adding an int value to the doubles, deltaX and deltaY. You need to keep track of the location coordinates of the actor in double fields. Also, you are not adjusting the position of the actor when colliding with a platform while moving horizontally. Either one or both of these could lead to the unwanted behavior you describe.
You need to login to post a reply.