When I use the getKey() method I can only seem to get it to work on one key. As in I can put if("space".equals(Greenfoot.getKey())) and if("q".equals(Greenfoot.getKey())) but it will only work for space not q. any help appreciated


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 | public class MainCharacter extends Actor { public int vSpeed = 0 ; public int walk = 0 ; private GreenfootImage imag; public int acceleration = 2 ; private GreenfootImage image1 = new GreenfootImage( "IceMan-Facing-Right.png" ); private GreenfootImage image2 = new GreenfootImage( "IceMan-Running-Right.png" ); private GreenfootImage image7 = new GreenfootImage( "IceMan-Facing-Left.png" ); private GreenfootImage image8 = new GreenfootImage( "IceMan-Running-Left.png" ); public int direc = 0 ; public int dir = 0 ; public int shot = 0 ; /** * Act - do whatever the MainCharacter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public MainCharacter() { setImage (image1); } public void act() { checkShoot(); checkJump(); fallCheck(); walk(); checkDir(); } public void checkDir() { if (shot == 1 ) { dir = 1 ; direc = 36 ; } if (shot == 2 ) { dir = 1 ; direc = 36 ; } if (shot == 7 ) { dir = 0 ; direc = - 36 ; } if (shot == 8 ) { dir = 0 ; direc = - 36 ; } } public void walk() { walk++; if (Greenfoot.isKeyDown( "right" )) { walkRight(); } if (Greenfoot.isKeyDown( "left" )) { walkLeft(); } } public void walkRight() { if (walk< 10 ) { setImage(image1); shot = 1 ; } if (walk> 10 ) { setImage(image2); shot = 2 ; } if (walk > 20 ) { walk = 0 ; } } public void walkLeft() { if (walk< 10 ) { setImage(image7); shot = 7 ; } if (walk> 10 ) { setImage(image8); shot = 8 ; } if (walk > 20 ) { walk = 0 ; } } public void fallCheck() { if (onPlatform()) { vSpeed = 0 ; } else { fall(); } } public boolean onPlatform() { Actor uPlat = getOneObjectAtOffset( 0 ,getImage().getHeight()/ 2 ,Platform. class ); return uPlat != null ; } public boolean touchPlatform() { Actor uPlat = getOneObjectAtOffset(getImage().getWidth()/ 2 , 0 ,Platform. class ); return uPlat != null ; } public void fall() { setLocation (getX(),getY() + vSpeed); vSpeed = vSpeed + acceleration; } public void jump() { vSpeed = - 16 ; fall(); } public void checkJump() { if ( "space" .equals(Greenfoot.getKey())) { jump(); } } public void checkShoot() { if ( "z" .equals(Greenfoot.getKey())) { shootIce(); } } public void shootIce() { getWorld().addObject( new Iceshot(dir),getX()+direc,getY()); } } |
1 | String key = Greenfoot.getKey(); |