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

2019/7/19

Problem with sound

zizousd zizousd

2019/7/19

#
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");
    }

}    
danpost danpost

2019/7/19

#
Your sound playing commands are not under any conditions. If you do not use curly brackets for your if blocks, one, and only one, statement will be under the given condition.
zizousd zizousd

2019/7/20

#
Thanks a lot, now it works, i'll pay more attention with the if commands.
You need to login to post a reply.