Hello everyone
I have a problem with my game code.
I have an actor (= player1) he can move left and right, when he stands still he does an animation too.
When my actor jumps from one platform to another, he gets stuck in the middle of the platform.
I have a method fallDown in my player class, I call it after my Actor jumps, so he falls down directly.
When i jump from the bottom floor and i land back on it, it give no problem.
The problem only appears when i jump from platform to platform.
Here is my code for my player1:
Also , don't pay attention to the arrays and the 'getIndex' methods, they are only used for the image animation.
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | public class Player1 extends Actor { private String[] walkLeft = { "Player1/left/left0.png" , "Player1/left/left1.png" , "Player1/left/left2.png" , "Player1/left/left3.png" }; private String[] walkRight = { "Player1/right/right0.png" , "Player1/right/right1.png" , "Player1/right/right2.png" , "Player1/right/right3.png" }; private String[] stopLeft = { "Player1/stopLeft/stopLeft0.png" , "Player1/stopLeft/stopLeft1.png" , "Player1/stopLeft/stopLeft2.png" , "Player1/stopLeft/stopLeft3.png" }; private String []stopRight = { "Player1/stopRight/stopRight0.png" , "Player1/stopRight/stopRight1.png" , "Player1/stopRight/stopRight2.png" , "Player1/stopRight/stopRight3.png" }; private int counterLeft,counterRight,counterStopLeft,counterStopRight; private int verticalSpeed, acceleration; private int jumpCooldown,jumpReady; private String directionFaced; private boolean moved; public Player1() { counterLeft = 0 ; counterRight = 0 ; counterStopLeft = 0 ; counterStopRight = 0 ; verticalSpeed = 0 ; acceleration = 1 ; jumpCooldown = 30 ; jumpReady = 0 ; moved = false ; setImage( "Player1/stopRight/stopRight0.png" ); } public void act() { checkKeys(); animation(); checkFallDown(); checkForWall(); jumpReady = jumpReady + 1 ; } private String getIndexWalkLeft( int index) { if (index >= 0 && index < walkLeft.length) { return walkLeft[index]; } else { return "" ; } } private String getIndexWalkRight( int index) { if (index >= 0 && index < walkRight.length) { return walkRight[index]; } else { return "" ; } } private String getIndexStopLeft( int index) { if (index >= 0 && index < stopLeft.length) { return stopLeft[index]; } else { return "" ; } } private String getIndexStopRight( int index) { if (index >= 0 && index < stopRight.length) { return stopRight[index]; } else { return "" ; } } public void animation() { if (!Greenfoot.isKeyDown( "left" )&& directionFaced == "left" ) { stopLeft(); } if (!Greenfoot.isKeyDown( "right" ) && directionFaced == "right" ) { stopRight(); } } public void checkKeys() { if (Greenfoot.isKeyDown( "left" )) { setLocation(getX()- 2 , getY()); walkLeft(); moved = true ; directionFaced = "left" ; } if (Greenfoot.isKeyDown( "right" )) { setLocation(getX()+ 2 , getY()); walkRight(); moved = true ; directionFaced = "right" ; } if (Greenfoot.isKeyDown( "space" ) && jumpReady >= jumpCooldown) { jump(); jumpReady = 0 ; } else { moved = false ; } } public void checkFallDown() { if (isOnPlatform()) { verticalSpeed = 0 ; } else { fallDown(); } } public void checkForWall() { } public void jump() { verticalSpeed = - 10 ; fallDown(); } public void fallDown() { setLocation(getX(), getY() + verticalSpeed); verticalSpeed = verticalSpeed + acceleration; } /** * Bij onderstaande methode ga ik controleren of de huidige actor op een platform staat. * 'under' gebruik ik omdat ik specifier naar de positie. * Op de plaats '0' staat het x-coordinaat ten opzichte van de locatie van het object. * 'under' is hier het object dat getourneerd wordt als er zich iets werkelijk, * onder de voeten van mijn actor bevindt. * Als er niks onder mijn actor kom te staan, dan zal deze methode een 'null' retourneren. * i.p.v. het object under. * */ public boolean isOnPlatform() { Actor under = getOneObjectAtOffset( 0 ,getImage().getHeight()/ 2 , Platforms. class ); return under != null ; } /** * animatie die uitgevoerd wordt als de speler naar links loopt. */ public void walkLeft() { if (counterLeft% 10 == 0 ) { setImage(getIndexWalkLeft(counterLeft/ 10 )); } counterLeft = (counterLeft + 1 )% 40 ; } /** * animatie als de speler naar rechts loopt. */ public void walkRight() { if (counterRight% 10 == 0 ) { setImage(getIndexWalkRight(counterRight/ 10 )); } counterRight = (counterRight + 1 )% 40 ; } public void stopLeft() { if (counterStopLeft% 22 == 0 ) { setImage(getIndexStopLeft(counterStopLeft/ 22 )); } counterStopLeft = (counterStopLeft + 1 )% 88 ; } public void stopRight() { if (counterStopRight% 22 == 0 ) { setImage(getIndexStopRight(counterStopRight/ 22 )); } counterStopRight = (counterStopRight + 1 )% 88 ; } } |