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

2012/9/20

Invoking an actor's meathod from the world?

Stormtrooper299 Stormtrooper299

2012/9/20

#
I know how to invoke one actor's meathod from another actor. but I need the world to invoke the meathod. I tried using the same technique and it did not work. Any Help? Actor I want to invoke is "Purchase" Meathod I want to invoke is "appear" World = MyWorld.
Gevater_Tod4711 Gevater_Tod4711

2012/9/20

#
this should work the same way like invoking the method from another actor. Could you give us the code for this? That would help.
Stormtrooper299 Stormtrooper299

2012/9/21

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;

/**
 * 
 * 
 * @author Dallin Wrathall
 */
public class MyWorld extends World
{
public int level;
private Counter theCounter;
public int pause = 10000;
public boolean timeOut;
private Barrier theBarrier;
private Purchase thePurchase;


    public MyWorld() 
    {
        super(600,400, 1);
        GreenfootImage background = getBackground();
        background.setColor(Color.WHITE);
        background.fill();
        theCounter = new Counter();
        addObject(new Survivor(), 300, 200);
        theBarrier = new Barrier();
        thePurchase = new Purchase();
     
        addObject(theCounter, 300, 20);
        
        
       
        }

  
    public void act()
    {
        if (Greenfoot.getRandomNumber(1000) < 3) 
            addObject(new Upgrade(), Greenfoot.getRandomNumber(500), Greenfoot.getRandomNumber(400));
        
        // level 0 is actually level 1
        if (level == 0)
        {
             timeOut = false;
        if (Greenfoot.getRandomNumber(1000) < 5) {
            addObject(new Zombie(),0,0) ;
        }
        if (Greenfoot.getRandomNumber(1000) < 5) {
            addObject(new Zombie(), 600, 0);
        }
        if (Greenfoot.getRandomNumber(1000) < 5) {
            addObject(new Zombie(), 600,400);
        }
        if (Greenfoot.getRandomNumber(1000) < 5) {
            addObject(new Zombie(), 0, 400);
        }
    }
        
        //level 1 is actually level 2
        if (level == 1)
        {
            
            if(pause>0){
            pause--;  
            timeOut = true;
        }
            
            if(pause == 0)  
            {  
                timeOut = false; 
            
            
          
        
        if (Greenfoot.getRandomNumber(300) < 5) {
            addObject(new Zombie(),0,0) ;
        }
        if (Greenfoot.getRandomNumber(300) < 5) {
            addObject(new Zombie(), 600, 0);
        }
        if (Greenfoot.getRandomNumber(300) < 5) {
            addObject(new Zombie(), 600,400);
        }
        if (Greenfoot.getRandomNumber(300) < 5) {
            addObject(new Zombie(), 0, 400);
        }
        }
        }
        
    //level 2 is actually level 3
    if (level == 2)
        {
            
            if(pause>0){
            pause--;  
            timeOut = true;
        }
            if(pause == 0)  
            {  
                 timeOut = false;
        if (Greenfoot.getRandomNumber(100) < 5) {
            addObject(new Zombie(),0,0) ;
        }
        if (Greenfoot.getRandomNumber(100) < 5) {
            addObject(new Zombie(), 600, 0);
        }
        if (Greenfoot.getRandomNumber(100) < 5) {
            addObject(new Zombie(), 600,400);
        }
        if (Greenfoot.getRandomNumber(100) < 5) {
            addObject(new Zombie(), 0, 400);
        }
    }

        
    
}
//level 3 is actually level 4
if (level == 3)
        {
            
            if(pause>0){
            pause--; 
            timeOut = true;
        }
            if(pause == 0)  
            {  
                 timeOut = false;
        if (Greenfoot.getRandomNumber(10) < 5) {
            addObject(new Zombie(),0,0) ;
        }
        if (Greenfoot.getRandomNumber(10) < 5) {
            addObject(new Zombie(), 600, 0);
        }
        if (Greenfoot.getRandomNumber(10) < 5) {
            addObject(new Zombie(), 600,400);
        }
        if (Greenfoot.getRandomNumber(10) < 5) {
            addObject(new Zombie(), 0, 400);
        }
    
}

    }
    //level 4 is actually level 5
if (level == 4)
        {

            if(pause>0){
            pause--;
            timeOut = true;
        }
            if(pause == 0)  
            {  
                 timeOut = false;
        if (Greenfoot.getRandomNumber(1) < 5) {
            addObject(new Zombie(),0,0) ;
        }
        if (Greenfoot.getRandomNumber(1) < 5) {
            addObject(new Zombie(), 600, 0);
        }
        if (Greenfoot.getRandomNumber(1) < 5) {
            addObject(new Zombie(), 600,400);
        }
        if (Greenfoot.getRandomNumber(1) < 5) {
            addObject(new Zombie(), 0, 400);
        }
}
}
}

    
     public Counter getCounter()
    {
        return theCounter;
    }
    public Barrier getBarrier()
    {
        return theBarrier;
    }
    public Purchase getPurchase()
    {
        return thePurchase;
    }
    public void purchaser()
    {
        
        MyWorld MyWorldWorld = (MyWorld) getWorld();
        Purchase purchase = MyWorldWorld.getPurchase();
        
        thepurchase.Appear();   

    }


  
    public void levelSwitch(int amount)
    {
        level += amount;
        
    }
  
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Purchase here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Purchase extends Actor
{
    /**
     * Act - do whatever the Purchase wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        Purchase();
    }  
    public void Appear()
    {
        getWorld().addObject(new Purchase(), 584, 207);
    }
    public void Disappear()
    {
        getWorld().removeObject(this);
    }
    public void Purchase()
    {
        Survivor a = (Survivor) getOneIntersectingObject(Survivor.class);
        if(a != null) {
            MyWorld MyWorldWorld = (MyWorld) getWorld();
        Barrier barrier = MyWorldWorld.getBarrier();
        barrier.bumpCount(1);
    }
    }
}

Stormtrooper299 Stormtrooper299

2012/9/21

#
line 189 - 192 is where im having problems MyWorld MyWorldWorld = (MyWorld) getWorld(); Purchase purchase = MyWorldWorld.getPurchase(); thepurchase.Appear(); i've also tried Purchase purchase = getPurchase(); thepurchase.Appear();
nccb nccb

2012/9/21

#
The second version you mention:
Purchase purchase = getPurchase();           
thepurchase.Appear();
Should work fine. I believe the problem is that although you create thePurchase in your world constructor (line 28 of MyWorld), you're not adding it to the world. You probably want a line like:
addObject(thePurchase, 100, 100);
On about line 29 or so of the MyWorld cocde.
Stormtrooper299 Stormtrooper299

2012/9/24

#
Thank you nccb, it worked :)
You need to login to post a reply.