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

2016/3/3

Make a method last for x time

Bassusour Bassusour

2016/3/3

#
Hello again So, I want my game to be a little like pacman. When pacman eats the bigger circle, he is capable of eating the ghosts, in a period of time. I want that too.
        /**
       * Denne metode sørger for at spise FD_special klassen. 
       */
       public void spis_fd_special()
       {
           if ( canSee(FD_special.class) )
           {
               eat(FD_special.class);
               Greenfoot.playSound("slurp.wav");
            }  
    }
As you can see, i havn't really reached far to accomplish that. The 'Big circle' in this example is the class FD_special. Also, when my instance eats the FD_special, i want it to become bigger, so it is visual that you ate the powerup. And at last, as the topic says, I want my class to only be able to this for a period of time.
Super_Hippo Super_Hippo

2016/3/3

#
First, you need to add a timer:
private int powerUpActive = 0;
Then, you can do it like this:
if (isTouching(FD_special.class))
{
    removeTouching(FD_special.class);
    powerUpActive = 300; //about 5 seconds, higher number=longer time
    Greenfoot.playSound("slurp.wav");
    setImage(bigImage);
}
For the bigger image, you could do this: (Usually it is better to scale down than to scale up. So if you can, use an image of the size of the big image (or even bigger) and scale it down. To do so, use a number below 1 where the 1.3 is right now to scale it up.)
GreenfootImage smallImage, bigImage;

public Pacman() //Name of the class
{
    smallImage = new GreenfootImage("pacman.png"); //name of the image file
    bigImage = new GreenfootImage("pacman.png");
    bigImage.scale((int)(bigImage.getWidth()*1.3), (int)(bigImage.getHeight()*1.3);
    setImage(smallImage);
}
Finally, you need this in the act-method, so the power up doesn't last forever:
public void act()
{
    if (powerUpActive > 0)
    {
        powerUpActive--;
        if (powerUpActive==0)
        {
            setImage(smallImage);
        }
    }
    
    //move around
}
You need to login to post a reply.