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

2018/12/6

Can't click an object created through an array

s-cooper s-cooper

2018/12/6

#
so i have an actor array in the MyWorld class and i used a for loop to add actors into the world. now, in a totally separate actor class, i'm trying to have it where when i click on one of these objects, a prompt shows up asking how many objects the player wants. I've tried so many different methods of defining the object to be clicked, half of which compile right, yet absolutely nothing happens when i click on them. Am i missing some important step or am i just going about this all wrong? This is the code from MyWorld where the array is
public class MyWorld extends greenfoot.World
{
    public static Actor[] items;
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 10x8 cells with a cell size of 60x60 pixels.
        super(10, 8, 60);
        //actor array
        Actor[] items = new Actor[10];
        items[0] = new burger();
        items[1] = new fries();
        items[2] = new pizza();
        //price array
        double[]prices= {3.69,1.55,2.99};     
        //for loop for world display
        for(int i=0; i<3; i++) {  
            showText("$"+prices[i], 1,(i+1));
            addObject(items[i], 0,(i+1));
        }
        
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        pay pay = new pay();
        addObject(pay,1,6);
    }
}
and this is whats in the shopping cart class, where everything is being put together to EVENTUALLY get a total cost, but if i cant get clicks to work thats gonna be far from possible
public class shoppingCart extends Actor
{
    int quantity = 0;
    double totalPrice = 0;
    Actor burger = MyWorld.items[0];
    Actor fries = MyWorld.items[1];
    Actor pizza = MyWorld.items[2];
    /**
     * Act - do whatever the shoppingCart wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Greenfoot.getMouseInfo();
        if(Greenfoot.mouseClicked(burger)) {
            quantity =Integer.parseInt(Greenfoot.ask("Enter quantity:"));
        }

    }    
}
danpost danpost

2018/12/6

#
s-cooper wrote...
so i have an actor array in the MyWorld class and i used a for loop to add actors into the world. now, in a totally separate actor class, i'm trying to have it where when i click on one of these objects, a prompt shows up asking how many objects the player wants. I've tried so many different methods of defining the object to be clicked, half of which compile right, yet absolutely nothing happens when i click on them. Am i missing some important step or am i just going about this all wrong? << Codes Omitted >>
The act method of the shoppingCart class will not execute unless you add a shoppingCart object into the world. However,, if there is no need for an Actor object there, you could just make use of the World class act method (add one into your MyWorld class and check for clicks right there).
s-cooper s-cooper

2018/12/7

#
Oh my God, of course it's the simplest thing that's the solution, thank you so much! It's working now, thanks a bunch!
You need to login to post a reply.