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

2023/10/27

Another problem occur while I tried to make my breakout game; please help!

yesineedhelp yesineedhelp

2023/10/27

#
Written in my Paddle class:
private int speed = 8;
private int p2time = 10; //field for how long this powerup can be use for
private int speed = 8;

public void act()
{
checkPUp2();
}

public void checkPUp2()
    {
       if(isTouching(PowerUp2.class))
        {
           //Increase paddle's speed
           Greenfoot.playSound("PowerUp 
           Sound.wav");
           removeTouching(PowerUp2.class);
           p2time--;
           if (p2time > 0) 
           {
               speed = speed +10;
           }
            else if (p2time == 0)
           {
               speed = speed;
            }
           p2time = 10;
        } 
    }
The ability to speed up the Paddle's speed is working, but the timer for how long I can use it doesn't. Currently, when the Paddle touchs the PowerUp2 actor, the Paddle's movement is faster but it doesn't go back to the normal speed. Please help me with the time thingy, and thank you!
Super_Hippo Super_Hippo

2023/10/27

#
The code starting from line 14 is only executed a single time (for each power up). The PowerUp2 is removed and p2time is changed from 10 to 9 and the speed is increased from 8 to 18. Then p2time is set back to 10. This code is not executed several times until the p2time reaches 0. It never reaches 0. It’s only changed briefly to 9 and right back to 10 where it stays.
yesineedhelp yesineedhelp

2023/10/27

#
I understand what you mean. But how do I make it so that p2time can eventually get to zero.
Super_Hippo Super_Hippo

2023/10/27

#
You could set p2time to 10 (or maybe more) when removing the PowerUp2. Then in act, you can reduce the variable each act cycle and when it hits 0, you set the speed to normal again.
yesineedhelp yesineedhelp

2023/10/27

#
So like this:
private int speed = 8;
private int p2time;

public void act()
{
checkPUp2();
p2time--;
}

public void checkPUp2()
    {
        if(isTouching(PowerUp2.class))
        {
           Greenfoot.playSound("PowerUp Sound.wav");
           removeTouching(PowerUp2.class);
           p2time = 10;

            if (p2time > 0)
           {
               speed = speed +10;
               
           }
           else if (p2time == 0)
           {
               speed = speed;
            }
          
           
        } 
    }
I try it and again, it speeds the Paddle but doesn't stop speeding after p2time ==0. I'm still new to Greenfoot, if you can post a code similar to this, that would be helpful. Thank you!
yesineedhelp yesineedhelp

2023/10/27

#
Please help me soon as possible, this is a school project, due tonight. Will reallly appreciate it!
Super_Hippo Super_Hippo

2023/10/28

#
Could look like this:
private final int defaultSpeed = 8;
private int currentSpeed = defaultSpeed;

private final int p2duration=10, p2speed=18;
private int p2time;

public void act()
{
    checkPUp2();
}

public void checkPUp2()
{
    if(isTouching(PowerUp2.class))
    {
        Greenfoot.playSound("PowerUp Sound.wav");
        removeTouching(PowerUp2.class);
        p2time = p2duration;
        currentSpeed = p2speed;
    }
    else if (p2time > 0)
    {
        if (--p2time==0)
        {
            currentSpeed = defaultSpeed;
        }
    }
}
You need to login to post a reply.