Good evening, I am a beginner. I am totally lost with this problem. I have to play a sound when the robot collisions with the block and the wall in different methods.
I am trying different approaches but it doesn't stop playing the music or it doesn't let the robot move.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Robot here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Robot extends Actor
{
/**
* Act - do whatever the Robot wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
robotMovement();
detectWallCollision();
detectBlockCollision();
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void robotMovement()
{ int y= getY();
int x= getX();
if(Greenfoot.isKeyDown("Up")) y--;
if(Greenfoot.isKeyDown("Down"))y++;
if(Greenfoot.isKeyDown("Left"))x--;
if(Greenfoot.isKeyDown("Right"))x++;
setLocation(x,y);
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void detectWallCollision()
{
if (isTouching(Wall.class))
setLocation(104,194);
Greenfoot.playSound ("hurt.wav");
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void detectBlockCollision()
{
if (isTouching(Block.class))
setLocation(104,194);
Greenfoot.playSound ("hurt.wav");
}
}
