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

2015/10/19

how do I wait

RadiantCheddar RadiantCheddar

2015/10/19

#
When I use Thread.sleep every actor stops but all I want is for it to wait
danpost danpost

2015/10/19

#
You probably should not be using threads. Also, you need to be specific as to what you are trying to do; supply some code; be clear as to what is to "wait" -- what all is to wait -- when -- for how long -- etc.
RadiantCheddar RadiantCheddar

2015/10/19

#
import greenfoot.*;
import java.lang.Math;
public class Ball extends Actor
{
    static int ballX=100;
    static int ballY=0;
    static boolean isTouching;
    int speed = 2;
    boolean allowedToBoost=true;
    public void act() 
    {   
        GreenfootImage myImage = new GreenfootImage("bee.png");
        setImage(myImage);
        ballX=getX();
        ballY=getY();
        if(Greenfoot.isKeyDown("right")){
            turn(2);
        }
        if(Greenfoot.isKeyDown("left")){
            turn(-2);
        }
        if(Greenfoot.isKeyDown("up")){
            move(speed);
        }
        if(Greenfoot.isKeyDown("space") && allowedToBoost==true){
           speed=5;
           try{
               Thread.sleep(1000);
           }
           catch(Exception e){
            }
           allowedToBoost=false;
           speed=2;
        }
    }
    public void moveX(int x){
        setLocation(getX()+x,getY());
    }
    public void moveY(int y){
        setLocation(getX(),getY()+y);
    }
}
When I used Thread.sleep(1000) it stopped every actor but all I want is for it to be like a clock and after 1 second it reverts to speed 2
danpost danpost

2015/10/19

#
Like I said, you probably should not be using thread commands. You can use an int field to count act cycles or a long field to hold the beginning time (when the boost is started). Then, if using the counter, zero the counter when boost is started, increment it each act and check its value for, say, 60 (around one second in a scenario running at normal speeds) to determine when to revert to speed of two. If using system time, save in field when boost is started and check its difference with current time for one thousand milliseconds (one second). Things to know: (1) java.lang.Math does not need to be imported; it is loaded automatically when your project is compiled; (2) none of your fields should be declared 'static' as they are all object fields (in this case, fields that each ball created will have); (3) in fact, none of your 'static' fields are being used within the class and should be removed; even if you refer to them from other classes, the values that 'ballX' and 'ballY' contain can be gotten directly by calling 'getX' and 'getY' on the ball object; (4) currently, each ball will only be able to "boost" just one time (nothing is setting 'allowedToBoost' back to true)
RadiantCheddar RadiantCheddar

2015/10/20

#
I have another actor called bee which is chasing the ball. How would I get the x of the ball from within the bee class. Also each ball is only supposed to be able to boost once as of right now.
danpost danpost

2015/10/20

#
RadiantCheddar wrote...
I have another actor called bee which is chasing the ball. How would I get the x of the ball from within the bee class. Also each ball is only supposed to be able to boost once as of right now.
In the Bee class, you can use:
if (!getWorld().getObjects(Ball.class).isEmpty())
{
    int ballX = ((Actor)getWorld().getObjects(Ball.class).get(0)).getX();
    // etc.
,
You need to login to post a reply.