This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kordus
Kordus wrote ...

2017/12/22

Want to make my enemies shoot

Kordus Kordus

2017/12/22

#
I have looked through many discussions to try and make my enemies in my game shoot but without no luck. The code I have now doesnt make my enemies shoot but when they get close to the player, it fires automatically for some reason.
Kordus Kordus

2017/12/22

#
(Tis is my fighter class code) import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * An attack class ship designed to kill the player * * @author INSITE STUDIOS * @version 1.0 */ public class fighterclass extends Characters { GreenfootImage image = getImage(); private static final int ROTATION_SPEED = 3; private static final double ACCELERATION = .1; private static final double MAX_SPEED = 4; public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); return actor != null; } //shoot player if player is nearby public void shootPlayer() { if (canSee (FredFish.class)) { getWorld().addObject(new projectile(), getX(), getY()); } } { GreenfootImage image = getImage(); image.scale(60, 60); setImage(image); } public void act() { // vhfj vhfdkvbhd vbhdfj move(3); randomTurn(); turnAtEdge(); shootPlayer(); } /** * With a 10% probability, turn a bit right or left. */ public void randomTurn() { if ( Greenfoot.getRandomNumber(100) < 10 ) { turn( Greenfoot.getRandomNumber(40)-20 ); } } /** * If we reach the edge of the world, turn a little bit. */ public void turnAtEdge() { if (atWorldEdge()) { turn(5); } } }
danpost danpost

2017/12/22

#
If you were to put a comment on the 'canSee' method, it should read:
1
// returns true if the image of any actor of the given class intersects my center
The center being the location gotten from '(getX(), getY())'. So, the 'shootPlayer' method will need to use something else to determine when to fire. It will also need another condition to limit how often a projectile is fired when the player is in range. This can be a random chance thing or a regular or randomized timer.
You need to login to post a reply.