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

2012/5/4

#
he everyone, I have make two codes one code for drive an actore and one code to let an actore walk automatic, but how can I make the code so that the actor walk autmatic walk like the actor who I can drive code let an actor walk automatic
import greenfoot.*;  

public class student extends school
{
   
    public void act() 

    {
        
        move(4);
        if ( Greenfoot.getRandomNumber(101) < 15)
        {
            turn( Greenfoot.getRandomNumber(40)-20 );
        }

               if (atWorldEdge())
        {

            turn(15);

        }  
       
        if (Greenfoot.getRandomNumber(2000) < 5)
        {
            getWorld().addObject(new afval2(), getX(), getY());
        }   

    
    }

}    

code for drive the actor

public void processKeys()
    {

        if(Greenfoot.isKeyDown ("left")) {
            setLocation(getX() - 2, getY());
        } 
        else {
            setLocation(getX(),getY());
        }

        if(Greenfoot.isKeyDown ("down")) {
            setLocation(getX(),getY() + 2);
        }
        else {
            setLocation(getX(),getY());
        }

        if(Greenfoot.isKeyDown ("right")) {
            setLocation(getX() + 2,getY());
        }
        else {
            setLocation(getX(),getY());
        }

        if(Greenfoot.isKeyDown ("up")) {
            setLocation(getX(),getY() - 2);
        }
        else {
            setLocation(getX(),getY());
        }
    }
I hope somebody can help me
martijn13039 martijn13039

2012/5/4

#
I mean the actor move randomly but than like the second code
ttamasu ttamasu

2012/5/5

#
Your post is confusing but I understand correctly you want to be able to use the same processKey method for the driver and walker. Just make a SuperClass and have driver and walker be a subclass of this class:
import greenfoot.*;  
public class SomeSuperClass extends Actor
{
    public void processKeys()
    {
        if (Greenfoot.isKeyDown("left")){
            setRotation(180);
            move(2);
        }
        else if(Greenfoot.isKeyDown("down")){
            setRotation(90);
            move(2);
        }
        else if (Greenfoot.isKeyDown("right")){
            setRotation(0);
            move(2);
        }
        else if (Greenfoot.isKeyDown("up")){
            setRotation(270);
            move(2);
        }
    }       
}
Then you just make two subclasses Driver and Walker of this superClass and put processKeys in the act method of both. The only difference is that you assign different default images for these classes
danpost danpost

2012/5/5

#
@ttamasu, I think what martijn13039 wants is that the same actor, if not being controlled by keys, move randomly. @martijn13039, change your processKeys() method to the following
public boolean processKeys()
{
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("up") dx--;
    if (Greenfoot.isKeyDown("left") dy--;
    if (Greenfoot.isKeyDown("down") dx++;
    if (Greenfoot.isKeyDown("right") dy++;
    setLocation(getX() + dx * 2, getY() + dy * 2);
    return (dx !=  0 || dy != 0);
}
Then, in the student class act() method, at line 9, insert the following line
if (!processKeys()) {
and a '}' at line 22 (indent lines 10 through 21).
martijn13039 martijn13039

2012/5/5

#
hey thx for the reacties on my confusing qustion and danpost you are right what I mean and I have try you code but there stay cannont fint symbool method processKeys on the first code and I think I have make a stupid question but I can't find him
 public void act() 

    {
        if (!processKeys()) {  and a; }

        
        if ( Greenfoot.getRandomNumber(101) < 15)
        {
            turn( Greenfoot.getRandomNumber(40)-20 );
        }

       
        if (atWorldEdge())
        {

            turn(15);

        }  
        
        move(4);
        

        if (Greenfoot.getRandomNumber(2000) < 5)
        {
            getWorld().addObject(new afval2(), getX(), getY());
        }   

    
    }
public boolean processKeys() 
    { 
        int dx = 0, dy = 0; 
        if (Greenfoot.isKeyDown("up"))dy--; 
        if (Greenfoot.isKeyDown("left")) dx--; 
        if (Greenfoot.isKeyDown("down")) dy++; 
        if (Greenfoot.isKeyDown("right")) dx++; 
        setLocation(getX() + dx * 2, getY() + dy * 2); 
        return (dx != 0 || dy != 0); 
    } 
danpost danpost

2012/5/5

#
Your act() method should be:
public void act() 
{
    if (!processKeys())
    {    
        move(4);
        if ( Greenfoot.getRandomNumber(101) < 15) turn( Greenfoot.getRandomNumber(40)-20 );
        if (atWorldEdge()) turn(15);
    }   
    if (Greenfoot.getRandomNumber(2000) < 5) getWorld().addObject(new afval2(), getX(), getY());
}
Also, the act() method and the processKeys() method should both be in the same class (which I assume they are).
martijn13039 martijn13039

2012/5/5

#
now is the problem the act student don't walk automatic I can drive them with an other acter and I want the studen walk automatica but than like the actor who i can drive so they will not be able to walk on their heads and there is still the same error cannont fint symbool method processKeys I hope you understand what I mean i got this now
public class concierge extends school
{
 public boolean processKeys()  
   {   
     int dx = 0, dy = 0;   
        if (Greenfoot.isKeyDown("up"))dy--;   
       if (Greenfoot.isKeyDown("left")) dx--;   
      if (Greenfoot.isKeyDown("down")) dy++;   
      if (Greenfoot.isKeyDown("right")) dx++;   
       setLocation(getX() + dx * 2, getY() + dy * 2);   
        return (dx != 0 || dy != 0);   
    } 
}
import greenfoot.*;  

public class student extends school
{
   

    public void act() 

    {  
        if (!processKeys())  
        {      
            move(4); 
            if ( Greenfoot.getRandomNumber(101) < 15) turn( Greenfoot.getRandomNumber(40)-20 ); 
            if (atWorldEdge()) turn(15);  
        }     
        if (Greenfoot.getRandomNumber(2000) < 5) getWorld().addObject(new afval2(), getX(), getY());  
    }  

    
     
}    
danpost danpost

2012/5/5

#
You need to move the 'processKeys()' method to the student class.
martijn13039 martijn13039

2012/5/5

#
danpost I´ve done what you say but there stil is one other error there stay a red squir on if (!processKeys()) void typ not allowed her
import greenfoot.*; 

public class student extends school 
{ 
    public void processKeys()
    { 
        if (!processKeys()) 
        { 
            move(4); 
            if ( Greenfoot.getRandomNumber(101) < 15) turn( Greenfoot.getRandomNumber(40)-20 ); 
            if (atWorldEdge()) turn(15); 
        } 
        if (Greenfoot.getRandomNumber(2000) < 5) getWorld().addObject(new afval2(), getX(), getY()); 
    } 

    
} 

danpost danpost

2012/5/5

#
You must not realize that you can have more than one method in a class, or something. Why did you change the name of your act() method? did you mis-understand me? I think there might be somewhat of a language barrier here! This should be the code in the student class:
import greenfoot.*; 

public class student extends school 
{ 
    public void act()
    { 
        if (!processKeys()) 
        { 
            move(4); 
            if ( Greenfoot.getRandomNumber(101) < 15) turn( Greenfoot.getRandomNumber(40)-20 ); 
            if (atWorldEdge()) turn(15); 
        } 
        if (Greenfoot.getRandomNumber(2000) < 5) getWorld().addObject(new afval2(), getX(), getY()); 
    } 

    public boolean processKeys()
    {
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("up") dx--;
        if (Greenfoot.isKeyDown("left") dy--;
        if (Greenfoot.isKeyDown("down") dx++;
        if (Greenfoot.isKeyDown("right") dy++;
        setLocation(getX() + dx * 2, getY() + dy * 2);
        return (dx !=  0 || dy != 0);
    }
}
martijn13039 martijn13039

2012/5/5

#
danpost thx for your respond but my problem still is there because the stundent don´t walk automatic i can drive him if i put on the computer but he need to drive automatic but than like the code and not that he make rotaions
danpost danpost

2012/5/5

#
Change 'processKeys() to:
public boolean processKeys()
{
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("up")) dy--;
    if (Greenfoot.isKeyDown("left")) dx--;
    if (Greenfoot.isKeyDown("down")) dy++;
    if (Greenfoot.isKeyDown("right")) dx++;
    setLocation(getX() + dx * 2, getY() + dy * 2);
    if (dx != 0) setRotation(0);
    if (dy > 0) setRotation(90);
    if (dy < 0) setRotation(270);
    return (dx !=  0 || dy != 0);
}
BTW, I tested the code.
martijn13039 martijn13039

2012/5/5

#
the problem stay because I drive the actor if i pus iskeydown up he go up but I want that the actor do that automatic
danpost danpost

2012/5/5

#
I guess I do not understand exactly what you are wanting to do. Sorry :+(
martijn13039 martijn13039

2012/5/5

#
oow, I hope I can explain it better - In the code 2 I can move the conciere with my keyboord en he can only go left, right, up or down (so he walk normal - in code 1 can move een studen automatic but he kan walk very weird like on his had and I want he walk like code 1 go left, right, up or down but than automatic
There are more replies on the next page.
1
2
3