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

2017/4/7

Setting an Image on another actor

Gaming_100 Gaming_100

2017/4/7

#
Hello! Sorry for the frequent help needed... I'm very new to greenfoot. Is it possible to set another actors image from when a button actor is clicked? Example: If i click a button another actor's image will change from green to red and visa versa.
Nosson1459 Nosson1459

2017/4/7

#
You'll have to have a reference from one to the other. In the World you can add the following:
1
2
3
4
5
6
7
8
9
10
// out of all methods
private ButonActor button = new ButtonActor();
 
// in constructor
addObject(button, x, y);
 
// with other methods
public ButtonActor getButton() {
    return button;
}
Then in the actor that you want to change the image:
1
2
3
4
public void act() {
    ButtonActor button = ((WorldName) getWorld()).getButton();
    if (Greenfoot.mouseClicked(button)) setImage(...
}
Gaming_100 Gaming_100

2017/4/7

#
Is there a way to send a simple command to the other actors?
Nosson1459 Nosson1459

2017/4/7

#
You can't just send a command to an actor, you can only change things that are static and public.
danpost danpost

2017/4/8

#
Gaming_100 wrote...
Is it possible to set another actors image from when a button actor is clicked?
Yes it is -- with simple commands. You will, however, probably need to show the codes to said Button class, the codes to the actor whose image is to change and your world constructor and prepare methods and any other codes in that class that reference the said button or said color-changing actor. You can start with the Button class for now as well as specifying the type of actor the color changer is (what class creates it).
Gaming_100 Gaming_100

2017/4/8

#
Ok.. i will try some things.. thanks for the help! :)
danpost danpost

2017/4/8

#
Gaming_100 wrote...
Is there a way to send a simple command to the other actors?
Does this mean you have multiple actors that you want to have changing colors when the button is clicked -- like ALL actors of a given type?
You need to login to post a reply.