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

2012/5/30

How would I make a button?

ChaosWar ChaosWar

2012/5/30

#
So, how would I go about making a button that can turn off the background music? For my game of Breakout I already have music playing in the background when you press 'Run'. I have no idea what the code would be for it etc. xD
erdelf erdelf

2012/5/30

#
make a class called button and type the following in it:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.awt.Color;
import javax.swing.*;

public class Button extends Actor
{
    private int funktion;
    private String name;
    private boolean funktionAusgefuehrt;

    public void setImages(GreenfootImage image)
    {
        setImage(image);
    }

    public Button(int width, int height, String pName, int pFunktion, GreenfootImage pImage)
    {
        name = pName;
        GreenfootImage image = pImage;
        image.drawString(pName, width, height);
        funktion = pFunktion;
        setImage(image);
    }

    public void act() 
    {
        mausUeberButton();
        anklicken();
    }    

    public void mausUeberButton()
    {
        MouseInfo meineMaus = Greenfoot.getMouseInfo();
        if(meineMaus!=null)
        {
            if(meineMaus.getActor() == this && !funktionAusgefuehrt)
            {

            }

            if(meineMaus.getActor() != this && funktionAusgefuehrt)
            {
                funktionAusgefuehrt = false;
            }
        }
    }


    public void anklicken()
    {
        MouseInfo meineMaus = Greenfoot.getMouseInfo();
        if(meineMaus != null)
        {
            if(funktion==0 && Greenfoot.mouseClicked(this))
            {
                World world = new YourWorld();
                world.yourMusic.stop();
            }
        }
    }
}
And in the world
Button endles = new Button(width, height, "", 0, imageOfButton);
ChaosWar ChaosWar

2012/5/30

#
So, in here: public Button(int width, int height, String pName, int pFunction, GreenfootImage pImage) how come it says 'pName' and 'pFunction' etc? -I'm still learning so, there's a fair bit i probably don't understand.
ChaosWar ChaosWar

2012/5/30

#
Thank you :D
erdelf erdelf

2012/5/30

#
it is only a name, call it an other name and it will work, too
You need to login to post a reply.