Hi, I'm relatively new to Greenfoot and I'm trying to make a game where "kirby" can first swallow "enemies", and the user can either press "s" to copy its ability and "e" to spit it out to hit other enemies. To "swallow", the user presses space, and then after swallowed the user can press e or s. I use isKeyDown for checking first if the user pressed "space" and then after "swallow" method is called s or e. It works, but it looks like I have to press space and e or s at the same time, and it's really hard to control. Is there any other way of doing this?
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 | public void act(){ if (Greenfoot.isKeyDown( "space" )){ //if space pressed swallow swallow(); } } public void swallow(){ //swallow setImage( "swallowingkirby.png" ); List enemies=getObjectsInRange( 150 ,enemies. class ); if (enemies.size()> 0 ){ Actor enemy = (Actor)enemies.get( 0 ); setImage( "swallowed.png" ); swallowed= true ; if (Greenfoot.isKeyDown( "e" )){ if (swallowed){ spitout(); swallowed= false ;} } else if (Greenfoot.isKeyDown( "sz" )){ for ( int x= 0 ;x<enemies.size();x++){ if (enemy instanceof swordenemies){ //if swallow sword enemy, become sword kirby getWorld().addObject( new swordkirby(),getX(),getY()); getWorld().removeObject( this );} else if (enemy instanceof neutralenemies){ getWorld().removeObject(enemy); } else if (enemy instanceof throwenemy){ getWorld().addObject( new throwkirby(),getX(),getY()); getWorld().removeObject(enemy); getWorld().removeObject( this ); } } } } } |