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

2020/11/30

How to remove/add an actor from a different world

Greenhorn4747 Greenhorn4747

2020/11/30

#
I'm creating an escape game and just as the title says when I click on an actor I want another actor from a different world to be removed and an other actor to take it's place. I'm new to greenfoot by the way, tnak you in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void act()
    {
        if(Greenfoot.mouseClicked(this))
        {
            Greenfoot.playSound("Piston.wav");
            world a = inkomthi
            a.removeObjects(getWorld().getObjects(keuthi.class));
            a.addObjects(getWorld().getObjects(keukthi.class));
            a.removeObjects(getWorld().getObjects(bibthi.class));
            a.addObjects(getWorld().getObjects(bliblotthi.class));           
        }
    }
    public kistthi(){
    GreenfootImage image = getImage();
    getImage().setTransparency(0);
    }
danpost danpost

2020/11/30

#
Assuming inkomthi is not the active world, lines 7 and 9 do nothing as you cannot remove actors from a world they are not in. That is, the listed set of actors acquired from the current world are necessarily in the current world and therefore not in another currently inactive world to be removed from. Clarification is needed as far as what actor in inkomthi is being replaced and what it ii being replaced with. Also, show code of addObjects method.
Greenhorn4747 Greenhorn4747

2020/11/30

#
I'm trying to replace keuthi with keukthi, those are the actors I use move to a world and the player is supposed to have unlocked a new area hence I'm trying to remove the path to to old world and have the new one take its place. But this is supposed to happen in a third world. "That is, the listed set of actors acquired from the current world are necessarily in the current world and therefore not in another currently inactive world to be removed from." I don't really know what you mean by that, but neither keuthi or keukthi are in the world that code is supposed to be happening in. "a.addObjects(getWorld().getObjects(bliblotthi.class)); " I thought this added the actor in the third world, so that's the only code I'm using to add an Object.
danpost danpost

2020/12/1

#
Greenhorn4747 wrote...
"a.addObjects(getWorld().getObjects(bliblotthi.class)); " I thought this added the actor in the third world, so that's the only code I'm using to add an Object.
Thing is, there is no such method called addObjects unless you defined one yourself. There is an addObject method (without the 's'), but it requires three parameters -- the actor to add and the x and y location coordinates where the actor is to be added; but, you are giving only a list of actors (albeit, an empty one).
neither keuthi or keukthi are in the world that code is supposed to be happening in.
If neither actor type is in current world, then why use getObjects on the current world? getWorld().getObjects(<< class >>) will return an empty list for keuthi.class and keukthi.class if no instances of those classes are in the current world. So, you have a world, inkomthi, containing exactly one instance of keuthi, which is to be replaced by an instance of keukthi. Is that correct?
Greenhorn4747 Greenhorn4747

2020/12/1

#
I never got the addObject to work it always gave either the "cannot find symbol - variable ..." error or if I put .class after the actor, the "java.lang... cannot be converted to greenfoot actor. I thought the world a=inkomthi, would make count for inkomthi and that if I said: a.addObjects(getWorld() that would read as for the world inkomthi check the world check the actors and add the actor. "So, you have a world, inkomthi, containing exactly one instance of keuthi, which is to be replaced by an instance of keukthi. Is that correct?" yes exactly.
danpost danpost

2020/12/1

#
Greenhorn4747 wrote...
I never got the addObject to work it always gave either the "cannot find symbol - variable ..." error or if I put .class after the actor, the "java.lang... cannot be converted to greenfoot actor.
So, either you had a faulty reference to an actor or a reference to something that was not an actor at all. A class of type Actor or sub-type of Actor is not an Actor instance.
I thought the world a=inkomthi, would make count for inkomthi
Not sure what you mean by "make count for". That line says to declare a new variable, named a, to reference a world object (from a class of which you must have defined yourself as it is not the same as World defined by greenfoot) and assign it whatever object is assigned to a previously defined variable called inkomthi. It, in itself, does not create a world object. The new keyword is used to create objects.
and that if I said: a.addObjects(getWorld() that would read as for the world inkomthi check the world check the actors and add the actor.
How is inkomthi declared in the class and what is assigned to it? Show class codes, if you do not understand.
You need to login to post a reply.