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

2018/3/22

Enemy turn towards you and shoot...

Timmy_11 Timmy_11

2018/3/22

#
I have an enemy class that aims at my tank and shoots at it on increments but if i drive past them they turn alot faster than at a distance how can I make them turn at a steady speed?? Enemy code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Animal
{
    int fire = 250; 
    public Enemy()
    {
        setImage("enemy.png");
    }
    /**
     * 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() 
    {
        if(!getWorld().getObjects(Tank.class).isEmpty())
        {
            aim();
            shoot();
        }
    }  
    public void shoot()
    {
        if (fire == 0)
        {
            getWorld().addObject(new EnemyBullet(getRotation()), getX(), getY());
            Greenfoot.playSound("GunShot.mp3");
            fire = 300;
        }
        if (fire > 0)
        {
            fire --;
        }
    }
    public void aim()
    {
        Tank tank = (Tank)getWorld().getObjects(Tank.class).get(0);
        turnTowards(tank.getX(), tank.getY()); 
    }
I know that the problem is in the turn towards in the aim method but i don't know how to slow it down.
Super_Hippo Super_Hippo

2018/3/23

#
You could see in which direction it has to turn (the number between 0 and 359). Then you compare this number with the current rotation and increase/decrease your rotation (= turn) by a number not greater than 3 (for example). Examples: (Current rotation / target rotation / change in rotation) - 100 / 200 / +3 - 100 / 350 / -3 - 100 / 101 / +1 If you do that, you also have to make sure that it only shoots when it aims in the correct rotation.
Timmy_11 Timmy_11

2018/3/23

#
Thanks I'll try it out
You need to login to post a reply.