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

2017/1/31

Problem with making my playhead move when my playbutton class is activated

Nickin Nickin

2017/1/31

#
I have a Computer Science class program I am doing and my teacher is getting really mad at me for not showing it yet and that's because I'm not quite sure how I can get the playhead to move in accordance with the play button being pressed given that I put them in different classes so could anyone help me with this??? here is everything in my coding relevant to that This is the main class layout under world
public Layout()
    {    
super(900, 600, 1);

buildBaseLayout();
buildTrackButtonLayout();
buildDrumNotesLayout();

addObject(new playHead("playHead.png"),5,302);


//for the label

biggerLabel = new Labels("BPM:", SMALL_FONT);
biggerLabel.setFocusable(true);
addObject(biggerLabel,65,150);
biggerLabel = new Labels("Menu", LARGE_FONT);
biggerLabel.setFocusable(true);
addObject(biggerLabel,225,450);
biggerLabel = new Labels("Key", LARGE_FONT);
biggerLabel.setFocusable(true);
addObject(biggerLabel,675,450);
biggerLabel = new Labels("High Hat", SMALL_FONT);
biggerLabel.setFocusable(true);
addObject(biggerLabel,515,500);
biggerLabel = new Labels("Snare", SMALL_FONT);
biggerLabel.setFocusable(true);
addObject(biggerLabel,675,500);
biggerLabel = new Labels("Bass Drum", SMALL_FONT);
biggerLabel.setFocusable(true);
addObject(biggerLabel,845,500);
biggerLabel = new Labels("Key Signature", SMALL_FONT);
biggerLabel.setFocusable(true);
addObject(biggerLabel,100,515);
biggerLabel = new Labels("3", SMALL_FONT);
biggerLabel.setFocusable(true);
addObject(biggerLabel,275,497);
biggerLabel = new Labels("4", SMALL_FONT);
biggerLabel.setFocusable(true);
addObject(biggerLabel,275,547);
//for the text fields
biggerTF = new TextFields("60", 6);
biggerTF.setFont(SMALL_FONT);
biggerTF.addActionListener(this);
addObject(biggerTF, 150, 150);
biggerTF = new TextFields("4", 2);
biggerTF.setFont(SMALL_FONT);
biggerTF.addActionListener(this);
addObject(biggerTF, 210, 500);
biggerTF = new TextFields("4", 2);
biggerTF.setFont(SMALL_FONT);
biggerTF.addActionListener(this);
addObject(biggerTF, 210, 550);




}

  public void buildBaseLayout()
  {
     addObject(new sheetMusic("Track.png"),480,300);
     addObject(new key("key.png"),675,550);
}

public void buildDrumNotesLayout()
 {
    int x=50;
    int y=0;
    while(y<32){
      addObject(new hiHat("HiHat.png"),x,250);
      addObject(new HiHatCover("HiHatCopy.png"),x,250);
      addObject(new Snare("Snare.png"),x,300);
      addObject(new SnareCover("SnareCopy.png"),x,300);
      addObject(new Bass("Bass.png"),x,350);
      addObject(new BassCover("BassCopy.png"),x,350);
      x=x+25;
      y++;

    }
     
}

    /**
     * Create all the different parts of the editable Layout not including the menu. Each one has distinct properties.
     */
    public void buildTrackButtonLayout()
    {
        // Here are all the objects created with their properties
        //  object normal image, clicked image
addObject(new PlayButton("p","PlayButton.png","PlayButtonPressed.png"), 90, 50);
addObject(new PauseButton("s","PauseButton.png","PauseButtonPressed.png"), 150, 50);
addObject(new RewindButton("r","RewindButton.png","RewindButtonPressed.png"),30,50);
addObject(new FastFowardButton("f","FastFowarrdButton.png","FastFowardButtonPressed.png"),210,50);
}

public void actionPerformed(GUIComponent c) {
        if (c.getID() == 1002) {
            biggerLabel.setText("action on\nbutton\n# " + c.getID());
            c.setText("Action \nPerformed");
        }
    }
}
here is my track button class that contains a play button, pause button, fast foward button, and rewind button:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class TrackButtons here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TrackButtons extends Actor
{
    private boolean isDown = false;
    private String shortCutKey;
    private String buttonNormal;
    private String buttonHit;
    
    public TrackButtons(String shortCutKey, String TrackButtonsNormal, String TrackButtonsHit) // Here we construnc the object
    {
        this.shortCutKey = shortCutKey; // 'this' refers to the current object that calls the constructor
        this.buttonNormal = buttonNormal;
        this.buttonHit = buttonHit;
    }
    
    /**
     * Act - do whatever the TrackButtons wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if ( !this.isDown && Greenfoot.isKeyDown(this.shortCutKey) ) // If we havent yet pressed the key but the button is down go do this
        {
            this.setImage(this.buttonHit);
            this.isDown = true;
        }
        if ( this.isDown && !Greenfoot.isKeyDown(this.shortCutKey) ) // If we are pressed but the button is not do this
        {
            this.setImage(this.buttonNormal);
            this.isDown = false;
        }
    }
    
    
}
and here is my code for the playbutton:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PlayButton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayButton extends TrackButtons
{
    /**
     * Act - do whatever the PlayButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public PlayButton(String key,String buttonNormal, String buttonHit)
    {
       super(key, buttonNormal, buttonHit);  
    }    
    
    
}
Then I have the playhead class which is the class import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class playHead here. * * @author (your name) * @version (a version number or a date) */ public class playHead extends SheetMusicStuff { public playHead(String sheetNormal){ super(sheetNormal); } } also the code of the class it extends:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SheetMusicStuff here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SheetMusicStuff extends Actor
{
    private String sheetNormal;
    public SheetMusicStuff(String sheetNormal) // Here we construnc the object
    {
        this.sheetNormal = sheetNormal;
    }
    
} 
Super_Hippo Super_Hippo

2017/1/31

#
You have an error in your 'TrackButtons' constructor. I guess you changed the parameter names but forgot to change the next lines accordingly:
public TrackButtons(String shortCutKey, String trackButtonsNormal, String trackButtonsHit)
{
    this.shortCutKey = shortCutKey; //"this" is only needed if both the parameter and the variable have the same name
    this.buttonNormal = trackButtonsNormal;
    this.buttonHit = trackButtonsHit;
}
You need to login to post a reply.