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

2014/2/17

making enemies come towards the player from the side..

jink jink

2014/2/17

#
i want to make a shooter game... and i want to know how to make the enemies come from the side so that the user can move the shooter up and down and shoot the enemies. i can make the enemies move to the player but i cannot make them originate from outside the world and towards the player .. PLZ HELP!!
erdelf erdelf

2014/2/17

#
well, how do you create the enemies`?
danpost danpost

2014/2/17

#
In order for any object to reside outside the edges of the world, you need to unbound the world. Refer to the World class constructors for this. Use a graphics editor like Paint.net (preferably one that supports transparencies) to rotate/flip the enemy image to face the direction required.
jink jink

2014/2/17

#
i got how to make the objects move towards the shooter.. i mean i got the code.. one more question.. how do i remove the enemy when the bullet touches the enemy. what i basically want is the bullet goes and hits "one" enemy and that enemy dissapears and a KABOOM appears. ive created the explosion image and class..
danpost danpost

2014/2/17

#
You could just have the KABOOM object get all its intersecting objects removed from the world. However, if your enemies are close to each other, then it might be better just to have the bullet 'eat' the enemy and add the KABOOM object before removing itself.
jink jink

2014/2/17

#
ya. but can you help me with the code?
jink jink

2014/2/17

#
public void kabom(){ bullet bull=new bullet(); if(this==this.getOneIntersectingObject(bullet.class)){ getWorld().removeObject(this); } }this is the code which i wrote this is in the monster class.. i am trying to remove the monster.
danpost danpost

2014/2/17

#
Use:
1
2
3
4
5
6
7
8
public void kabom() {
    Actor bull = getOneIntersectingObject(bullet.class); // get bullet
    if (bull != null) // is bullet there
    {
        getWorld().removeObject(bull); // remove bullet
        getWorld().removeObject(this); // remove monster
    }
}
adi110 adi110

2014/5/23

#
can you write the code for making the objects move towards the shooter?? i need the that code and i don't know how to do it
hugtvy hugtvy

2014/5/23

#
heres the the code to make the enemy move towards your "shooter", replace Surivovor with the name of your shooter and change the int dist to how close the shooter has to be to be followed eg: does the zombie always follow the shooter or is it only once it has come closs. hope this helps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void followHuman()
    {
        int dist = 1000;
        Actor closest = null;
         
        if(!getObjectsInRange(dist, Survivor.class).isEmpty())
        {
        for (Object obj: getObjectsInRange(dist, Survivor.class))
        {
            Actor guy = (Actor) obj;
            int guyDist = (int) Math.hypot(guy.getX() - getX(), guy.getY() - getY());
            if (closest == null || guyDist< dist)
            {
                    closest = guy;
                    dist = guyDist;
            }
        }
        turnTowards(closest.getX(),closest.getY());
        }  
    }
You need to login to post a reply.