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

2012/1/6

Why won't the net go up??

b321234 b321234

2012/1/6

#
So initially I want it to go down, and when it hits Y = 53 it changes its direction and goes up. But it just keeps going down but I can't see where the problem is.
 public void act() 
    {
        movement(checkDirection());
    }    

    public int checkDirection(){
        int speed=0;
        if (getY()>330){
            speed= -5;
        }
        if (getY()<53){
            speed= 5;
        }
        return speed;
    }

    public void movement(int a){
        setLocation(getX(),getY()+a);
    }
Thanks ;))))
davmac davmac

2012/1/6

#
From the code you posted it looks like if getY() is between 53 and 330, it won't move at all because checkDirection() will always return 0. Eg, if getY() is 100: 07. int speed = 0; Ok, speed is now 0. 08. if (getY() > 330) { No, getY() is 100, it's less than 330, so we skip to line 11: 11. if (getY() < 53) { No again, getY() is 100 which is greater than 53, skip to line 14: 14. return speed; Speed is 0, so it returns 0. The actor then doesn't move at all.
b321234 b321234

2012/1/6

#
davmac thanks for your reply but can you tell me how should I change it?? I just want the net to move up and down..
danpost danpost

2012/1/6

#
Just remove line 7 from the code (int speed = 0;). I am guessing you already have speed as an instance variable for the actor in question.
Duta Duta

2012/1/6

#
You could move "int speed=0;" from being a local variable to a field, and also change its initialization value to 5 instead of 0.
b321234 b321234

2012/1/7

#
@danpost @duta, that was what I did at first but it just kept going DOWN. It ddn even go up a little :(
danpost danpost

2012/1/7

#
I created a simple scenario, first applying the code you supplied to the actor (the world just added the actor to the center of the world), and got no movement from the actor, as anticipated. Then, I commented out line 7 of your code (int speed = 0;), and got an up-and-down repetition of movement as you wanted. There must be more to your code than you are showing (or something lacking from what I now have). This is what I have: The World class:
import greenfoot.*;

public class Arena extends World
{
    public Arena()
    {
        super(600, 400, 1);
        addObject(new Net(), 300, 200);
    }
}
and in the Net class:
import greenfoot.*;

public class Net extends Actor
{
    int speed = 5;

    public void act() 
    {
        movement(checkDirection());
    }    

    public int checkDirection()
    {
        // int speed = 0;  Commented out
        if (getY() > 330){
            speed = -5;
        }
        if (getY() < 53){
            speed = 5;
        }
        return speed;
    }

    public void movement(int a)
    {
        setLocation(getX(), getY() + a);
    }        
}
Duta Duta

2012/1/7

#
Yeah, @danpost that's what I was talking about
You need to login to post a reply.