Hi, I am using Decorator pattern for this game, my base class is WhiteEgg which is also an Actor, Silver and Golden are children of WhiteEgg. In Playing world, I wanted the eggTest object to be randomly spawned as White, Silver, or Golden egg once at a time. But now it can only be chosen at the beginning and stuck in that type of egg, how can I change the object in run time? If I place the random code from the Constructor inside the act() method of the Playing world, all 3 types of eggs will be dropped at the same time, which is not what I want. Can you please help me? Thank you
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Playing here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Playing extends World
{
private Rabbit rabbit;
private Chicken chicken1;
private Chicken chicken2;
private Chicken chicken3;
private Chicken chicken4;
private BrokenEgg brokenEgg;
private BrokenEgg brokenEgg2;
private BrokenEgg brokenEgg3;
private WhiteEgg eggTest;
/**
* Constructor for objects of class Playing.
*
*/
public Playing()
{
super(1280, 720, 1);
init();
}
public void init(){
Greenfoot.setSpeed(100);
chicken1 = new Chicken();
chicken2 = new Chicken();
chicken3 = new Chicken();
chicken4 = new Chicken();
rabbit = new Rabbit();
brokenEgg = new BrokenEgg();
brokenEgg2 = new BrokenEgg();
brokenEgg3 = new BrokenEgg();
int rand = (int)(Math.random() * 3) + 1;
if(rand == 1){
eggTest = new WhiteEgg();
}
else if(rand == 2){
eggTest = new GoldenEggDecorator();
}
else{
eggTest = new SilverEggDecorator();
}
addObject(chicken1, 100, 120);
addObject(chicken2, 340, 120);
addObject(chicken3, 580, 120);
addObject(chicken4, 820, 120);
addObject(brokenEgg, 1020, 437);
addObject(brokenEgg2, 1115, 437);
addObject(brokenEgg3, 1210, 437);
addObject(rabbit, 640, 610);
}
public void act()
{
int random = (int)(Math.random() * 4) + 1;
int x = 0;
if(random == 1){
addObject(eggTest, 100, 150);
}
else if (random == 2){
addObject(eggTest, 340, 150);
}
else if (random == 3){
addObject(eggTest, 580, 150);
}
else{
addObject(eggTest, 820, 150);
}
}
}
