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

2017/1/9

Shoot Towards Hero

rkr rkr

2017/1/9

#
Im trying to make a code where my enemy who is always still is shooting towards the hero but i cant figure it out. Can some one help me out?
Nosson1459 Nosson1459

2017/1/10

#
Use
Greenfoot API wrote...
turnTowards(int x, int y) Turn this actor to face towards a certain location
in the bullet to face the hero. (That's the amount that I can help you with the amount of information you gave me.)
rkr rkr

2017/1/10

#
My enemy turns towards the hero but does not shoot diagonally towards the person. How do i fix this? Here is my code: ENEMY:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class WalkingHost here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class WalkingHost extends Enemy
{
    GreenfootSound shootSFX = new GreenfootSound("enemyshoot.mp3");
    SimpleTimer shootTimer = new SimpleTimer();
    private double health = 10;
    private int timer = 200;
    /**
     * Act - do whatever the WalkingHost wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        timer --;
        if (timer == 2){
            shoot();
            timer = 200;
        }
    
 
    public void shoot(){
        EnemyBullet a = new EnemyBullet();
        Hero h = ((Hero) getWorld().getObjects(Hero.class).get(0));
        turnTowards(h.getX(), h.getY());
        getWorld().addObject(a, getX(), getY());
    }
}
BULLET:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class EnemyBullet here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class EnemyBullet extends Bullet
{
    /**
     * Act - do whatever the EnemyBullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        //moves it its current direction at speed 10
        move(-10);
        remove();
    }
 
    //removes bullet if it comes into contact with hero or edge of world
    public void remove(){
        // Actor d = getOneIntersectingObject(Hero.class);
        Actor d = getOneIntersectingObject(Obstacle.class);
        // if bullet intersects with hero then send value to method adjust in actor hearts
        if (d!= null){
            //  Room r = (Room) getWorld();
            //  r.getObjects(Hearts.class).get(0).adjust(-1);
        }
        if (d!= null || isAtEdge()) { //if the bullet is touching an enemy or an obstacle
            getWorld().removeObject(this); //removes this specific instance of bullet
        }
    }
}
rkr rkr

2017/1/10

#
basically shoots in 90 degree angles
Nosson1459 Nosson1459

2017/1/10

#
rkr wrote...
My enemy turns towards the hero but does not shoot diagonally towards the person. How do i fix this?
If the EnemyBullet is set to move forward and then you do (in bullet) turnTowards(WalkingHost.getX(), WalkingHost.getY()); (<-- that code probably/might won't/not work) it will move straight towards the WalkingHost as long as you do the turn towards correctly. "My enemy turns towards the hero" that won't help since the bullet is a separate actor from he enemy. The same way you got the enemy to turn towards the hero you should do for the bullet and it will just move straight in whichever direction you set it.
rkr rkr

2017/1/10

#
Okay i figured out how to shoot diagonally but my bullet now shoots opposite from where my hero is. For example if my hero is near the top left my bullet goes to the bottom right ENEMY:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class WalkingHost here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class WalkingHost extends Enemy
{
    GreenfootSound shootSFX = new GreenfootSound("enemyshoot.mp3");
    SimpleTimer shootTimer = new SimpleTimer();
    private double health = 10;
    private int timer = 200;
    /**
     * Act - do whatever the WalkingHost wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        shoot();
    
 
    public void shoot(){
        int dist = 10000;
        Hero h = ((Hero) getWorld().getObjects(Hero.class).get(0));
        // EnemyBullet a = new EnemyBullet();
        HostBullet a = new HostBullet();
        double hypLength;
        double adjLength;
        Double d_radAngle; //starting point
        double radAngleUnbox;
        int i_radAngle;
        Integer degAngle;
        turnTowards(h.getX(), h.getY());
        if(!getObjectsInRange(dist, Hero.class).isEmpty()){
            for (Object obj: getObjectsInRange(dist, Hero.class)) {
                Actor Hero = (Actor) obj;
                hypLength = (double)Math.sqrt(Math.pow((Hero.getX() - getX()), 2) + Math.pow((Hero.getY() - getY()), 2));
                adjLength = (double)Hero.getX() - getX();
                d_radAngle = Math.acos(adjLength/hypLength) * 180/Math.PI; //starting point
                radAngleUnbox = d_radAngle.doubleValue(); //unbox
                i_radAngle = (int) radAngleUnbox;//cast
                degAngle = Integer.valueOf(i_radAngle);//box
                //shoots random at random intervals
                if (Greenfoot.getRandomNumber(76) == 0 && shootTimer.millisElapsed() >= 100){
                    shootSFX.play();
                    getWorld().addObject(a, getX(), getY());
                    shootTimer.mark();
                    if (Hero.getY() <= getY()){
                        a.setRotation(-degAngle);
                    }
                    else {
                        a.setRotation(degAngle);
                    }
                }
            }
        }
 
    }
}
BULLET:
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 HostBullet here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class HostBullet extends Bullet
{
    /**
     * Act - do whatever the HostBullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        //moves it its current direction at speed 10
        turn();
        move(-10);
        remove();
    }
 
    public void turn(){
        Hero h = ((Hero) getWorld().getObjects(Hero.class).get(0));
        turnTowards(h.getX(), h.getY());
    }
 
    //removes bullet if it comes into contact with hero or edge of world
    public void remove(){
        // Actor d = getOneIntersectingObject(Hero.class);
        Actor d = getOneIntersectingObject(Obstacle.class);
        // if bullet intersects with hero then send value to method adjust in actor hearts
        if (d!= null){
            //  Room r = (Room) getWorld();
            //  r.getObjects(Hearts.class).get(0).adjust(-1);
        }
        if (d!= null || isAtEdge()) { //if the bullet is touching an enemy or an obstacle
            getWorld().removeObject(this); //removes this specific instance of bullet
        }
    }
}
danpost danpost

2017/1/10

#
Change line 19 in the HostBullet class to move the other (in a positive or forward) direction.
rkr rkr

2017/1/10

#
O yea true thanks it works
rkr rkr

2017/1/10

#
What if i want to put invincibility after a bullet is shot for 5 seconds. Basically my bullet cannot deal damage during the 5 second of invincibility How do i do that?
Super_Hippo Super_Hippo

2017/1/10

#
You mean the bullet has to fly 5 seconds before it can deal damage? In this case, you could add a timer to track the "life time" of the bullet:
1
2
3
4
5
6
7
8
9
10
11
12
13
private int timer = 0;
 
public void act()
{
    turn();
    move(10);
    if (timer < 300)
    {
        timer++;
        return;
    }
    remove();
}
rkr rkr

2017/1/10

#
what i meant was after one bullet is shot from the enemy i want the enemy to not take damage from the hero for 5 seconds and after the 5 seconds the enemy ican take damage from the hero and can shoot a bullet
Super_Hippo Super_Hippo

2017/1/10

#
So if the enemy shoots right after the 5 seconds, it will be invincible for ever? Well, the code is quite the same then for the Enemy class:
1
2
3
4
5
6
7
8
9
10
11
12
13
private int timer = 0;
  
public void act()
{
    //moving... or not in your case I guess
    if (timer < 300)
    {
        timer++;
        return;
    }
    //take damage if hero hits it
    //check if shooting → when shooting, set timer back to 0
}
rkr rkr

2017/1/10

#
after the enemy bullet hits something then the enemy is invinvible for 5 sec. after the 5 sec the enemy is able to shoot again
Super_Hippo Super_Hippo

2017/1/11

#
Ah ok. In this case, you can save a reference to the shooting enemy in the bullet class and when it hits, the enemy can get invinsible:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//in Bullet class
private Enemy owner; //the Enemy which shot this bullet
 
public Bullet(Enemy e)
{
    owner = e;
}
 
public void act()
{
    move(/*something*/);
    if (isTouching(/*something*/))
    {
        //deal damage to target or remove it
        e.invinsible();
        getWorld().removeObject(this);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Enemy class
private int timer = 0;
   
public void act()
{
    if (timer < 300)
    {
        timer++;
        return;
    }
    //check if shooting
}
 
public void invinsible()
{
    timer = 0;
}
By the way, in your 'shoot' method, line 46 (the random and the timer condition) should be checked before all the calculations are done.
You need to login to post a reply.