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

2016/11/27

Can anybody show me how to remove an enemy after being shot?

Tryxe Tryxe

2016/11/27

#
I'm doing this for my school project and its going good. My Hero shoots and the bullets disappear when it hits the edge but I can't seem to shoot the enemy and I need help. I got this code from another thread for basis, here's the code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bullet here. * * @author (your name) * @version (a version number or a date) */ public class Bullet extends Actor { private int direction, speed; public Bullet(int dir) { direction = dir; speed = 15; } /** * Act - do whatever the Bullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { isWorldEdge(); setRotation(direction); move(speed); } public void isWorldEdge() { if(atWorldEdge() == true ) { World world; world = getWorld(); world.removeObject(this); } } public boolean atWorldEdge() { if(getX() < 20 || getX() > getWorld().getWidth() - 20) return true; if(getY() < 20 || getY() > getWorld().getHeight() - 20) return true; else return false; } }
Super_Hippo Super_Hippo

2016/11/27

#
Maybe this is what you are searching for.
if (isTouching(Enemy.class))
{
    removeTouching(Enemy.class);
}
Tryxe Tryxe

2016/11/27

#
So I put this in public void act: isTouching(); Then I make this: public void isTouching() { if (isTouching (Enemy.class)) { removeTouching(Enemy.class); } } This is what I do right? I'm kinda new to this
Nosson1459 Nosson1459

2016/11/28

#
That should work (if you have an Enemy.class). Your code is a little difficult to read, to make it easier you should click on the link on the bottom of the Post a reply box for using code-tags (it looks like this: "Posting code? read this! "), it shows the code like an IDE and shows proper indentation.
Tryxe Tryxe

2016/11/28

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
 
{
private int direction, speed;
    public Bullet(int dir)
    {
        direction = dir;
        speed = 15;
    }
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        isWorldEdge();
        setRotation(direction);
        move(speed);
        isTouching();
    }
        public void isWorldEdge()
    { 
            if(atWorldEdge() == true )
            {
        World world;
        world = getWorld();
        world.removeObject(this);
    }
}
public boolean atWorldEdge()
    {
    if(getX() < 20 || getX() > getWorld().getWidth() - 20)
    return true;
    if(getY() < 20 || getY() > getWorld().getHeight() - 20)
    return true;
    else
    return false;
}
public void isTouching()
{
if(isTouching(Enemy.class))
{
removeTouching(Enemy.class);
}
}
}
Tryxe Tryxe

2016/11/28

#
This is what it would look like if I placed the "isTouching" code. Is this good?
Tryxe Tryxe

2016/11/28

#
I tried the code and its working but every time the enemy class dies and the bullet reaches the edge of the map an error message appears. Help!!!
danpost danpost

2016/11/28

#
Tryxe wrote...
I tried the code and its working but every time the enemy class dies and the bullet reaches the edge of the map an error message appears. Help!!!
Move line 24 from the beginning to the end of the act method. The other methods (or at least some of them) require that the actor be in the world; and if it is not, an exception will be thrown.
Tryxe Tryxe

2016/11/28

#
It's working perfectly thanks!!!
You need to login to post a reply.