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

2018/4/16

How to make ai fire at the player's spaceship?

Jol159 Jol159

2018/4/16

#
I'm just wondering how do you make AI fire at the player's spaceship. Any ideas? Here is the code for one of the AI spaceships:
/**
 * Write a description of class Enemy_Spaceship_2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy_Spaceship_2 extends Actor
{
    /**
     * Act - do whatever the Enemy_Spaceship_2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setRotation (180);
        flySpaceship();
    }
     
    public void flySpaceship()
    {
        move(2);
        if(isAtEdge())
        {
            setLocation(550,150);
        }
    }
danpost danpost

2018/4/16

#
Jol159 wrote...
I'm just wondering how do you make AI fire at the player's spaceship.
Add:
fire();
to the act method and create a public void fire method. Then, if you have issues, show what you have tried and ask for assistance.
Jol159 Jol159

2018/4/16

#
danpost wrote...
Jol159 wrote...
I'm just wondering how do you make AI fire at the player's spaceship.
Add:
fire();
to the act method and create a public void fire method. Then, if you have issues, show what you have tried and ask for assistance.
thanks! I'll give it a try!
Jol159 Jol159

2018/4/16

#
Tried it but got an error. Here is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy_Spaceship_2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy_Spaceship_2 extends Actor
{
    /**
     * Act - do whatever the Enemy_Spaceship_2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setRotation (180);
        flySpaceship();
        fire();
    }
     
    public void flySpaceship()
    {
        move(2);
        if(isAtEdge())
        {
            setLocation(550,150);
        }
    }
    
    public void fire()
    {
        Laserbeam_Green();
    }
}    

And here is the code for the Laserbeam:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Laserbeam_Green here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Laserbeam_Green extends Actor
{
    /**
     * Act - do whatever the Laserbeam_Green wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(3);
        checkTop();
    }    

    public void checkTop()
    {
        World world = getWorld();
        if(isAtEdge())
        {
            world.removeObject(this);
        }
    }
}    
Jol159 Jol159

2018/4/16

#
danpost wrote...
Jol159 wrote...
I'm just wondering how do you make AI fire at the player's spaceship.
Add:
fire();
to the act method and create a public void fire method. Then, if you have issues, show what you have tried and ask for assistance.
Tried it but got an error. Here is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Enemy_Spaceship_2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy_Spaceship_2 extends Actor
{
    /**
     * Act - do whatever the Enemy_Spaceship_2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setRotation (180);
        flySpaceship();
        fire();
    }
      
    public void flySpaceship()
    {
        move(2);
        if(isAtEdge())
        {
            setLocation(550,150);
        }
    }
     
    public void fire()
    {
        Laserbeam_Green();
    }
}    
And here is the code for the Laserbeam:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Laserbeam_Green here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Laserbeam_Green extends Actor
{
    /**
     * Act - do whatever the Laserbeam_Green wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(3);
        checkTop();
    }    
 
    public void checkTop()
    {
        World world = getWorld();
        if(isAtEdge())
        {
            world.removeObject(this);
        }
    }
}
danpost danpost

2018/4/16

#
There is no method called Laserbeam_Green() (see line 33 of the Enemy_Spaceship_2 class). If you want to create an object from the class, you need to use the new keyword. Just creating the object is still not enough, as you need to do something with the object that is created immediately (either retain a reference to it in a variable or add it into the world):
// adding directly into world
getWorld().addObject(new Laserbeam_Green(), getX(), getY());

// or retaining a reference first
Actor beam = new Laserbeam_Green();
getWorld().addObject(beam, getX(), getY());
Jol159 Jol159

2018/4/16

#
danpost wrote...
There is no method called Laserbeam_Green() (see line 33 of the Enemy_Spaceship_2 class). If you want to create an object from the class, you need to use the new keyword. Just creating the object is still not enough, as you need to do something with the object that is created immediately (either retain a reference to it in a variable or add it into the world):
// adding directly into world
getWorld().addObject(new Laserbeam_Green(), getX(), getY());

// or retaining a reference first
Actor beam = new Laserbeam_Green();
getWorld().addObject(beam, getX(), getY());
I presume I would need to put the code from the second option in the act method in the spaceship code. Am I correct?
danpost danpost

2018/4/16

#
Jol159 wrote...
I presume I would need to put the code from the second option in the act method in the spaceship code. Am I correct?
Either option -- they do exactly the same thing, but not in the act method -- in the fire method. However, it should probably only execute under specific conditions (within an if block); otherwise, they will spawn like mad and lag will be eminent.
Jol159 Jol159

2018/4/16

#
danpost wrote...
Jol159 wrote...
I presume I would need to put the code from the second option in the act method in the spaceship code. Am I correct?
Either option -- they do exactly the same thing, but not in the act method -- in the fire method. However, it should probably only execute under specific conditions (within an if block); otherwise, they will spawn like mad and lag will be eminent.
It works but no idea what if statement to use to stop the laserbeams from spawning like mad. Any ideas?
danpost danpost

2018/4/16

#
Jol159 wrote...
It works but no idea what if statement to use to stop the laserbeams from spawning like mad. Any ideas?
It depends on what behavior you are wanting -- periodic or random timing of shots would be one unknown; if periodic, how often would be another unknown; if random, the possible ranges in timing would be the second unknown.
You need to login to post a reply.