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

2019/11/27

Transfering Variables from Actor to World

PhoenyxProgramming PhoenyxProgramming

2019/11/27

#
I'm new to Greenfoot (Programming in general aswell) and trying to make a Simple Game where an Ant eats Food and that way its Score gets higher. I'm now trying to make the chance of Food Spawning randomly, which I have coded in the MyWorld class, dependant on the 'Score' variable from the Ant class (or set global Variables in the World class which can be changed by Actor classes). Below is the code I have in the MyWorld class, and I will gladly send the code of the Ant class if that is needed.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class MyWorld extends World
{
   int Chance = 1;
    public MyWorld()
   {     
        super(1000, 800, 1);
        prepare();
   }
    public void act()
   {
     SpawnStuff();
     if(Greenfoot.getRandomNumber(250) == 1)
     {
       Chance = (Chance + 1); 
     }
   }
   private void prepare()
   {
      addObject(new AntPlayer(), 100, 100);
      addObject(new FoodGood(), 300, 300);
   }
   public void SpawnStuff()
   {
       if(Greenfoot.getRandomNumber(150) == Chance)
       {
           int x = Greenfoot.getRandomNumber(990);
           int y = Greenfoot.getRandomNumber(790);
           
           addObject(new FoodGood(), x, y);
        }
   }
}
PhoenyxProgramming PhoenyxProgramming

2019/11/27

#
Here is the Code from the Actor class 'AntPlayer':
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class AntPlayer extends Actor
{
    
    public void act() 
    {
        int Score = 0;
        move(4);
          if(Greenfoot.isKeyDown("Left"))
       {
        setRotation(180);
       }
       if(Greenfoot.isKeyDown("Right"))
       {
        setRotation(0);
       }
       if(Greenfoot.isKeyDown("Down"))
       {
        setRotation(90);
       }
       if(Greenfoot.isKeyDown("Up"))
       {
        setRotation(-90);
       }
       
       if(!getIntersectingObjects(FoodGood.class).isEmpty()) //Überprüft, ob die Ameise eine Erdbeere berührt
        {
         removeTouching(FoodGood.class);
         Score = (Score + 1);
         
        }
    }    
 }
danpost danpost

2019/11/28

#
PhoenyxProgramming wrote...
I'm now trying to make the chance of Food Spawning randomly, which I have coded in the MyWorld class, dependant on the 'Score' variable from the Ant class (or set global Variables in the World class which can be changed by Actor classes). << Code Omitted >>
An easy way is just to keep a reference to the AntPlayer object in your MyWorld world:
public class MyWorld extends World
{
    AntPlayer antPlayer = new AntPlayer();
    
    // ...

    public void prepare()
    {
        addObject(antPlayer, 100, 100);
        addObject(new FoodGood, 300, 300);
    }
    // ...
}
Then you can use antPlayer.Score in your MyWorld class.
You need to login to post a reply.