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

2012/10/17

Problem

Mirza Mirza

2012/10/17

#
I am beginer in Greenfoot programing. Right now I am writing code for guitar that I will play with keybord and mous. I have created array for strings and notes. every thing is working fine. Now Ineed code that vill remember all strings that I have played and in the ende it needs to print out all string that i have playd in order. I have no idea how to begin with that. ?? help plz
limefortheworld limefortheworld

2012/10/17

#
Now the question is how do you store strings and notes. Are strings and notes classes, and you have done the storing in the array, right? Assuming that the array is in the right order (and that you are using the array), you may wish to run a for loop printing out the notes. However, the bigger issue is what to print out.
for(int i = 0; i < "your array variable name".length; i++)
{
    System.out.println("your array variable"[i].toString());
}
This forces us to define the function toString(). Frankly I don't know squat about guitar, but the function should return a string like
public String toString()
{
if("insert note variable comparison")
{
}
...
}
danpost danpost

2012/10/18

#
Can you show how you constructed the arrays for the strings and the notes and explain how you wish to utilize each?
Mirza Mirza

2012/10/18

#
this is code for the strings
 public String string;
    public String sound;

    public String1(String stringName, String soundFile)
    {
        string = stringName;
        sound = soundFile;
    }

    /**
     * Act - do whatever the String1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(!isDown && Greenfoot.isKeyDown(string) || Greenfoot.mouseClicked(this)) {
            v = 90;
            vibrate();
            play();

        }
        vibrate(); 
       
        
    }   
and this is code in world class array for key whit wich i am going to play and notes for the strings public class Guitar extends World { private String names = {"a","s","d","f","g","h"}; private String notes = {"a1","b","c","d","e","f"}; /** * Constructor for objects of class Guitar. * */ public Guitar() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(797, 469, 1); makeStrings(); } public void makeStrings() {int i = 0; while (i < names.length) { String1 string = new String1(names, notes + ".wav"); addObject(string, 399, 75*i + 45); i++; } } } now mine question is . Keep track of all keys pressed so that you after a play session can see exactly what keys you pressed and in what order.
limefortheworld limefortheworld

2012/10/18

#
Ah. Then add a function to the world code, as well as an ArrayList variable to store your keys. Now you'd ask, why an arraylist? Because it is mutable in size, so you don't have to put a set size.
import java.util.ArrayList;

private ArrayList<String> keyPressed = new ArrayList<String>(); //The variable to store the keys

public void addString(String a)
{
keyPressed.add(a);
}
This will store the keys. Now add this to your act() method, right inside the if statement
((Guitar)getWorld()).addString(string or sound, depending on pick);
By doing so, every time you play the note the note will be stored in the array list, and it will be stored in order. As to displaying the keys, that is up to you.
danpost danpost

2012/10/18

#
I feel it would be much simpler just to use a String (which is a character array) to store the keystrokes themselves in (i.e. "dsasddd" -- mary had a little lamb )
limefortheworld limefortheworld

2012/10/18

#
Darn it, danpost, you's outthinking me, simplifying far more than I could.
Mirza Mirza

2012/10/18

#
Still have a problem limefortheworld I have tryed your code but it doesent work or I dont now how to implement it.
danpost danpost

2012/10/19

#
Here is the code with all the checking and saving
// instance variable
String notes = ""; // a String to hold the keystrokes
// method to check keystrokes
private void processKeys()
{
    String key = Greenfoot.getKey(); // get possible keystroke
    if (key != null && "asdfgh".indexOf(key) > -1) notes = notes + key; // save keystroke, if exists and valid
}
@limefortheworld, my suggestion was not intended to hurt your feelings. I am sorry if it came across that way. Rather, I was hoping to make things less complicated for the less experienced programmer.
limefortheworld limefortheworld

2012/10/19

#
@Danpost: No, you didn't hurt my feelings at all. But rather I was attempting to express awe at your superior method in a humorous way, which didn't work out so well. :P
You need to login to post a reply.