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

2014/2/23

Send 2 counters to one class

Osidy Osidy

2014/2/23

#
Hello, have ( with the help of @danpost ) figured out how to get counters to communicate with a single class, but in the world class, how do I send Counter2 and Counter3 classes to the Truck class? Do I separate the counters like this: Truck(moneyCounter, timeCounter) ? (Referring to line 37)
public void prepare()
    {
        Counter scoreCounter = new Counter();
        addObject(scoreCounter, 59, 15);
        
        Counter2 moneyCounter = new Counter2();
        addObject(moneyCounter, 149, 15);
        
        Counter3 timeCounter = new Counter3();
        addObject(timeCounter, 239, 15);

        Depot depot = new Depot(scoreCounter);
        addObject(depot, 50, 62);

        crateSpawn();//randomly spawn 2 starting crates
        crateSpawn();
        
        MoneyCrate moneycrate = new MoneyCrate();
        addObject(moneycrate, 311, 294);
        MoneyCrate moneycrate2 = new MoneyCrate();
        addObject(moneycrate2, 488, 294);

        Barrier barrier = new Barrier();
        addObject(barrier, 370, 244);
        barrier.setLocation(397, 185);
        Barrier barrier2 = new Barrier();
        addObject(barrier2, 398, 286);
        Barrier barrier3 = new Barrier();
        addObject(barrier3, 398, 387);
        Barrier barrier4 = new Barrier();
        addObject(barrier4, 189, 291);
        barrier4.setRotation(90);
        Barrier barrier5 = new Barrier();
        addObject(barrier5, 612, 296);
        barrier5.setRotation(90);

        Truck truck = new Truck(moneyCounter);
        addObject(truck, 63, 63);
        truck.setRotation(0);

    }
Osidy Osidy

2014/2/23

#
I asked too soon, for anyone else who had this question, you simply separate with a comma. This is how line 37 looks sending both Counter2, and Counter3:
Truck truck = new Truck(moneyCounter, timeCounter);
        addObject(truck, 63, 63);
        truck.setRotation(0);
And to make the truck recive the classes, in the truck constructor, it should look something like:
 /**
     * Truck Constructor
     */
    public Truck(Counter2 moneyCounter, Counter3 timeCounter)
    {
        counter=moneyCounter;
        counter3=timeCounter;
    }
You need to login to post a reply.