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

2017/3/15

I need my subclass of Actor(my character) to gain a boost in speed

Katsura Katsura

2017/3/15

#
Good evening fellow greenfooters. I would really appreciate assistance with my issue, I need my subclass of Actor(my Character that the player controls) to gain a boost of speed for 2 seconds every 3 seconds. I haven't been able to figure this out and I'm pulling my hair out XD! Also when the Character gets this 2 second speed boost I want him to become invulnerable and be able to eat the enemy "creeps" its kinda like how PACMAN works when the world goes blue. Here is my movement code because I'm sure it has something to do with that.
 public void checkKeyPress()
    {
        
     if(Greenfoot.isKeyDown("left"))
    {
     int X= getX();
     int Y= getY();
     setLocation(X-4,Y);
     
    }

    if(Greenfoot.isKeyDown("right"))
    {
        int X= getX();
        int Y= getY();
        setLocation(X+4,Y);
    }

    if(Greenfoot.isKeyDown("up"))
    {
        int X= getX();
        int Y= getY();
        setLocation(X,Y-4);
    }

    if(Greenfoot.isKeyDown("down"))
    {
        int X= getX();
        int Y= getY();
        setLocation(X,Y+4);
    }
    }
Best regards Katsura
danpost danpost

2017/3/15

#
Use just need something to multiply the speed values (literal '4's) with to act as a variable speed factor. With a value of one, the character will move normally and with a value of two, the character will double-time it. Like this:
private int factor = 1;

public void checkKeys()
{
    int dx = 0, dy = 0;
    if(Greenfoot.isKeyDown("left")) dx--;
    if(Greenfoot.isKeyDown("right")) dx++;
    if(Greenfoot.isKeyDown("up"))dy--;
    if(Greenfoot.isKeyDown("down")) dy++;
    setLocation(getX()+4*dx*factor, getY()+4*dy*factor);
}
Now, you just need to control the value of the factor. An int timer is called for here:
private int timer;

// in act or method it calls
timer = (timer+1)%300;
if (timer%180 == 0) factor = 1+(timer/180);
Katsura Katsura

2017/3/15

#
Good evening danpost. Thank you very much for your assistance, found it very helpful and I really appreciate it. Hope you have a great evening. Best regards Katsura
danpost danpost

2017/3/15

#
As far as invincibility and eating enemy creeps, you can just check on the value of 'factor' -- if '1' cannot eat and not invincible, if' '2' can and is.
Scarlett Scarlett

2017/3/16

#
Hi I know what to do
You need to login to post a reply.