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

2015/9/10

if (sound) setimage

hoothoot hoothoot

2015/9/10

#
what would a code look like where if a certain sound was playing, the image of the character changes?
danpost danpost

2015/9/10

#
In order to be able to check to see if a sound is playing, you need to keep a reference to the sound as a GreenfootSound object as well as any image used save in fields. With the following fields
private GreenfootSound mySound = new GreenfootSound(<sound file name>);
private GreenfootImage normalImage = new GreenfootImage(<normal image file name>);
private GreenfootImage soundImage = new GreenfootImage(<sound image file name>);
You can have something like the following in the act method or in a method it calls
// for when sound is triggered
if (<whatever> && getImage() != soundImage)
{
    mySound.play();
    setImage(soundImage);
}
// for when sound ends
if (getImage() == soundImage && !mySound.isPlaying())
{
    setImage(normalImage);
}
hoothoot hoothoot

2015/9/10

#
thanks danpost!
hoothoot hoothoot

2015/9/10

#
forget this one-
hoothoot hoothoot

2015/9/10

#
let me clear this up, if object 2 dies: object 2 plays (sound.wav) object 2 disappears. what i want so know is, is there a way for me to set the image on object 1 when the sound plays?
danpost danpost

2015/9/10

#
hoothoot wrote...
what i want so know is, is there a way for me to set the image on object 1 when the sound plays?
let me clear this up: if object 2 dies object 1 changes image object 1 plays (sound.wav) object 2 disappears That is, place these codes in the object 1 class. If interaction with object 1 is the only thing that decreases the health of object 2, then this should be no problem. Do not have object 2 check is own health value for death. Have object 1 check it when decreasing its health.
hoothoot hoothoot

2015/9/11

#
i think I understand. thanks for the help
You need to login to post a reply.