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

2013/12/19

Variables between world and actor

groep5 groep5

2013/12/19

#
We have a problem. We are making a basic version of Snake, and we have stumbled across a problem. At the start, you can choose between easy and hard. If you choose easy the variable level in the world (Achtergrond) becomes 1. We want to transfer the variable level to the actor Hoofd.
import greenfoot.*;
import java.util.List;
public class Hoofd extends Actor
{
    //Variabelen
    Lichaam next;
    int x;
    int y;
    int rotation;  


    public  void act() 
    {
        //Hij kijkt of de slang iets aanraakt.
    x =getX();
    y =getY();
    rotation =getRotation();
    processKeys();    
    
  if(getIntersectingObjects(Lichaam.class).size() > 0)
    {
      ((Achtergrond)(getWorld())).GameOver();
      return;
      //Hij gaat weer terug naar het vorige scherm.
    }    

    if (getRotation()==0 )
    {
        setLocation(getX(),getY()-1);
    }
    else if (getRotation()==90 &)
    {
       setLocation(getX()+1, getY());    
    }
     else if (getRotation()==180 && level==1)
    {
       setLocation(getX(), getY()+1);    
    }
     else if (getRotation()==270 && level==1)
    {
       setLocation(getX()-1, getY());    
    }
    
    if (y==0 && getRotation()==0 && level==1)
       {
         setLocation(getX(),52);
           
       }
    if (y==51 && getRotation()==180 && level==1)
       {
           setLocation(getX(),-1);
           
       }   
    if (x==0 && getRotation()==270 && level==1)
       {
         setLocation(52,getY());
           
       }
    if (x==51 && getRotation()==90 && level==1)
       {
           setLocation(0,getY());
           
       }   
    
    if (intersects((((Achtergrond)(getWorld())).voedsela)))
    {
        (((Achtergrond)(getWorld())).voedsela).setLocation(Greenfoot.getRandomNumber(50)+1,Greenfoot.getRandomNumber(50)+1);
        addLichaam();
        ((Achtergrond)(getWorld())).score.addPoint();
    }
        if (intersects((((Achtergrond)(getWorld())).voedselb)))
    {
        (((Achtergrond)(getWorld())).voedselb).setLocation(Greenfoot.getRandomNumber(50)+1,Greenfoot.getRandomNumber(50)+1);
        addLichaam();
        ((Achtergrond)(getWorld())).score.addPoint();
    }
            if (intersects((((Achtergrond)(getWorld())).voedselp)))
    {
        (((Achtergrond)(getWorld())).voedselp).setLocation(Greenfoot.getRandomNumber(50)+1,Greenfoot.getRandomNumber(50)+1);
        addLichaam();
        ((Achtergrond)(getWorld())).score.addPoint();
    }
   
    if (next!=null)
    {
    next.move(x,y,rotation);
}
}
public void addLichaam()
{
    if (next==null)
    {
        next=new Lichaam();
        if (getRotation()==0)
        getWorld().addObject(next, getX(), getY()+1);      
        else if (getRotation()==90)
        getWorld().addObject(next, getX()-1, getY()); 
        else if (getRotation()==180)
        getWorld().addObject(next, getX(), getY()-1);
        else if (getRotation()==270)
        getWorld().addObject(next, getX()+1, getY());
}
else if (next!=null)
{
    next.addLichaam();
}
    } 
     private void processKeys() 
  {
    if(Greenfoot.isKeyDown("down") && this.getRotation() != 180 && this.getRotation() != 0)
      this.setRotation(180);
    else if(Greenfoot.isKeyDown("up") && this.getRotation() != 0 && this.getRotation() != 180)
      this.setRotation(0);
    else if(Greenfoot.isKeyDown("left") && this.getRotation() != 270 && this.getRotation() != 90)
      this.setRotation(270);
    else if(Greenfoot.isKeyDown("right") && this.getRotation() != 90 && this.getRotation() != 270)
      this.setRotation(90);
    else{}
  }
}



import greenfoot.*;
import java.util.List; //Om alle objecten weg te halen
public class Achtergrond extends World
{
    //variabelen staan hier
    public Hoofd hoofda;
    public Voedsel voedsela;
    public Voedsel2 voedselb;
    public Voedsel3 voedselp;
    public Score score;
    public Terugknop terugknopa;
    public Terugknop2 terugknopb;
    public Startknop startknopa;
    public Easyknop easyknopa;
    public Hardknop hardknopa;
    int x;
    int level;
  

    public Achtergrond()
    {    
        
        super(52, 52, 10);
        setBackground("Achtergrond4.png");
        startknopa=new Startknop();
        addObject(startknopa, 26, 40);
    }
    
    public void Easy()
    {
        level=1;
         removeObject(hardknopa);
         removeObject(easyknopa);
      hoofda=new Hoofd();
        addObject(hoofda,26, 26);
        voedsela=new Voedsel();
        voedselb=new Voedsel2();
        voedselp=new Voedsel3();
        x = Greenfoot.getRandomNumber(3);
        if (x==0)
        {
          addObject(voedsela, Greenfoot.getRandomNumber(50)+1, Greenfoot.getRandomNumber(50)+1);  
        }
        if (x==1)
        {
          addObject(voedselb, Greenfoot.getRandomNumber(50)+1, Greenfoot.getRandomNumber(50)+1);  
        }
        if (x==2)
        {
          addObject(voedselp, Greenfoot.getRandomNumber(50)+1, Greenfoot.getRandomNumber(50)+1);  
        }
        Greenfoot.setSpeed(35);
        score = new Score();
        addObject(score, 5, 50);
        Greenfoot.setSpeed(35);
        setBackground("sand2.jpg");  
    }
    
    public void Hard()
    {
        level=2;
         removeObject(hardknopa);
          removeObject(easyknopa);
        hoofda=new Hoofd();
        addObject(hoofda,26, 26);
        voedsela=new Voedsel();
        voedselb=new Voedsel2();
        voedselp=new Voedsel3();
        x = Greenfoot.getRandomNumber(3);
        if (x==0)
        {
          addObject(voedsela, Greenfoot.getRandomNumber(50)+1, Greenfoot.getRandomNumber(50)+1);  
        }
        if (x==1)
        {
          addObject(voedselb, Greenfoot.getRandomNumber(50)+1, Greenfoot.getRandomNumber(50)+1);  
        }
        if (x==2)
        {
          addObject(voedselp, Greenfoot.getRandomNumber(50)+1, Greenfoot.getRandomNumber(50)+1);  
        }
        Greenfoot.setSpeed(35);
        score = new Score();
        addObject(score, 5, 50);
        Greenfoot.setSpeed(35);
        setBackground("sand2.jpg");
    }
    
    public void Start()
    {
        removeObject(startknopa);
        easyknopa= new Easyknop();
        addObject(easyknopa, 26, 30);
        hardknopa= new Hardknop();
        addObject(hardknopa, 26, 40);
    }

    public void GameOver()
    {
        //2 regels om alle objecten te verwijderen als je dood gaat
        List objects = getObjects(null);
        removeObjects(objects);  
        //Greenfoot.stop();
        setBackground("Achtergrond2.png");
        terugknopa=new Terugknop();
        addObject(terugknopa, 26, 40);
       
        
    }
    public void Return()
    {
        setBackground("Achtergrond3.png");
        terugknopb= new Terugknop2();
        addObject(terugknopb, 26,40);
        Greenfoot.delay(2);
        List objects = getObjects(null);
        removeObjects(objects);
        setBackground("sand2.jpg");
        hoofda=new Hoofd();
        addObject(hoofda,26, 26);
        voedsela=new Voedsel();
        //Voedsel op willekeurige plaats
        addObject(voedsela, Greenfoot.getRandomNumber(50)+1, Greenfoot.getRandomNumber(50)+1);
        Greenfoot.setSpeed(35);
        score = new Score();
        addObject(score, 5, 50);
        Greenfoot.setSpeed(35);
        Greenfoot.delay(10);
    }    
}
    
danpost danpost

2013/12/19

#
Once you add an instance field to hold the level ('private int level;') in the Hoofd class, you can easily accomplish this by adding a 'public void setLevel' method in the Hoofd class:
public void setLevel(int level)
{
    this.level = level;
}
and calling this method after creating the Hoofd object in both the 'Easy' and 'Hard' methods of the Achtergrond class using:
hoofda.setLevel(level);
groep5 groep5

2013/12/20

#
Thank you, it works!
You need to login to post a reply.