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

2014/4/30

mouseClicked(object) from world

kasperk22 kasperk22

2014/4/30

#
Hey guys.. In my world class, i want to execute some methods if the mouse is clicked on an actor class. But the if ((Greenfoot.mouseClicked(Object)) seems to only work if i sinsert "this" instead of Object. If i insert an Actor class in there, it does not work. Here is my code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ZombieWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ZombieWorld extends World
{
    int ZombieDelay = 0;
    PointCounter pointCounter = new PointCounter();
    LifeCounter lifeCounter = new LifeCounter();
    Skydemand skydemand = new Skydemand();
    Køremand køremand = new Køremand(pointCounter);
    Hjerne hjerne = new Hjerne ();
    Zombie zombie = new Zombie();
    SinglePlayer singlePlayer = new SinglePlayer();
    TwoPlayer twoPlayer = new TwoPlayer();
    GreenfootSound shooting =new GreenfootSound("Shooting.wav");
    boolean Started = false;
    /**
     * Constructor for objects of class ZombieWorld.
     */
    public ZombieWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1100, 600, 1); 
        addObject (new Instructions(), getWidth()/2, 100);
        addObject (new SinglePlayer(), 200, getHeight()/2);
        addObject (new TwoPlayer(), 900, getHeight()/2);
        //PrepareSingle();
        //PrepareTwo();
        setPaintOrder(Skydemand.class,Hjerne.class,Køremand.class,Bullet.class,Zombie.class,Splat.class);
    }
    
    public void act()
    {
      ZombieAdd();
      StartUp();
    }
    
    private void StartUp()
    { 
        if (Started==false)
        {
           if ((Greenfoot.mouseClicked(singlePlayer)==true))
           {
                PrepareSingle();
                removeObject (singlePlayer);
                removeObject(twoPlayer);
                Started=true;
           }
           if ((Greenfoot.mouseClicked(twoPlayer)==true))
           {
                PrepareTwo();
                removeObject (singlePlayer);
                removeObject(twoPlayer);
                Started=true;
           }
        }
    }
    
    /**
     * Prepare the world for the start of the program(if single player is selected). That is: create the initial
     * objects and add them to the world.
     */
    private void PrepareSingle()
    {
        addObject(hjerne, getWidth()/2, getHeight()/2);
        addObject(skydemand, getWidth()/2+65, getHeight()/2);
        addObject(pointCounter,50,25);
        addObject(lifeCounter, getWidth()/2, 40);
    }
    
    /**
     * Prepare the world for the start of the program(if two player is selected. That is: create the initial
     * objects and add them to the world.
     */
    private void PrepareTwo()
    {
        addObject(hjerne, getWidth()/2, getHeight()/2);
        addObject(skydemand, getWidth()/2+65, getHeight()/2);
        addObject(køremand, getWidth()/2, getHeight()/2-80);
        addObject(pointCounter,50,25);
        addObject(lifeCounter, getWidth()/2, 40);
    }
    
    private void ZombieAdd()
    {
    if (Started==true)
    {
        ZombieDelay++;
        
        if (ZombieDelay>200)
        {
            RandomPlacement();
            ZombieDelay=0;
        }
    }
    }
    
     /**
     * generates the "Zombies" in random places at the edge of the world
     */
    public void RandomPlacement()    
    {      
        int chooser = Greenfoot.getRandomNumber(4);
        if (chooser==0)
        {
        int x = Greenfoot.getRandomNumber(getWidth());    
        int y = 0;
        addObject (new Zombie(),x,y);  
    }
           if (chooser==1)
        {
        int x = Greenfoot.getRandomNumber(getWidth());    
        int y = getHeight();
        addObject (new Zombie(),x,y);  
    }
           if (chooser==2)
        {
        int x = 0;    
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject (new Zombie(),x,y);  
    }
           if (chooser==3)
        {
        int x = getWidth();    
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject (new Zombie(),x,y);  
    } 
    }  
}
davmac davmac

2014/4/30

#
If i insert an Actor class in there, it does not work.
The mouseClicked method doesn't take a class as a parameter, it takes an object. That's the problem right there. You are creating two objects on lines 18 and 19:
    SinglePlayer singlePlayer = new SinglePlayer();  
    TwoPlayer twoPlayer = new TwoPlayer();  
But you never add these objects in to the world. Instead, you add two new instances (lines 30-31):
        addObject (new SinglePlayer(), 200, getHeight()/2);  
        addObject (new TwoPlayer(), 900, getHeight()/2);  
So, the singlePlayer and twoPlayer objects never get clicked on, because they are never added to the world. You need to change lines 30 and 31 to:
        addObject (singlePlayer, 200, getHeight()/2);  
        addObject (twoPlayer, 900, getHeight()/2);  
kasperk22 kasperk22

2014/4/30

#
ooooh, i see... thanks alot, i actually never ment to write "new Singleplayer"... that was just a dumb fail my me :-)
You need to login to post a reply.