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

2016/6/18

Changing a door image from a closed door to an open one after the object has been removed.

bilibull bilibull

2016/6/18

#
So here is the gist of it; i been trying to make my door that gets removed after its clicked, to change its image from the closed door to an open door. The ways I've so far: The typical setImage(_filename_) but then realised you cant really change an image from an object that has been previously removed. So the second thing I though of trying to do is creating another object with the name openDoor and have it replace the previous door when clicked. But that is pretty much where i got stuck. So the question here is is there a way to change the image of a removed object by either replacing the old object with a new one? or at least have it change its old image to a new one after its been removed? Here is the door code just as a reference:
    private void chkDoorOpen()

    {
        Dungeon dungeon = (Dungeon) getWorld();
        Lockpick_total lTotal = dungeon.counter;
        if (Greenfoot.mouseClicked(this) && lTotal.getLockpick_total() > 0)
        {            
            lTotal.sub(1);
            getWorld().removeObject(this);
        }
    }    
So just a quite brief up; the lockpicks are just a way I used to make the doors open. Think of them as the keys used to unlock/remove the doors.
danpost danpost

2016/6/18

#
The best way to do this is to use the same Door object for both open and closed states. Changing the image is only have the solution because you still need to know what state the door is in (opened or closed). You could add a boolean field to track this state; but then you would need to add more code to access the value of the field because it is within the Door class. An easier way is to have the opened door image saved in a static field in the Door class. Use it to set the new image when the door is opened and check to see if the image of the door is that image to check if the door is opened:
/** in door class */
// class field
public static final GreenfootImage openImage = new GreenfootImage("opendoor.png"); // adjust file name as needed

// to open
setImage(openImage); // or 'door.setImage(Door.openImage);' from another class

// to check if open in class of character to enter door 
Actor door = getOneIntersectingObject(Door.class);
if (door != null && door.getImage() == Door.openImage)
bilibull bilibull

2016/6/19

#
I never though of using a static field; that actually does seem interesting. I will definitely give it a go to all of the methods you gave so far and have some fun. Quick question about the static final though; since its public. My code has multiple doors that use the same code when it comes to opening/removing the door. Since using a duplicate class was the only way i knew on making the same code work for different worlds. But back to the subject at hand. Wouldn't there be a problem if used the same public static variable for all three of my doors? since they will all try to call on the already existing openImage value from the first door? I know I am not making much sense at the moment; but in the off chance you understood what i meant. Please do tell me if I am being horrible stupid or just completely misunderstanding on how a public static final variable works. Thank you for your time and the help you have provided!
danpost danpost

2016/6/19

#
bilibull wrote...
Wouldn't there be a problem if used the same public static variable for all three of my doors? since they will all try to call on the already existing openImage value from the first door?
What is the problem with using just one Door class to create the doors in different worlds?
how a public static final variable works
'public static final' just means that the field, which is constant ('final'), belongs to the class (not to any object(s) created from the class) ('static') and is accessible to all classes within the project ('public').
bilibull bilibull

2016/6/19

#
I would't really call it a problem but more like lack of knowledge. You see, the door code requires to call upon the world in which the door is placed in. Since without that i wont be able to use the lockpicks (call them keys for now) as a condition to open the door. So in order words this code
  Dungeon dungeon = (Dungeon) getWorld();
        Lockpick_total lTotal = dungeon.counter;
        if (Greenfoot.mouseClicked(this) && lTotal.getLockpick_total() > 0)
        {            
            lTotal.sub(1);
            getWorld().removeObject(this);
        }
Would most likely fail to work on the Dungeon_2 world. Thus making a duplicate class and just changing the code in order to fit for Dungeon_2 was a much more logical choice.
danpost danpost

2016/6/19

#
bilibull wrote...
I would't really call it a problem but more like lack of knowledge. You see, the door code requires to call upon the world in which the door is placed in. Since without that i wont be able to use the lockpicks (call them keys for now) as a condition to open the door.
You could always do something like this:
Lockpick_total lTotal = null;
if (getWorld() instanceof Dungeon) lTotal = ((Dungeon)getWorld()).counter;
if (getWorld() instanceof Dungeon_2) lTotal = ((Dungeon_2)getWorld()).counter;
// etc.
If the Lockpick_total object is an Actor object that is placed in the world, and the one and only object of its type in any world, then you can just do this:
Lockpick_total lTotal = (Lockpick_total)getWorld().getObjects(Lockpick_total.class).get(0);
bilibull bilibull

2016/6/22

#
Oh... so thats how you do it without duplication! Thanks you so much!
You need to login to post a reply.