Is there a way to add a sound based on an object class that you land on? and that it will only play every 5 seconds ? so far I got it to play when I'm on the object but it plays so fast.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Man here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Man extends Actor
{
private GreenfootImage red;
private GreenfootImage black;
public Man()
{
red = new GreenfootImage("man2.png");
black = new GreenfootImage ("man1.png");
setImage(black);
}
/**
* Act - do whatever the Man wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
checkKeyPress(); // call this method to work
changeImage();
}
public void checkKeyPress()
{
if (Greenfoot.isKeyDown("left"))
{
setLocation(getX()-1,getY()); // get loaction of x and y, then move -1 on x move Left
}
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+1,getY()); // move Right
}
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(),getY()+1); // move down
}
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(),getY()-1); // move up
}
}
public boolean onFire()
{
Actor Fire = getOneObjectAtOffset( 0 , 0, Fire.class);
return Fire != null;
}
public boolean onWall() // test to see if the man class is on the wall class
{
Actor Wall = getOneObjectAtOffset( 0 , 0, Wall.class);
return Wall != null;
}
public boolean onDoor()
{
Actor Door = getOneObjectAtOffset( 0 , 0, Door.class);
return Door != null;
}
public void changeImage() // use method to change image of man
{
if( onWall() )
{
setImage( black );
}
if( onFire() )
{
setImage( red );
Greenfoot.playSound("au.wav");
}
if( onDoor() )
{
Greenfoot.playSound("hooray.wav");
Greenfoot.stop();
}
}
}