May 20, 2015 at 7:06pm UTC
I got the problem with the platforms. Sometimes the platforms are solid. But sometimes the character just falls through it.
It's about sections with the comment above "// hit platform 1".
(But the problem is affecting all platforms. This is a piece of the code. Not the whole code.)
I would like to know why it happens and how it can be solved.
Thanks in advance.
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
void updatePlayer()
{
// player hits the jump coordinates
if ((player_x >= 200 && player_x <= 206) // jump spot on the ground floor
&&
(player_y == 10 || player_y == 9 || player_y == 8 || player_y == 7 || player_y == 6))
{
player_y += 175;
}
if ((player_x >= 400 && player_x <= 406) // jump spot on the 1st platform
&&
(player_y >= 100 && player_y <= 110))
{
player_y += 150;
}
if ((player_x >= 600 && player_x <= 606) // jump spot on the 2d platform
&&
(player_y == 208 ))
{
player_y += 300;
}
if ((player_x >= 400 && player_x <= 406) // jump spot on the 3d platform
&&
(player_y == 308 || player_y == 307 || player_y == 308 || player_y == 309 || player_y == 310))
{
player_y += 190;
}
if ((player_x >= 200 && player_x <= 206) // jump spot on the 4d platform
&&
(player_y == 408 || player_y == 409 || player_y == 410 || player_y == 411))
{
player_y += 200;
}
// hit the top (ceiling)
if (player_y >= 590)
{
playerSpeedUp = false ;
}
if (player_y < 590)
{
playerSpeedUp = true ;
}
// hit the left wall
if (player_x <= 10)
{
playerSpeedLeft = false ;
}
if (player_x > 10)
{
playerSpeedLeft = true ;
}
// hit the right wall
if (player_x >= 780)
{
playerSpeedRight = false ;
}
if (player_x < 780)
{
playerSpeedRight = true ;
}
// hit platform 1 - top
if ((player_y >= 108 && player_y <= 109) && (player_x >= 300 && player_x <= 450))
{
gravity = false ;
}
else if (player_y > 109 && (player_x < 300 || player_x > 450))
{
gravity = true ;
}
// hit platform 1 - bottom
if ((player_y <= 90 && player_y >= 85) && (player_x > 300 && player_x < 450))
{
playerSpeedUp = false ;
}
if (player_y > 85 && (player_x < 300 || player_x > 450))
{
playerSpeedUp = true ;
}
// hit platform 2 - top
if ((player_y >= 208 && player_y <= 209) && (player_x >= 500 && player_x <= 650))
{
gravity = false ;
}
else if (player_y > 209 && (player_x < 500 || player_x > 650))
{
gravity = true ;
}
// hit platform 2 - bottom
if ((player_y <= 190 && player_y >= 185) && (player_x > 500 && player_x < 650))
{
playerSpeedUp = false ;
}
if (player_y > 185 && (player_x < 500 || player_x > 650))
{
playerSpeedUp = true ;
}
// hit platform 3 - top
if ((player_y >= 300 && player_y <= 309) && (player_x >= 280 && player_x <= 450))
{
gravity = false ;
}
else if (player_y > 308 && (player_x < 280 || player_x > 450))
{
gravity = true ;
}
// hit platform 3 - bottom
if ((player_y <= 290 && player_y >= 285) && (player_x > 280 && player_x < 430))
{
playerSpeedUp = false ;
}
if (player_y > 285 && (player_x < 280 || player_x > 430))
{
playerSpeedUp = true ;
}
// hit platform 4 - top
if ((player_y >= 408 && player_y <= 410) && (player_x >= 80 && player_x <= 230))
{
gravity = false ;
}
else if (player_y > 410 && (player_x < 80 || player_x > 230))
{
gravity = true ;
}
// hit platform 4 - bottom
if ((player_y <= 390 && player_y >= 385) && (player_x > 80 && player_x < 230))
{
playerSpeedUp = false ;
}
if (player_y > 385 && (player_x < 80 || player_x > 230))
{
playerSpeedUp = true ;
}
// hit platoform 5 - top
if ((player_y >= 508 && player_y <= 509) && (player_x >= 350 && player_x <= 500))
{
gravity = false ;
}
else if (player_y >508 && (player_x < 350 || player_x > 500))
{
gravity = true ;
}
// hit platform 5 - bottom
if ((player_y <= 490 && player_y >= 485) && (player_x > 350 && player_x < 500))
{
playerSpeedUp = false ;
}
if (player_y > 485 && (player_x < 350 || player_x > 500))
{
playerSpeedUp = true ;
}
// hit the bottom (wall)
if (player_y < 11)
{
playerSpeedDown = false ;
gravity = false ;
}
if (player_y > 14)
{
playerSpeedDown = true ;
}
if (gravity == true )
{
player_y -= 3;
}
}
Last edited on May 20, 2015 at 7:11pm UTC
May 20, 2015 at 7:51pm UTC
It's hard to say without seeing all the code, but here are some things to consider:
Lines 50-67: If player_x is 100 then you set playerSpeedLeft and playerSpeedRight to true. Does that mean the player is going both left and right?
Lines 164-179. If player_x is 100 and player_y is 500 then this code will set playerSpeedUp and playerSpeedDown to true. Does that mean the player is moving both up and down?
I suspect that there are cases where you don't set gravity at all.
I think the key here is to structure the code so that you're guaranteed that it covers all the values of player_x and player_y.
May 21, 2015 at 6:48am UTC
I cannot paste the whole document in here. The document is too big.
About the playerSpeedRight and playerSpeedLeft etc. Those are bool variables which enables or disables the keys which the player can use. For example, in the game you can go right until you get to the end of the map and then the key yo use to right is disable so the character won't go out of the map and screen.
Thank you for the tip about covering all the variables. But it would be helpful if you could give an example how I should do it.
May 21, 2015 at 2:08pm UTC
I solved the problem by making the the gap bigger.
So instead of this: (player_y >= 108 && player_y <= 109).
I did this: (player_y >= 106 && player_y <= 109).
It's a small difference. Unfortunately, it also makes the character go into the platform a little bit. But that is hardly noticeable.
Thank you for your time and effort. Have a nice day.