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
danpost danpost

2013/5/21

#
Then, I stand corrected, if I am understanding you properly. The code that bourne gave should be sufficient (with my one added line).
forest forest

2013/5/21

#
Thank you! But I tried it out, exactly as bourne did it + what you have added, but my spider doesn't go up. It does go sideways but I never see it higher than it's original position
bourne bourne

2013/5/21

#
Could we see what you have put together?
forest forest

2013/5/21

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ListIterator;
/**
 * Write a description of class Spider here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Spider extends Actor
{
    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))  
        {
            if (randX > getX())
                    setLocation(getX() + 1, getY());
            else
                    setLocation(getX() - 1, getY());
        } 
        else  
        {  
            turnTowards( getX(), randY);  
            move(1);  
        }  
        setRotation(0); 
    }    
    else    
        newRandomLoc();    
}    
public void addedToWorld(World world)    
{    
    newRandomLoc();    
}   
}
bourne bourne

2013/5/21

#
What's the World's dimensions and cell size?
forest forest

2013/5/21

#
super(800,600,1,false);
danpost danpost

2013/5/21

#
I would code it something like this:
private int randX = -1;
private int randY = -1;

public void act()
{
    if (randX == -1 || (randX ==getX() && randY == getY())) {
        randX = Greenfoot.getRandomNumber(getWorld().getWidth());
        randY = Greenfoot.getRandomNumber(getWorld().getHeight());
    }
    int dx = randX-getX();
    int dy = randY-getY();
    int absDx = (int)Math.abs(dx);
    int absDy = (int)Math.abs(dy);
    if (Greenfoot.getRandomNumber(absDx+absDy) < absDx) {
        setLocation(getX()+dx/absDx, getY());
    } else {
        setLocation(getX(), getY()+dy/absDy);
    }
}
forest forest

2013/5/21

#
it's still doing the same thing though, the spider just dips down from view, and never goes back up, yet I can see it's web moving. I'm trying for it so that it's above ground most of the time as it will be dropping stuff randomly too
danpost danpost

2013/5/21

#
Please show what the Spider class look like now.
forest forest

2013/5/21

#
haven't changed anything yet :/
private int randX = -1;  
private int randY = -1;  
  
public void act()  
{  
    if (randX == -1 || (randX ==getX() && randY == getY())) {  
        randX = Greenfoot.getRandomNumber(getWorld().getWidth());  
        randY = Greenfoot.getRandomNumber(getWorld().getHeight());  
    }  
    int dx = randX-getX();  
    int dy = randY-getY();  
    int absDx = (int)Math.abs(dx);  
    int absDy = (int)Math.abs(dy);  
    if (Greenfoot.getRandomNumber(absDx+absDy) < absDx) {  
        setLocation(getX()+dx/absDx, getY());  
    } else {  
        setLocation(getX(), getY()+dy/absDy);  
    }  
}  
danpost danpost

2013/5/21

#
I know what is going on.
// change instance field declarations to
private int randX = -1000;
private int randY = -1000;
// change this part in line 6
randX == -1
// to
randX == -1000
// and change line 8 to this
randY = Greenfoot.getRandomNumber(getWorld().getHeight())-getImage().getHeight()+20;
This will compensate for the fact that the spider itself is not the center of the image used.
danpost danpost

2013/5/21

#
I had to make some slight corrections to the changes above (through editing).
forest forest

2013/5/21

#
spider keeps going up then disappears, never coming back down again?
danpost danpost

2013/5/21

#
Sorry, was only supposed to be half the height:
//  on line 8
randY = Greenfoot.getRandomNumber(getWorld().getHeight())-getImage().getHeight()/2+20; 
forest forest

2013/5/21

#
OH wow thank you so much! spider moves perfectly. could you please explain why line1 and 2 randX and randY = -1000 and why I must use abs?
There are more replies on the next page.
1
2
3