Hey, I am having some issues with my code. I want to make an enemy shoot me but I should still be able to dodge the bullet. Until now the bullet will follow me and I have no possibility to escape the bullet. Please help me!
Enemy.class:


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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Anemy here. * * @author (your name) * @version (a version number or a date) */ public class Enemy extends UnterActor { public int LebensCounterEnemy; int spawnDieEnemyBullet = 250 ; public Enemy(){ LebensCounterEnemy= 0 ; } /** * Act - do whatever the Enemy wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { shoot(); zählLebenEnemy(); aim(); } private void shoot(){ if (spawnDieEnemyBullet== 0 ){ getWorld().addObject( new EnemyBullet(), 50 , 500 ); setRotation( this .getRotation()); spawnDieEnemyBullet= 300 ; } if (spawnDieEnemyBullet> 0 ){ spawnDieEnemyBullet=spawnDieEnemyBullet- 1 ; } } public void zählLebenEnemy(){ if (isTouching(Bullet. class )){ LebensCounterEnemy=LebensCounterEnemy+ 1 ; removeTouching(Bullet. class ); } if (LebensCounterEnemy== 1 ){ } if (LebensCounterEnemy== 2 ){ } if (LebensCounterEnemy== 3 ){ getWorld().addObject(( new WinScreen()), getWorld().getHeight()/ 2 , getWorld().getWidth()/ 2 ); Greenfoot.stop(); } } public void aim() { PlayerOne playerOne = (PlayerOne)getWorld().getObjects(PlayerOne. class ).get( 0 ); turnTowards(playerOne.getX(), playerOne.getY()); } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class AnemyBullet here. * * @author (your name) * @version (a version number or a date) */ public class EnemyBullet extends UnterActor { /** * Act - do whatever the AnemyBullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { aim(); move( 2 ); } public void aim() { PlayerOne playerOne = (PlayerOne)getWorld().getObjects(PlayerOne. class ).get( 0 ); turnTowards(playerOne.getX(), playerOne.getY()); } } |
1 2 3 | Actor bullet = new EnemyBullet(); getWorld().addObject(bullet, 50 , 500 ); bullet.setRotation(getRotation()); |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bullet here. * * @author (your name) * @version (a version number or a date) */ public class Bullet extends UnterActor { /** * Act - do whatever the Bullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { direction(); removeBullet(); } public void direction(){ turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY()); move( 20 ); } private void removeBullet(){ if (isTouching(Wall. class )){ getWorld().removeObject( 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 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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class PlayerOne here. * * @author (your name) * @version (a version number or a date) */ public class PlayerOne extends UnterActor { public int LebensCounter; public PlayerOne(){ LebensCounter= 0 ; } /** * Act - do whatever the PlayerOne wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { zählLeben(); stoppe(); move(); collisionEnemyBullet(); shoot(); } public void move(){ if (Greenfoot.isKeyDown( "W" )){ move( 2 ); setRotation( 270 ); } if (Greenfoot.isKeyDown( "D" )){ move( 2 ); setRotation( 0 ); } if (Greenfoot.isKeyDown( "A" )){ move( 2 ); setRotation( 180 ); } if (Greenfoot.isKeyDown( "S" )){ move( 2 ); setRotation( 90 ); } if (Greenfoot.mouseClicked( null )){ getWorld().addObject(( new Bullet()), getX(), getY()); } } public void stoppe(){ if (isTouching(Wall. class )){ move(- 4 ); } } private void zählLeben(){ if (isTouching(EnemyBullet. class )){ LebensCounter=LebensCounter+ 1 ; removeTouching(EnemyBullet. class ); } if (LebensCounter== 1 ){ getWorld().addObject(( new Leben23()), 83 , 60 ); } if (LebensCounter== 2 ){ getWorld().addObject(( new Leben13()), 83 , 60 ); } if (LebensCounter== 3 ){ getWorld().addObject(( new Leben03()), 83 , 60 ); getWorld().addObject(( new GameOverScreen()), getWorld().getHeight()/ 2 , getWorld().getWidth()/ 2 ); Greenfoot.stop(); } } private void shoot(){ } } |
1 2 3 4 | if (Greenfoot.mouseClicked( null )){ getWorld().addObject(( new Bullet()), getX(), getY()); setRotation(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY()); } |