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

2021/6/22

Power Up Problem

Gabe1098 Gabe1098

2021/6/22

#
I'm trying to make a game where if you touch the orange you're speed goes up a little and then goes back to normal. But when I made it, the speed goes up, but it doesn't go down after a little. Can you help me? here's my code
public class Player extends Actor
{
    int speed = 2;
    int repeat = 0;
    /**
     * Act - do whatever the Player wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (Greenfoot.isKeyDown("right")) {
            setLocation(getX()+speed,getY());
            setRotation(0);
        }
        if (Greenfoot.isKeyDown("left")) {
            setLocation(getX()-speed,getY());
            setRotation(180);
        }
        if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(),getY()-speed);
            setRotation(-90);
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(),getY()+speed);
            setRotation(90);
        }
        if (isTouching(Orange.class)) {
            if (repeat<50) {
                speed=3;
                repeat++;
            }
            else {
               speed=2;
               repeat=0;
            }
        }
    }
}
Gabe1098 Gabe1098

2021/6/22

#
of I updated it
public class Player extends Actor
{
    int speed = 2;
    int repeat = 0;
    int check = 1;
    /**
     * Act - do whatever the Player wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setImage(new GreenfootImage(""+repeat, 24, Color.WHITE, Color.BLACK));
        if (Greenfoot.isKeyDown("right")) {
            setLocation(getX()+speed,getY());
            setRotation(0);
        }
        if (Greenfoot.isKeyDown("left")) {
            setLocation(getX()-speed,getY());
            setRotation(180);
        }
        if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(),getY()-speed);
            setRotation(-90);
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(),getY()+speed);
            setRotation(90);
        }
        if (isTouching(Orange.class)) {
            if (repeat<50) {
                speed=3;
                check=2;
            }
             if (repeat>50) {
               speed=2;
               repeat=0;
               check=1;
            }
         }
        if (check==2) {
             repeat++; 
        }
    }
}
but it still doesn't work
You need to login to post a reply.