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

2019/4/5

How to remove objects in other class?

ordinaryblade ordinaryblade

2019/4/5

#
I want to create a RPG Game where an Arrow is shot. If it touches the Enemy it should disappear. This works so far. I want the Enemy to disappear aswell but it does not work. The Problem appears in the method kill(); This is my code in class Arrow.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Arrow here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Arrow extends Actor
{
    private int speed;
     
         
    public Arrow()
    {
        speed = 8;
        getImage().scale(getImage().getWidth()/28,getImage().getHeight()/8);
         
    }
    public void act()
    {
        move(speed);
        kill();
        disappear();
         
    }  
    public void disappear()
    {
        Actor noob;
        noob = getOneIntersectingObject(NPC.class);
        Actor wall;
        wall = getOneIntersectingObject(Obstacle.class);
         
        if(isAtEdge() || noob != null || wall!= null)// Verschwindet am Welt ende un beim auftreffen eines NPC
        {
            getWorld().removeObject(this);
             
        }
         
    }
    public void kill()
    {
         
        if( isTouching(NPC.class))
        {
            Actor mob;
            mob = new NPC();
             
            getWorld().removeObject(mob);
        }
    }
}
Normally nothing happens, but sometimes the treminal says: ava.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:714) at greenfoot.Actor.isTouching(Actor.java:979) at Arrow.töten(Arrow.java:43) at Arrow.act(Arrow.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) This message comes after hitting the Enemy. It takes me to the if statement in the kill method Thank you for help :-)
danpost danpost

2019/4/5

#
ordinaryblade wrote...
I want to create a RPG Game where an Arrow is shot. If it touches the Enemy it should disappear. This works so far. I want the Enemy to disappear aswell but it does not work. The Problem appears in the method kill(); This is my code in class Arrow. << Code Omitted >> Normally nothing happens, but sometimes the treminal says: << Error Trace Omitted >> This message comes after hitting the Enemy. It takes me to the if statement in the kill method
Insufficient code given. Please provide the entire Arrow class code. Also, provide an error trace that is produced from code given. As far as removing the enemy, it does no good to create a new one to remove (lines 46 to 49). You need to remove the one already created, in the world and touching the arrow. Use the removeTouching method.
ordinaryblade ordinaryblade

2019/4/6

#
Thank you. How do I use remove touching?
Super_Hippo Super_Hippo

2019/4/6

#
1
removeTouching(NPC.class)
You need to login to post a reply.