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

2019/11/26

Pause Code of one Object

insurqassable insurqassable

2019/11/26

#
Hey there, I'm programming a little game. You have to walk through different rooms and find an exit. In front of the exit, there is a lock that needs to be opened. The code for the lock is the following:
public class KeyLock extends Actor
{
    /**
     * Act - do whatever the KeyLock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
    public void open(){
        setImage("KeyLockOpen.png");
        GameWorld world=(GameWorld)getWorld();
        world.removeObject(this);
    }
}
The code get's called by another Class called Player. It say's:
public class Player extends Actor
{
private boolean hasKey;
public Player(){
hasKey=true;
}
public void act(){
        openLocks();
    }    
public void openLocks(){
        KeyLock northLock=(KeyLock)getOneObjectAtOffset(0,-this.getImage().getHeight()/2-3,KeyLock.class);
        KeyLock eastLock=(KeyLock)getOneObjectAtOffset(this.getImage().getWidth()/2+3,0,KeyLock.class);
        KeyLock southLock=(KeyLock)getOneObjectAtOffset(0,this.getImage().getHeight()/2+3,KeyLock.class);
        KeyLock westLock=(KeyLock)getOneObjectAtOffset(-this.getImage().getWidth()/2-3,0,KeyLock.class);
        if(northLock!=null&&hasKey==true&&Greenfoot.isKeyDown("e")){
            northLock.open();
        }
        if(eastLock!=null&&hasKey==true&&Greenfoot.isKeyDown("e")){
            eastLock.open();
        }
        if(southLock!=null&&hasKey==true&&Greenfoot.isKeyDown("e")){
            southLock.open();
        }
        if(westLock!=null&&hasKey==true&&Greenfoot.isKeyDown("e")){
            westLock.open();
        }
    }
}
Now to my problem: I want to have a little bit of time between the image of the lock getting changed and the lock getting deleted, so you can actually see the opened Lock. Adding a delay will pause the whole program, which is not what I want. Are there any options to pause the code of/delay only one Actor?
danpost danpost

2019/11/26

#
insurqassable wrote...
<< Code Omitted >> I want to have a little bit of time between the image of the lock getting changed and the lock getting deleted, so you can actually see the opened Lock.
Add an int timer field to the KeyLock class. Set it to some positive value (30-60 maybe) when opened. Use act to count down the timer and upon reaching zero, remove the actor.
You need to login to post a reply.