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

2020/4/23

Enemies not removing from world

Ryaj987 Ryaj987

2020/4/23

#
Hi, I cannot get the enemies in my world to remove. The image flashes so I know it's registering the hit. What would cause this? Is it trying to remove the animation and not the actor? I have the scenario posted with the source code viewable.
macaroon72 macaroon72

2020/4/23

#
I did not view your code yet, but is is possible that you did not use... getWorld().removeObject(" "); if you are in your actor's code, you would need to use this to remove any objects. I can not help you much further than this, sorry, I am a very beginner level coder.
Ryaj987 Ryaj987

2020/4/23

#
I'm using the getWorld().removeObject(enemies); and it still isn't working. Thank you!
danpost danpost

2020/4/23

#
Ryaj987 wrote...
I'm using the getWorld().removeObject(enemies); and it still isn't working. !
Try using:
removeTouching(Enemy.class);
Ryaj987 Ryaj987

2020/4/23

#
Still had the same issue
Super_Hippo Super_Hippo

2020/4/23

#
Show your code.
Ryaj987 Ryaj987

2020/4/23

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class HitManager extends Actor { private int timer = 0; /** * Act - do whatever the HitManager wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { killEnemies(); } public void remove() { Actor walls = getOneIntersectingObject(Platform.class); if(getX() <=1 || getX() >= getWorld().getWidth() -1) { getWorld().removeObject(this); } else if(walls != null) { getWorld().removeObject(this); } } public boolean hit(Class clss) { Actor actor = getOneObjectAtOffset(0,0, Enemies.class); return actor != null; } public void kill() { Actor enemies = getOneObjectAtOffset(0, 0, Enemies.class); if(enemies != null) { getWorld().removeObject(enemies); } } public void killEnemies() { if(hit(Enemies.class)) { kill(); getWorld().removeObject(this); } else { remove(); Range(); } } public void Range() { if(timer == 5) { getWorld().removeObject(this); } timer++; } }
Ryaj987 Ryaj987

2020/4/23

#
That's the original. I tried it with the removeTouching(enemies) as dan suggested to the same effect
danpost danpost

2020/4/23

#
What is wrong with:
public void act()
{
    kill();
}
Ryaj987 Ryaj987

2020/4/24

#
It's still doing it and I can't figure it out for the life of me
danpost danpost

2020/4/24

#
Ryaj987 wrote...
It's still doing it and I can't figure it out for the life of me
Do you have collision code in the Enemies class as well?
Super_Hippo Super_Hippo

2020/4/24

#
Okay sorry, apparently I didn’t read your first post carefully. Didn’t notice that the code was available. I looked at your code and I can tell you what’s wrong with it. You code does remove the enemy. It is not really efficient or easy to read, but it does remove it. The problem is that the world has a list of all skeletons and is constantly updating the location so they correctly move with the background when that moves. The following code (line 142–144 in class “ground”) reads: “if the skeleton isn’t in the world, add it”. And that’s what’s happening. It is removed and then, in the next act cycle, it is added again. That’s also why you noticed the “flashing”.
                if(thisSkeleton.getWorld() == null) {
                    addObject(thisSkeleton, screenX, screenY);
                }
You need to login to post a reply.