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

2013/5/21

HELP PLEASE! move to setLocation

1
2
3
forest forest

2013/5/21

#
I have this code so far, and I have randomized it so that my actor would go to different parts of the screen. I have setLocation randomly so that it's moves are unpredictable. But how do I make it so that my actor doesn't just show up in the new location, but would actually make it's way there one move at a time?
bourne bourne

2013/5/21

#
So that it moves in a straight line toward the location you could have something like:
private int randX, randY;

private void newRandomLoc()
{
    randX = ...;
    randY = ...;
}

public void act()
{
    // If not close to destination, move toward it.
    if (Math.sqrt(Math.pow(randX - getX(), 2) + Math.pow(randY - getY(), 2)) > 10)
    {
        turnToward(randX, randY);
        move(1); // Some distance
    }
}
If you want to account for moving around objects, take a look at this: http://www.greenfoot.org/scenarios/8226
forest forest

2013/5/21

#
This doesn't work! I tried it, or should I have done exactly as you did? With the private int and private void? public void act() { int rdrctn = Greenfoot.getRandomNumber(4) ; int move = (Greenfoot.getRandomNumber(5));//0-5 steps if (Greenfoot.getRandomNumber(100) < 50)//50% chance of moving each frame { if (rdrctn == 0)//move left { turnToward( getX() - move , getY() ) ; move(1); } if (rdrctn == 1)//move right { turnToward( getX() + move , getY() ) ; move(1); } if (rdrctn == 2)//up { turnToward( getX() , getY() - move ) ; move(1); } if (rdrctn == 3)//move down { turnToward( getX() , getY() + move ) ; move(1); } }
bourne bourne

2013/5/21

#
You need to not pick a random location every act cycle. Below, it picks a random location, and doesn't pick a new location until it reaches there.
private int randX, randY;  
  
private void newRandomLoc()  
{  
    randX = Greenfoot.getRandomNumber(getWorld().getWidth());  
    randY = Greenfoot.getRandomNumber(getWorld().getHeight());  
}  
  
public void act()  
{  
    // If not close to destination, move toward it.  
    if (Math.sqrt(Math.pow(randX - getX(), 2) + Math.pow(randY - getY(), 2)) > 5)  
    {  
        turnToward(randX, randY);  
        move(1); // Some distance  
    }
    else
        newRandomLoc();
}
public void addedToWorld(World world)
{
    newRandomLoc();
}
forest forest

2013/5/21

#
Your codes makes more sense, thank you! but the actor that I am moving is a spider and must only go in one direction; vertical or horizontal, and not diagonal as it has a web to move on. Should I then just separate the command for X and Y?
bourne bourne

2013/5/21

#
Yeah something like this: (where it moves in direction that is greatest difference)
private int randX, randY;    
    
private void newRandomLoc()    
{    
    randX = Greenfoot.getRandomNumber(getWorld().getWidth());    
    randY = Greenfoot.getRandomNumber(getWorld().getHeight());    
}    
    
public void act()    
{    
    // If not close to destination, move toward it.    
    if (Math.sqrt(Math.pow(randX - getX(), 2) + Math.pow(randY - getY(), 2)) > 0)    
    {    
        if (Math.abs(getX() - randX) > Math.abs(getY() - randY))
        {
            turnToward( randX  , getY() ) ;
            move(1);
        }
        else
        {
            turnToward( getX(), randY);
            move(1);
        }
    }  
    else  
        newRandomLoc();  
}  
public void addedToWorld(World world)  
{  
    newRandomLoc();  
}  
forest forest

2013/5/21

#
what must I change so that when it moves horizontally, the image does not rotate at all?
danpost danpost

2013/5/21

#
If you are using the last code posted, insert the following line between lines 23 and 24:
setRotation(0);
bourne bourne

2013/5/21

#
Instead of: if (Math.abs(getX() - randX) > Math.abs(getY() - randY)) { turnToward( randX , getY() ) ; move(1); } Have this: if (Math.abs(getX() - randX) > Math.abs(getY() - randY)) { if (randX > getX()) setLocation(getX() + 1, getY()); else setLocation(getX() - 1, getY()); }
danpost danpost

2013/5/21

#
I do not think that the given code will do what you want. When the offsets equalize, your actor will basically be going diagonally again.
bourne bourne

2013/5/21

#
But it only moves horizontally or vertically every act cycle, not both.
bourne bourne

2013/5/21

#
It needs to be tailored to what he means by
as it has a web to move on
danpost danpost

2013/5/21

#
If it is moving either horizontally or vertically every act, then every pixel of the world is open to be a location for the spider. But the spider must stay on the web! What exactly is the layout of the web? If you could describe it in detail, we might be able to help.
danpost danpost

2013/5/21

#
@bourne, I may be jumping the gun, here. If forest is moving the spider from node to node on each act cycle, then your method would be sufficient. However, forest was showing 'move(1)'s in the code, which means that this is not the case.
forest forest

2013/5/21

#
actor's image is a spider with a really long web sticking out from it's top. I have disabled the boundaries so that the actor could go higher than the top w/o being stopped.
There are more replies on the next page.
1
2
3