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

2016/12/17

IndexOutOfBound error

1
2
3
danpost danpost

2016/12/17

#
rkr wrote...
so if the bullet collides with the enemy i should set the location of the enemy by using the bullet rotation how should i do that? is there any trigonometry involved
No trigonometry. Just move the enemy back a little in the same direction as the bullet is currently travelling:
// in action code in class of enemy
Actor bullet = getOneIntersectingObject(Bullet.class);
if (bullet != null)
{
    int rotation = getRotation(); // save current rotation of enemy
    setRotation(bullet.getRotation()); // set rotation to that of bullet
    move(3); // getting pushed back
    setRotation(rotation); // set rotation back to what it was
}
You may want to add some collision detection after the move to prevent going inside of solid objects.
rkr rkr

2016/12/17

#
my enemy isnt moving back when it gets hit
rkr rkr

2016/12/17

#
nevermind
rkr rkr

2016/12/17

#
thanks
rkr rkr

2016/12/17

#
my bullet removes it self before it hits the enemy
rkr rkr

2016/12/17

#
and if i dont add the collision my bullet hits the enemy and bullet continually pushes my enemy back until the edge
rkr rkr

2016/12/17

#
it only happens if move is 10 or more and if it is less i cant see the knock back
danpost danpost

2016/12/17

#
rkr wrote...
my bullet removes it self before it hits the enemy
Have the enemy remove the bullet. After line 8 above, add the following line:
getWorld().removeObject(bullet);
Remove any collision detection of enemy in the class of the bullet.
rkr rkr

2016/12/17

#
if i do that my enemy does not lose health when the bullet hits it
danpost danpost

2016/12/17

#
rkr wrote...
if i do that my enemy does not lose health when the bullet hits it
That can be done in the same place in the class of the enemy.
rkr rkr

2016/12/17

#
How do i do this ?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Grunt here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Grunt extends Enemy
{
    SimpleTimer timer = new SimpleTimer();
    double turntime = 50;
    private int health = 5;
    GreenfootSound shootSFX = new GreenfootSound("enemyshoot.mp3");
    /**
     * Act - do whatever the Grunt wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act(){
        if (!isCollidingWithHero()){
            moveRandomly();
        }
        randomShoot();
        checkDeath();
    }    

    //moves the grunt randomly
    public void moveRandomly(){
        //countinously moving in the direction its facing at speed 5
        move(5); 
        //greenfoot continously pulls a random number. If the random number is less than 10 turns the grunt a random angle between 45 and -45 
        if (Greenfoot.getRandomNumber(100) < 10){ 
            turn(Greenfoot.getRandomNumber(90 + 1) - 45);
        }       
        //if colliding with any obstacle, turns a complete 180 degrees
        if (timer.millisElapsed() > 90){
            if (isColliding()){ 
                turn(180);
                timer.mark();
            }
        }
        // code for knockback
        int timer = 5;
        Actor bullet = getOneIntersectingObject(HeroBullet.class);
        if (bullet!=null){
            int rotation = getRotation();
            setRotation(bullet.getRotation());
            move(50);
            setRotation(rotation);
            getWorld().removeObject(bullet);
        }
    }

    //checks if the grunt is colliding with any object of class Obstacle and returns true if it is.
    public boolean isColliding(){
        Actor d = getOneIntersectingObject(Enemy.class);
        Actor f = getOneIntersectingObject(Obstacle.class);
        if (d!= null || f != null){
            return true;
        }
        return false;
    }

    //check if grunt is colliding with hero
    public boolean isCollidingWithHero(){
        Actor d = getOneIntersectingObject(Hero.class);
        return d != null;
    }

    //shoots a bullet randomly
    public void randomShoot() {
        EnemyBullet e = new EnemyBullet();
        //greennfoot picks a random number between 0 and 499 each act. If this number is less than 5, then a bullet is spawned. 
        //Actual shooting happens in bullet class
        if (Greenfoot.getRandomNumber(500) < 10){
            shootSFX.play();
            getWorld().addObject(e, getX(), getY());
        }   
    }

    public void checkDeath() {
        Actor d = getOneIntersectingObject(HeroBullet.class);
        Hero h = ((Hero) getWorld().getObjects(Hero.class).get(0));
        boolean bombExist = !getWorld().getObjects(Bomb.class).isEmpty();
        if (bombExist == true){
            int healthLostBomb = getWorld().getObjects(Bomb.class).get(0).damage();
            healthBomb(healthLostBomb);
            if (getHealth() <= 0) {
                getWorld().removeObject(this);
            }
        }
        if (d!=null) {
            int healthLost = getWorld().getObjects(Hero.class).get(0).getDamage();
            health(healthLost);
            getWorld().removeObject(getOneIntersectingObject(HeroBullet.class));
            if (getHealth() == 0) {
                getWorld().removeObject(this);
            }
        }
    }

    public void health(int healthLostBullet ){
        health = health + healthLostBullet;
    }

    public void healthBomb(int healthLostBomb){
        health = health + healthLostBomb;
    }

    public int getHealth(){
        return health;
    }
}
danpost danpost

2016/12/17

#
rkr wrote...
How do i do this ?
Please show the code you are currently using to apply damage to enemy when a bullet hits one.
rkr rkr

2016/12/17

#
line 92
danpost danpost

2016/12/17

#
rkr wrote...
line 92
Okay -- I see. Remove lines 44 through 51 (I do not think line 43 belongs with this group of code). Then replace lines 85 through 99 with the following:
if (bombExist) {
    int healthLostBomb = ((Bomb)getWorld().getObjects(Bomb.class).get(0)).damage();
    healthBomb(healthLostBomb);
}
if (d!=null) {
    health(h.getDamage());
    int rotation = getRotation();
    setRotation(bullet.getRotation());
    move(50);
    setRotation(rotation);
    getWorld().removeObject(d);
}
if (getHealth() <= 0) {
    getWorld().removeObject(this);
}
rkr rkr

2016/12/17

#
THANK YOU IT WORKS! Thanks for all your help
There are more replies on the next page.
1
2
3