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

2012/10/16

using setImage on another actor

jmr jmr

2012/10/16

#
I'm trying to change the image of a boat after catching a fish and I can't seem to figure out the syntax in order to accomplish this. Right now the Hook image is being affected by setImage and I want the boat's to change. Here's the code I'm using:
public void CatchFish()
{
int count = 0;
if (Greenfoot.isKeyDown("space"))
{
Actor fish = getOneIntersectingObject(Fish.class);
{
if (fish != null)
{
getWorld().removeObject(fish);
count = count + 1;
if (count == 1)
{
getWorld().getObjects(Boat.class);
setImage("1fish.gif");
}
}
}
}
}
}
danpost danpost

2012/10/16

#
Line 14, does not set a local variable to the boat object. Line 14 and 15 need to be
Boat boat = (Boat) getWorld().getObjects(Boat.class).get(0);
boat.setImage("1fish.gif");
The first line gets the first object (in the zero position) from the list of objects that 'getObjects(Class cls)' returns. The object is returned as of an Object type (not a Boat type) and therefore needs to be cast to a Boat class object. The variable 'boat' (that is declared to be of type Boat) is then set to that object. The next statement says to change the image of that boat object to the image in the file "1fish.gif".
jmr jmr

2012/10/17

#
thanks a lot! works like a charm!
jmr jmr

2012/10/17

#
ok so I'm still having a little trouble... if i need to change the boat again (and again and again) how exactly would I be referencing it?
danpost danpost

2012/10/17

#
Please refer to the discussion Need help changing actor image.
You need to login to post a reply.