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

2012/8/16

Calling methods in one actor through another actor. (not sub classes).

gusbus123 gusbus123

2012/8/16

#
Hi, i have a method in a, lets say a class called "Block". In this class i have a method that i want to be able to use through another actor. So if i clicked on the Button class it would call the method in the Block class. How would you be able to do this if they are not subclasses of each other?
davemib123 davemib123

2012/8/16

#
I would suggest checking this link: http://www.greenfoot.org/doc/howto-1
gusbus123 gusbus123

2012/8/16

#
ok thx.
gusbus123 gusbus123

2012/8/16

#
that tutorial allows you to save 1 variable in the world, so it will not work for me as i have multiple of the same objects, hence when it is created only the last one that is created will be accessed by my second class. I want to be able to access this code below which in my real scenario is in the class called 'Pins', through the class called 'Buttons'. This method will be called when the object of the Buttons class is clicked on.
public void newRow()
    {
        if(!used) {
            used=true; getWorld().addObject(new Pins(PosNum, CurrentRow+1), getX(), getY()-35);
        }
    }
and all of the objects of the Pins class needs to do this method when the Buttons object is clicked on. The amount of Pins are unlimited.
gusbus123 gusbus123

2012/8/17

#
dw. just realised its more or less impossible the way i was thinking of. Ill just do it another way then.
danpost danpost

2012/8/17

#
In the Buttons class act method:
if (Greenfoot.mouseClicked(this))
{
    for (Object obj : getWorld().getObjects(Pins.class))
    {
        Pins pins = (Pins) obj;
        pins.newRow();
    }
}
gusbus123 gusbus123

2012/8/18

#
ok thx. that worked perfectly.
You need to login to post a reply.