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

2017/4/26

Change the speed of an actor after clicking a button?

Nozarashi Nozarashi

2017/4/26

#
Hello, I want to create levels, that all lead to one world, but I need to change the speed of and actor, in this case a Bee. For example, if i'm on the level select screen, if i click on level one, I want the speed of the Bee to be at 1, but if I click on level 2, I want the Bee's speed to be increased to 2.
danpost danpost

2017/4/26

#
You can have the speed field in the Bee class be static:
public static int speed;
Then, for each button, you can set the speed of the bee(s) created from the class:
if (Greenfoot.mouseClicked(this))
{
    Bee.speed = 2;
    Greenfoot.setWorld(new Level2());
}
Nozarashi Nozarashi

2017/4/26

#
danpost wrote...
You can have the speed field in the Bee class be static:
public static int speed;
Then, for each button, you can set the speed of the bee(s) created from the class:
if (Greenfoot.mouseClicked(this))
{
    Bee.speed = 2;
    Greenfoot.setWorld(new Level2());
}
Okay, I added the code, but it doesn't seem to be changing the speed. if I inspect the Bee, it says the speed is 4 but its really 2 or 1. Here is the code if it helps: Bee:
import greenfoot.*;

public class Bee extends Actor
{
    public static int speed;
    public void act() 
    {
        move(2);
        chaseSpider();

    }    
    
    public void chaseSpider()
    {
        turnTowards(Spider.spiderX, Spider.spiderY);
        
    }
}

Button:
import greenfoot.*; 
public class Hard extends Actor
{
   public void act() 
    {
       if (Greenfoot.mouseClicked(this)) 
       {
           Bee.speed = 4;
           Greenfoot.setWorld(new Floor());
       }
   }
}
Nozarashi Nozarashi

2017/4/26

#
I believe I fixed it. Correct me if i'm wrong. I changed move(2); to move(Bee.speed);
danpost danpost

2017/4/26

#
Nozarashi wrote...
I believe I fixed it. Correct me if i'm wrong. I changed move(2); to move(Bee.speed);
That works. You could just use this, however:
move(speed);
since it is in the Bee class.
Nozarashi Nozarashi

2017/4/26

#
danpost wrote...
Nozarashi wrote...
I believe I fixed it. Correct me if i'm wrong. I changed move(2); to move(Bee.speed);
That works. You could just use this, however:
move(speed);
since it is in the Bee class.
Okay, thanks for the help.
You need to login to post a reply.