Hi, so I'm making a pretty simple platform game with 3 different types of platforms and 1 moving platform. The problem I'm having is that when I'm jumping to other platforms, when I sometimes go off the platform the "gravity" does not work like my actor will just be walking on air. It seems to be happening with 2 platforms only and I'm not sure why.
I'll post my code but it might be a bit tricky to understand.
hopefully someone can help me!
thanks.
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Riggy here. * * @author (your name) * @version (a version number or a date) */ public class Riggy extends Actor { private int vSpeed = 0 ; private int acceleration = 1 ; /** * Riggy Constructor * */ public Riggy() { } /** * Act - do whatever the Riggy wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. checkKeys(); onLadder(); fall(); } public void checkKeys() { if (Greenfoot.isKeyDown( "left" )) { move(- 5 ); } if (Greenfoot.isKeyDown( "right" )) { move( 5 ); } if (Greenfoot.isKeyDown( "space" ) && onPlatform1() || Greenfoot.isKeyDown( "space" ) && onPlatform2() || Greenfoot.isKeyDown( "space" ) && onPlatform3() || Greenfoot.isKeyDown( "space" ) && onmovingPlatform()) { jump(); } } public void jump() { vSpeed = - 10 ; } public void fall() { setLocation(getX(), getY() + vSpeed); if (onPlatform1()) { vSpeed = 0 ; } else if (onPlatform2()) { vSpeed = 0 ; } else if (onPlatform3()) { vSpeed = 0 ; } else if (onmovingPlatform()) { vSpeed = 0 ; } else { vSpeed = vSpeed + acceleration; } } public boolean onPlatform1() { Actor under = getOneObjectAtOffset( 0 , getImage().getHeight()/ 2 - 8 , platform1. class ); return under != null ; } public boolean onPlatform2() { Actor under = getOneObjectAtOffset( 0 , getImage().getHeight()/ 2 - 8 , platform2. class ); return under != null ; } public boolean onPlatform3() { Actor under = getOneObjectAtOffset( 0 , getImage().getHeight()/ 2 - 8 , platform3. class ); return under != null ; } public boolean onmovingPlatform() { Actor under = getOneObjectAtOffset( 0 , getImage().getHeight()/ 2 - 8 , movingPlatform. class ); return under != null ; } public void onLadder() { ladder ld = (ladder)getOneIntersectingObject(ladder. class ); if (ld != null && Greenfoot.isKeyDown( "up" )) { vSpeed = 0 ; setLocation(getX(), getY() - 5 ); } } } |