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

2013/2/25

Can't call method from world class in actor class

ycart ycart

2013/2/25

#
So let me put in some snippets of code Here is the method from my world class
    public void startMission()
    {
        addRandomFood(); 
        addRandomPeople();
        addRandomTreasures();
        noFuel();
    }    
    public void addRandomFood()
    {
       if(Greenfoot.getRandomNumber(300)<1)
       {
          addObject(new Food(), getWidth (), Greenfoot.getRandomNumber(getHeight()));
       }
    }
    public void addRandomTreasures()
    {
       if(Greenfoot.getRandomNumber(600)<1)
       {
          addObject(new Treasures(), getWidth (), Greenfoot.getRandomNumber(getHeight()));
       }        
    }
    public void addRandomPeople()
    {
       if(Greenfoot.getRandomNumber(100)<1)
       {
          addObject(new Person(), Greenfoot.getRandomNumber(getWidth()), 0 );
       }
    }
    public void noFuel()
    {
        if (fuelCounter.getValue()==-1)
        {
            gameOver();
            Greenfoot.stop();
        }
    }
and here it is when I call it in my actor class
    public void act() 
    {
        starting2();
    }    
    private void starting1()
    {
        if (Greenfoot.mouseClicked(this))
        {            
           CarWorld w = (CarWorld) getWorld();
           w.startMission();
           getWorld().removeObject(this);
        }
    }
The idea behind the method is: 1) game is running w/ a start actor image that pops up 2) click the start image for it to go away --> then objects from startMission() can start appearing randomly There is no error message popping up when I run the game, but the startMission() method doesn't work. The removeObject works fine though. Any ideas?
ycart ycart

2013/2/25

#
Opps the last bit of code should have been
    public void act() 
    {
        starting1();
    }    
    private void starting1()
    {
        if (Greenfoot.mouseClicked(this))
        {            
           CarWorld w = (CarWorld) getWorld();
           w.startMission();
           getWorld().removeObject(this);
        }
    }
Sorry about that!
danpost danpost

2013/2/25

#
I am quite sure that the 'startMission' method IS working. It is only called once after the mouse click. Each method it calls will run one time. Therefore, you will have a 1 in 300 chance of one Food object appearing; a 1 in 600 chance of one Treasures object appearing; and, a 1 in 100 chance of one Person object appearing. You would be very lucky to have anything appear, which is why it seems like it is not working. I think what you wanted is that these object randomly, but continuously, appear throughout the game. In order to accomplish this, you will need a boolean field in the world class to track whether the method should execute or not (or, whether the mission is started yet or not). The 'startMission' method should change the state of the boolean field from false to true. The 'act' method in the world class should call the other methods only if the boolean is set to true.
You need to login to post a reply.