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

2012/5/4

how can I make this two codes in one

1
2
3
danpost danpost

2012/5/5

#
Maybe I now understand. Try this for student class:
import greenfoot.*;  

public class student extends school
{
     public void act() 
    {
        move(4);
        if ( Greenfoot.getRandomNumber(101) < 15 || atWorldEdge())  turn( (Greenfoot.getRandomNumber(2)  * 2 - 1) * 90);
        if (Greenfoot.getRandomNumber(2000) < 5) getWorld().addObject(new afval2(), getX(), getY());
    }
}
martijn13039 martijn13039

2012/5/5

#
danpost this is almost what I mean I'm glad you understand what I mean and the code workt great but only the studen can turn so that he walk on his head if he go down, but I want that he go down but than with his head normall but I really thank you for this help and the code works great
danpost danpost

2012/5/5

#
Instead of turn() and move(), maybe you should use setLocation() and setRotation(). Try the following:
int direction = 0;

public void moveAndTurn()
{
    int dx = direction % 2 == 0 ? 1 - direction : 0;
    int dy = direction % 2 == 1 ? 2 - direction : 0;
    setLocation(getX() + dx * 4, getY() + dy * 4);
    if (Greenfoot.getRandomNumber(101) < 15 || atWorldEdge()) direction += Greenfoot.getRandomNumber(2) * 2 - 1);
    int newRotation = ((dx == 0 ? 0 : (1 - dx) * 90) + (dy == 0 ? 0 : (2 - dy) * 90));
    if ((getRotation + newRotation + 360) % 360 != 180) setRotation(newRotation);
    if (Greenfoot.getRandomNumber(2000) < 5) getWorld().addObject(new afval2(), getX(), getY());
}
martijn13039 martijn13039

2012/5/5

#
hey danpost there is one problem if I compile cannont find symbool variable getRotation (I have try to change public void but it didn't work) I hope you have one more advice for me
danpost danpost

2012/5/5

#
Sorry, forgot the parenthesis '()' after 'getRotation' in line 10. Should be 'if ((getRotation() + newRotation + 360) ...'
martijn13039 martijn13039

2012/5/5

#
hey danpost there is something really weirt there are no errors at the compiling but the actore don't move I have try to ad move(4); but the still did't move
danpost danpost

2012/5/5

#
What does your student class look like now? (post code)
martijn13039 martijn13039

2012/5/5

#
this is the code I got now for the student class code]import greenfoot.*; public class student extends school { int direction = 0; public void moveAndTurn() { int dx = direction % 2 == 0 ? 1 - direction : 0; int dy = direction % 2 == 1 ? 2 - direction : 0; setLocation(getX() + dx * 4, getY() + dy * 4); if (Greenfoot.getRandomNumber(101) < 15 || atWorldEdge()) direction += (Greenfoot.getRandomNumber(2) * 2 - 1); int newRotation = ((dx == 0 ? 0 : (1 - dx) * 90) + (dy == 0 ? 0 : (2 - dy) * 90)); if ((getRotation() + newRotation + 360) % 360 != 180) setRotation(newRotation); if (Greenfoot.getRandomNumber(2000) < 5) getWorld().addObject(new afval2(), getX(), getY()); } }
martijn13039 martijn13039

2012/5/5

#
import greenfoot.*;    
public class student extends school  
{  

    int direction = 0;  
    public void moveAndTurn()  
    {  
        
        int dx = direction % 2 == 0 ? 1 - direction : 0;  
        int dy = direction % 2 == 1 ? 2 - direction : 0;  
        setLocation(getX() + dx * 4, getY() + dy * 4);  
        if (Greenfoot.getRandomNumber(101) < 15 || atWorldEdge())
            direction += (Greenfoot.getRandomNumber(2) * 2 - 1);  
        int newRotation = ((dx == 0 ? 0 : (1 - dx) * 90) + 
                (dy == 0 ? 0 : (2 - dy) * 90));  
        if ((getRotation() + newRotation + 360) % 360 != 180)
            setRotation(newRotation);  
        if (Greenfoot.getRandomNumber(2000) < 5) getWorld().addObject(new afval2(), getX(), getY());  
    }  
    
    

}  
danpost danpost

2012/5/5

#
Just change line 6 to the following:
public void act()
danpost danpost

2012/5/5

#
Hope that works! Will be departing for about 8 hours. Cheers. :+)
martijn13039 martijn13039

2012/5/5

#
the actor wil walk but it is very weird I don“t know how I can explain maby you can make an actor and see by your self what happen its verry weird
danpost danpost

2012/5/6

#
Made it a little better with
import greenfoot.*; 
   
public class student extends school  
{  
    int direction = 0; 

    public void act()  
    {        
        int dx = direction % 2 == 0 ? 1 - direction : 0;  
        int dy = direction % 2 == 1 ? 2 - direction : 0;  
        setLocation(getX() + dx * 2, getY() + dy * 2);  
        if (Greenfoot.getRandomNumber(101) < 15)
            direction = (direction + Greenfoot.getRandomNumber(2) * 2 + 3) % 4;
        int newRotation = direction * 90;  
        if (newRotation != 180) setRotation(newRotation); else setRotation(0);
        if (Greenfoot.getRandomNumber(2000) < 5) getWorld().addObject(new afval2(), getX(), getY());  
    }
}
martijn13039 martijn13039

2012/5/6

#
hey danpost the code works very nice but I have put if (atworldedge()) in and sometimes if he come at the end of the world he can do very weird, he stay on his place and stay turning but don't go way but thx for the great help the code without if atworldedge()) works great
import greenfoot.*; 

public class student extends school  
{  
    int direction = 0; 

    public void act()  
    {        
        int dx = direction % 2 == 0 ? 1 - direction : 0;  
        int dy = direction % 2 == 1 ? 2 - direction : 0;  
        setLocation(getX() + dx * 2, getY() + dy * 2);  
        if (Greenfoot.getRandomNumber(750) < 15) 
            direction = (direction + Greenfoot.getRandomNumber(2) * 2 + 3) % 4;
        int newRotation = direction * 90;  
        if (newRotation != 180) setRotation(newRotation); else setRotation(0);
        if (Greenfoot.getRandomNumber(2000) < 5) getWorld().addObject(new afval2(), getX(), getY());  

        if (atWorldEdge())

         direction = (direction + Greenfoot.getRandomNumber(2) * 2 + 3) % 4;

    
    }

    
}
danpost danpost

2012/5/7

#
That is because you need to ask 'if at world's edge and facing the edge' (not just 'if at world's edge'). One way to get around it is, if at world's edge: move back to previous location and random change direction. How about this:
import greenfoot.*; 

public class student extends school  
{  
    int direction = 0; 

    public void act()  
    {        
        int dx = direction % 2 == 0 ? 1 - direction : 0;  
        int dy = direction % 2 == 1 ? 2 - direction : 0;  
        setLocation(getX() + dx * 2, getY() + dy * 2);
        boolean edged = atWorldEdge();
        if (edged) setLocation(getX() - dx * 2, getY() - dy * 2);  
        if (edged || Greenfoot.getRandomNumber(750) < 15) 
            direction = (direction + Greenfoot.getRandomNumber(2) * 2 + 3) % 4;
        int newRotation = direction * 90;  
        if (newRotation != 180) setRotation(newRotation); else setRotation(0);
        if (Greenfoot.getRandomNumber(2000) < 5) getWorld().addObject(new afval2(), getX(), getY());  
    }
}
We do not have to ask 'if facing' because we rectify the situation at the time the edge is encountered.
There are more replies on the next page.
1
2
3