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

2014/9/1

Image Switching

awent0428 awent0428

2014/9/1

#
In this game that I made, there are cars that run over people. I would like to make the image switch from the original to a different image when a car collides with it. Could someone help please?
NikZ NikZ

2014/9/1

#
You could use setImage("Your file here").
danpost danpost

2014/9/1

#
It appears you already have an Actor subclass for a run over person called 'Dead'. Create and add a 'Dead' object at the location of the 'person' found' before removing 'person' from the world. You may want to add a 'setPaintOrder' statement in your World subclass constructor to paint 'Car' and 'Car2' objects after (or above) other objects:
setPaintOrder(Car.class, Car2.class);
FYI, having the following code in any 'act' method is totally useless (see your Car class 'act' method):
if (Greenfoot.isKeyDown("enter")) {
    Greenfoot.start();
}
The only way that code would get executed is if the project was already started.
awent0428 awent0428

2014/9/1

#
I can not understand how to do this.
danpost danpost

2014/9/1

#
Setting the image of any 'person' found will not change it from being a Person object and you end up repeatedly setting its image and playing the sound until the center of the car not longer intersects the image of 'person'. By replacing the Person object with a Dead object, each Person object will only be detected once and the sound will play once per person:
if (person != null)
{
    getWorld().addObject(new Dead(), person.getX(), person.getY());
    getWorld().removeObject(person);
    Greenfoot.playSound("splat.wav");
}
awent0428 awent0428

2014/9/1

#
Wow! Thanks! That was easier than I thought!
You need to login to post a reply.