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

2013/6/1

Shooting Help

Robert2.0 Robert2.0

2013/6/1

#
I can I make an enemy actor shoot randomly with least a two second wait and then the bullet follows another actor.
Gevater_Tod4711 Gevater_Tod4711

2013/6/1

#
Well an enemy that shoots randomly is not very difficult. To let your enemy wait two seconds after a shot you could use this superclass demo. To shoot in a random direction the easyest way would be to use a constructor in the bullet class which turns the bullet to a certain direction and then just call this constructor with a random value:
//in your bullet class:
public Bullet(int rotation) {
    setRotation(rotation);
}
//in your enemy class:
public void shoot() {
    getWorld().addObject(new Bullet(Greenfoot.getRandomNumber(360), getX(), getY());
}
this will make your enemy shoot randomly. The bullet that follows an actor is not that easy. How exactly should this bullet work? Should it first fly in the direction it was shot and than follow the nearest actor? Or how do you want the bullet to behave?
Robert2.0 Robert2.0

2013/6/1

#
Gevater_Tod4711 wrote...
Well an enemy that shoots randomly is not very difficult. To let your enemy wait two seconds after a shot you could use this superclass demo. To shoot in a random direction the easyest way would be to use a constructor in the bullet class which turns the bullet to a certain direction and then just call this constructor with a random value:
//in your bullet class:
public Bullet(int rotation) {
    setRotation(rotation);

I want the bullet to follow my "monster" class for only 5 seconds then the bullet disappears.
}
//in your enemy class:
public void shoot() {
    getWorld().addObject(new Bullet(Greenfoot.getRandomNumber(360), getX(), getY());
}
this will make your enemy shoot randomly. The bullet that follows an actor is not that easy. How exactly should this bullet work? Should it first fly in the direction it was shot and than follow the nearest actor? Or how do you want the bullet to behave?
Gevater_Tod4711 Gevater_Tod4711

2013/6/1

#
Have you got any questions?
Robert2.0 Robert2.0

2013/6/1

#
Gevater_Tod4711 wrote...
Have you got any questions?
Yeah, how can I make the bullet follow my "monster" class for 5 seconds before it disappears.
Gevater_Tod4711 Gevater_Tod4711

2013/6/2

#
To follow an actor is not the problem. But to find the actor which you should follow. To just follow an actor you need to know the reference to the actor and than you can turn to the actor:
turnTowards(followedActor.getX(), followedActor.getY());
But which actor do you want your bullet to follow? And when should it start following?
Robert2.0 Robert2.0

2013/6/2

#
Gevater_Tod4711 wrote...
To follow an actor is not the problem. But to find the actor which you should follow. To just follow an actor you need to know the reference to the actor and than you can turn to the actor:
turnTowards(followedActor.getX(), followedActor.getY());
But which actor do you want your bullet to follow? And when should it start following?
I got the following to work but how can I remove the bullet after 5 seconds.
chrl98 chrl98

2013/12/31

#
I've encountered an error with my random shooting as well. Here's the code: *Note*: This is not my own project. I am modifying it solely for school-related purposes.

public class EnemyTruck extends Truck implements LandUnit
{
    private static final int ROTATION_SPEED = 3;
    private static final double ACCELERATION = .1;
    private static final double MAX_SPEED = 4;
    private GreenfootImage background;
    private boolean Startgame = false;
    private boolean clicked = false;

    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

    public void act()
    {
        //moveAround();
        shootPlayer();
    }
    
    //shoot player if player is nearby
    public void shootPlayer()
    {
        if (canSee (Soldier.class))
        {
            getWorld().addObject(new Projectile(Greenfoot.getRandomNumber(250), getX(), getY()); 
        }
    }
}

Returning error: "method addObject in class greenfoot.World cannot be applied to given types" in line 27. What should I do to rectify the problem?
chrl98 chrl98

2013/12/31

#
Nevermind, I fixed it.
madeirense madeirense

2016/4/6

#
How did you fix this error?
Maimoce Maimoce

2016/4/6

#
how did you fix this error that you mentioned?
Super_Hippo Super_Hippo

2016/4/6

#
There is a closing brace ')' missing.
You need to login to post a reply.