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

2016/6/14

Need help with AI firing in multiple (fixed) directions at the same time

kinghunter kinghunter

2016/6/14

#
Hey all, I'm fairly new to programming and am trying to make a game in which there is an alien (enemy) space ship that is stationary in the middle which fires lasers in multiple directions at once (up, down, side to side, and the 4 horizontals) I don't really know where to start and have an assignment due this Friday (to make our own game). what i have right now is barely anything, just the alien ship and an actor that i can move around the screen. i don't have much experience with bullets but any help will be greatly appreciated, thanks :)
danpost danpost

2016/6/14

#
Sounds like you need to have the alien create 8 bullets, each moving at a different 45 degree multiple up to 360 degrees. Obviously, there will be a timer field used to control the wait time between firings. What code have you attempted in the class of the alien to do so?
Super_Hippo Super_Hippo

2016/6/14

#
Create a bullet class and give it an image. The bullet itself doesn't do much. It moves straight forward (unless you have gravity for example) and damages or destroys objects (and itself) when touching them.
up, down, side to side, and the 4 horizontals
I am not sure about what you mean with this. The four horizontals would be up, down, left and right (=side to side?) with my understanding. Your game is two-dimensional, isn't it? To fire bullets in the four directions I just mentioned, the code could look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private int bulletTimer = 50;
 
public void act()
{
    bulletTimer--;
    if (bulletTimer==0)
    {
        bulletTimer = 50; //higher number=slower shooting
        for (int i=0; i<4; i++)
        {
            Bullet b = new Bullet(); //create the bullets
            getWorld().addObject(b, getX(), getY()); //add the bullets on the actor's position in the world
            b.setRotation(i*90); //each bullet needs a different direction
            b.move(5); //if you don't want to let the bullets start in the middle of the actor. If you want them to start in the middle, just remove this line. Maybe adjust the value
        }
    }
}
kinghunter kinghunter

2016/6/14

#
this is the code I was using and yes by side to side I meant both left and right, I input your code however when I try to run it the bullets just cluster up on the Alien do i have to call move(); in the bullet class?
1
2
3
4
5
6
7
8
9
10
11
private void fire()
   {
       Lazer lazer = new Lazer();
       getWorld().addObject(lazer, getX(), getY());
        if(fireDelay >= 25)
       {
           getWorld().addObject(new Lazer(), getX(), getY());
           fireDelay = 0;
       }
       fireDelay++;
   }
Wow thanks a lot, this was the code i was trying to use... i input yours into the Alien class but when i try to run it the bullets do not move to the right, do i have to call move(); in the Lazer class?
kinghunter kinghunter

2016/6/14

#
kinghunter wrote...
this is the code I was using and yes by side to side I meant both left and right, I input your code however when I try to run it the bullets just cluster up on the Alien do i have to call move(); in the bullet class?
1
2
3
4
5
6
7
8
9
10
11
private void fire()
   {
       Lazer lazer = new Lazer();
       getWorld().addObject(lazer, getX(), getY());
        if(fireDelay >= 25)
       {
           getWorld().addObject(new Lazer(), getX(), getY());
           fireDelay = 0;
       }
       fireDelay++;
   }
Super_Hippo Super_Hippo

2016/6/14

#
Yes, the bullets have to move using the move-method in the act-method.
kinghunter kinghunter

2016/6/14

#
okay thanks I tried this earlier and works just as I wanted it to, if I want I want the bullets to fire on the 45 I just change b.setRotation (i*45); I'm not at a computer right now so I just want to make sure...
Super_Hippo Super_Hippo

2016/6/14

#
Yes and you need to change i<4 to i<8.
You need to login to post a reply.