Im trying to make a code where my enemy who is always still is shooting towards the hero but i cant figure it out.
Can some one help me out?


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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class WalkingHost here. * * @author (your name) * @version (a version number or a date) */ public class WalkingHost extends Enemy { GreenfootSound shootSFX = new GreenfootSound( "enemyshoot.mp3" ); SimpleTimer shootTimer = new SimpleTimer(); private double health = 10 ; private int timer = 200 ; /** * Act - do whatever the WalkingHost wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { timer --; if (timer == 2 ){ shoot(); timer = 200 ; } } public void shoot(){ EnemyBullet a = new EnemyBullet(); Hero h = ((Hero) getWorld().getObjects(Hero. class ).get( 0 )); turnTowards(h.getX(), h.getY()); getWorld().addObject(a, getX(), 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 30 31 32 33 34 35 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class EnemyBullet here. * * @author (your name) * @version (a version number or a date) */ public class EnemyBullet extends Bullet { /** * Act - do whatever the EnemyBullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { //moves it its current direction at speed 10 move(- 10 ); remove(); } //removes bullet if it comes into contact with hero or edge of world public void remove(){ // Actor d = getOneIntersectingObject(Hero.class); Actor d = getOneIntersectingObject(Obstacle. class ); // if bullet intersects with hero then send value to method adjust in actor hearts if (d!= null ){ // Room r = (Room) getWorld(); // r.getObjects(Hearts.class).get(0).adjust(-1); } if (d!= null || isAtEdge()) { //if the bullet is touching an enemy or an obstacle getWorld().removeObject( this ); //removes this specific instance of bullet } } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class WalkingHost here. * * @author (your name) * @version (a version number or a date) */ public class WalkingHost extends Enemy { GreenfootSound shootSFX = new GreenfootSound( "enemyshoot.mp3" ); SimpleTimer shootTimer = new SimpleTimer(); private double health = 10 ; private int timer = 200 ; /** * Act - do whatever the WalkingHost wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { shoot(); } public void shoot(){ int dist = 10000 ; Hero h = ((Hero) getWorld().getObjects(Hero. class ).get( 0 )); // EnemyBullet a = new EnemyBullet(); HostBullet a = new HostBullet(); double hypLength; double adjLength; Double d_radAngle; //starting point double radAngleUnbox; int i_radAngle; Integer degAngle; turnTowards(h.getX(), h.getY()); if (!getObjectsInRange(dist, Hero. class ).isEmpty()){ for (Object obj: getObjectsInRange(dist, Hero. class )) { Actor Hero = (Actor) obj; hypLength = ( double )Math.sqrt(Math.pow((Hero.getX() - getX()), 2 ) + Math.pow((Hero.getY() - getY()), 2 )); adjLength = ( double )Hero.getX() - getX(); d_radAngle = Math.acos(adjLength/hypLength) * 180 /Math.PI; //starting point radAngleUnbox = d_radAngle.doubleValue(); //unbox i_radAngle = ( int ) radAngleUnbox; //cast degAngle = Integer.valueOf(i_radAngle); //box //shoots random at random intervals if (Greenfoot.getRandomNumber( 76 ) == 0 && shootTimer.millisElapsed() >= 100 ){ shootSFX.play(); getWorld().addObject(a, getX(), getY()); shootTimer.mark(); if (Hero.getY() <= getY()){ a.setRotation(-degAngle); } else { a.setRotation(degAngle); } } } } } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class HostBullet here. * * @author (your name) * @version (a version number or a date) */ public class HostBullet extends Bullet { /** * Act - do whatever the HostBullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { //moves it its current direction at speed 10 turn(); move(- 10 ); remove(); } public void turn(){ Hero h = ((Hero) getWorld().getObjects(Hero. class ).get( 0 )); turnTowards(h.getX(), h.getY()); } //removes bullet if it comes into contact with hero or edge of world public void remove(){ // Actor d = getOneIntersectingObject(Hero.class); Actor d = getOneIntersectingObject(Obstacle. class ); // if bullet intersects with hero then send value to method adjust in actor hearts if (d!= null ){ // Room r = (Room) getWorld(); // r.getObjects(Hearts.class).get(0).adjust(-1); } if (d!= null || isAtEdge()) { //if the bullet is touching an enemy or an obstacle getWorld().removeObject( this ); //removes this specific instance of bullet } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | private int timer = 0 ; public void act() { turn(); move( 10 ); if (timer < 300 ) { timer++; return ; } remove(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | private int timer = 0 ; public void act() { //moving... or not in your case I guess if (timer < 300 ) { timer++; return ; } //take damage if hero hits it //check if shooting → when shooting, set timer back to 0 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | //in Bullet class private Enemy owner; //the Enemy which shot this bullet public Bullet(Enemy e) { owner = e; } public void act() { move( /*something*/ ); if (isTouching( /*something*/ )) { //deal damage to target or remove it e.invinsible(); getWorld().removeObject( this ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //Enemy class private int timer = 0 ; public void act() { if (timer < 300 ) { timer++; return ; } //check if shooting } public void invinsible() { timer = 0 ; } |