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

2014/5/13

Buttons

BeYouself BeYouself

2014/5/13

#
How do i make the scenario play sound if the user clicks on a particular button
Zamoht Zamoht

2014/5/13

#
When the button is clicked you should use GreenfootSound to play a sound. Please tell me how much you've managed to code yourself. It will make it easier to help you.
BeYouself BeYouself

2014/5/13

#
I am kind of new to Greenfoot. So, i need the code from scratch. Let the actor be Button. I need the code for if you click on the button you will play a particular sound
danpost danpost

2014/5/13

#
This shows the basic components:
1
2
3
4
5
6
7
8
9
Actor button = new Actor()
{
    public void act()
    {
        if (Greenfoot.mouseClicked(this)) (new GreenfootSound(/* sound filename */)).play();
    }
};
button.setImage(new GreenfootImage(/* image filename */));
addObject(button, /* x, y locations */);
BeYouself BeYouself

2014/5/14

#
Should i put this code in the button's code editor? When I type in the first line of code, a syntax error is coming: greenfoot.Actor is abstract;cannot be instatiated. When i add the object to the world, a terminal error is coming: Stack Overflow error. What do I do?
danpost danpost

2014/5/14

#
I said it shows the basic components. The way I wrote it above, the first seven lines is one statement (that is probably why after typing the first line you got an error; as well as the abstract Actor instantiation error). The stack overflow may or may not be due to how you applied the snippet into your code. You will probably have to post your code to see.
BeYouself BeYouself

2014/5/14

#
Actor SoundOn=new Actor
1
2
3
4
5
6
7
8
9
10
11
12
public void act()
    {
 
                 
        if (Greenfoot.mouseClicked(this))
        {(new GreenfootSound("Star_Wars_Theme_Song.mp3")).play(); 
              
 
        getWorld().addObject(new SoundOn() ,909,62); 
    }
    SoundOn.setImage(new GreenfootImage("SoundOff.png")); 
}}
danpost danpost

2014/5/14

#
Because line 9 is being unconditionally executed from within your act method (of your Button class, I presume), you will be creating an abundance of SoundOn objects (stacked on top of one another) which is probably causing the memory problems; although stack overflow usually means you have methods calling each other or one method calling itself. Line 9 should probably be in the 'addedToWorld' method; and line 11 should be executed only on the condition that the song is no longer playing. This you will not be able to determine unless you have a reference to the GreenfootSound object that is playing. Also, line 11 will not execute properly because you are trying to set the image of a class (classes do not have images; World and Actor objects have images; the classes only hold the default image to be used for its instantiated objects). So, you will also need a reference to the SoundOn object that is created.
BeYouself BeYouself

2014/5/14

#
could you fix it and post the code?
BeYouself BeYouself

2014/5/18

#
Hello danpost. I have fixed my above code. It is showing no syntax error,but this code is not executing in the world. Could you help?
1
2
3
4
5
if(Greenfoot.mouseClicked(this))
        {
          (new GreenfootSound("SW.mp3")).play(); 
          setImage("SoundOn.png");
        }
danpost danpost

2014/5/18

#
If this code is in your world class, then you cannot use 'setImage'. It must go in the class of the actor that you click on to start the music.
BeYouself BeYouself

2014/5/18

#
it is in the actor class. The sound isn't playing for some reason after i press run and click on the object
danpost danpost

2014/5/18

#
Did you put the code in the 'act' method?
BeYouself BeYouself

2014/5/18

#
yes
danpost danpost

2014/5/18

#
Can you drag the object around before clicking on 'Run'?
You need to login to post a reply.