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

2012/8/13

boss movement pattern

davemib123 davemib123

2012/8/13

#
Hi trying to get the boss to move between certain distances but it doesnt seem to work out:
    public void bossMovementPattern1(){
        if(getX() == 400)
        {
            setLocation ( getX() - velocity, getY() );
        }
else {
 if(getX() <= 750)
        {
            setLocation ( getX() + velocity, getY() );
        }
}
    }
Trying to move between 750 and 400 only. Once it reaches either end I want it to change direction to the other.
erdelf erdelf

2012/8/13

#
are you sure that this line:
if(getX() == 400) 
shouldn't be this:
if(getX() >= 400) 
davemib123 davemib123

2012/8/13

#
I did try that before I posted but the boss does not move backwards. It just get stuck at 400.
SPower SPower

2012/8/13

#
Change this to your code:
if(getX() == 400) {  
    setLocation ( getX() - velocity, getY() );
    System.out.println("Velocity is: "+velocity);
}
This will log the velocity out to the console. If that is 0, that's where the error is.
davemib123 davemib123

2012/8/13

#
shows the velocity as 4. ive uploaded the scenario http://www.greenfoot.org/scenarios/5799 you will have to download and add the boss in manually
SPower SPower

2012/8/13

#
I know the problem: it's this part:
if(getX() <= 750) {
    setLocation ( getX() + velocity, getY() );
change it to:
if(getX() <= 750) {
    setLocation ( getX() - velocity, getY() );
After that, it works.
SPower SPower

2012/8/13

#
This is what happened: when the first condition (getX() == 400) became false, the other was true. Then, it would move the Hunter a bit to the right. Now, the first condition becomes true again, and it moves a bit to the left. This repeated itself.
davemib123 davemib123

2012/8/14

#
still not functioning .....
SPower SPower

2012/8/14

#
What's not functioning? It moves to the right, doesn't it?
davemib123 davemib123

2012/8/14

#
nope, moves to the left and then just gets stuck.
SPower SPower

2012/8/14

#
Do you really use this code in Mover:
/**
     * Move between set distances
     */
    public void bossMovementPattern1(){
        if(getX() >= 400) {
            setLocation ( getX() - velocity, getY() ); 
        } else {
            if(getX() <= 750) {
                setLocation ( getX() - velocity, getY() );
            }
        }
    }
danpost danpost

2012/8/14

#
You need to also check and change the direction of movement:
public void bossMovementPattern1()
{
    setLocation(getX() + velocity, getY()); // move
    if (velocity < 0 && getX() <= 400)
    { // moving left and past left limit
        setLocation(400, getY()); // reset position
        velocity = -velocity; // change direction
    }
    if (velocity > 0 && getX() >= 750)
    { // moving right and past right limit
        setLocation(750, getY()); // reset position
        velocity = -velocity; // change direction
    }
}
davemib123 davemib123

2012/8/14

#
yep. I've uploaded the scenario with that in. edit: works great. many thanks for that.
You need to login to post a reply.