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

2015/3/28

Can anyone tell me how to maek my enemy shoot?

jocallaghan97 jocallaghan97

2015/3/28

#
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 enemy here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class enemy extends ships
{
    /**
     * 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()
    {
        AI();
        die();
    }   
 
    public void AI()
    {
        turnTowards(player.currentX, player.currentY);
        move(1);
    }
 
    public void die()
    {
        Actor bullet = getOneIntersectingObject(bullet.class);
        if(bullet!=null)
        {
            getWorld().removeObject(bullet);
            Greenfoot.playSound("explosion.wav");
            getWorld().addObject(new explosion(), getX(), getY());
            level1 level1 = (level1)getWorld();
            counter counter = level1.getCounter();
            counter.addScore();
            getWorld().removeObject(this);
        }
    }
}
This is my code for my enemy, i need some sort of way to make them shoot. can anyone help?
danpost danpost

2015/3/29

#
How would you like the bullets to be generated -- at set intervals of time or randomly? also, at what rate or average rate are they to be produced? do you have a separate class for the enemy bullets or will you be using the existing 'bullet' class? (using the same class may require you to have to move the bullets the enemies spawn off of them when created so they do not die from their own bullets when detected) Once you answer the first two questions, which comprise the trigger, you might be able to see how it is different from how the player shoots bullets (instead of a key press as the trigger, you will use a timer or a random chance).
jocallaghan97 jocallaghan97

2015/3/29

#
i would like them to be produced randomly and i do have a seperate class for the bullets.
danpost danpost

2015/3/29

#
You will need three values -- one for the minimum delay (measured in act cycles) between shots and another for the maximum delay. The third value will be for the timer itself and will be an instance field in the class (the other two values can also be made fields if desired as follows:
1
2
3
4
5
6
7
8
9
10
11
// instance fields
private int minShotDelay = 40;
private int maxShotDelay = 160;
private int shotTimer = minShotDelay;
// in 'act' method or a 'spawnBullet' method it calls
if (shotTimer == 0)
{
    getWorld().addObject(new Bullet2(getRotation()), getX(), getY());
    shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay);
}
else shotTimer--;
You can adjust the 'min' and 'max' fields as needed; but, 'max' cannot be less than 'min' which cannot be less than zero,
You need to login to post a reply.