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

2020/2/21

If Statement Help

DaRafster DaRafster

2020/2/21

#
I'm trying to create an "if statement" saying: "If the world's super values are increased (dimensions and cell size increased), then run this code" How would I do this exactly?
danpost danpost

2020/2/21

#
DaRafster wrote...
I'm trying to create an "if statement" saying: "If the world's super values are increased (dimensions and cell size increased), then run this code" How would I do this exactly?
A world's super values, once a world is created, are set in stone. Maybe you need to rephrase your question and give some context.
DaRafster DaRafster

2020/2/21

#
First off here's my code for a piano I created with some help:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A piano that can be played with the computer keyboard.
 * 
 * @author:
 * @version: 0.1
 */



public class Piano extends World
{
    int change = 0;
    
    
    /**
     * Make the piano.
     */
    public Piano() 
    {
        
        super(815, 340, 1);
        
        
        
        // Keys on the home row act as the notes of the white keys on the piano
        whiteKeyPlacement("a","2c.wav");
        whiteKeyPlacement("s", "2d.wav");
        whiteKeyPlacement("d", "2e.wav");
        whiteKeyPlacement("f", "2f.wav");
        whiteKeyPlacement("g", "2g.wav");
        whiteKeyPlacement("h", "2a.wav");
        whiteKeyPlacement("j", "2b.wav");
        whiteKeyPlacement("k", "3c.wav");
        whiteKeyPlacement("l", "3d.wav");
        whiteKeyPlacement(";", "3e.wav");
        whiteKeyPlacement("'", "3f.wav");
        whiteKeyPlacement("enter", "3g.wav");
        
        
       
        // Keys above the home row act as the notes of the black keys on the piano
        blackKeyPlacement("w", "2c#.wav", -700);
        blackKeyPlacement("e", "2d#.wav", -700);
        blackKeyPlacement("t", "2f#.wav", -700 + 65);
        blackKeyPlacement("y", "2g#.wav", -700 + 65);
        blackKeyPlacement("u", "2a#.wav", -700 + 65);
        blackKeyPlacement("o", "3c#.wav", -700 + 65*2);
        blackKeyPlacement("p", "3d#.wav", -700 + 65*2);
        blackKeyPlacement("]", "3f#.wav", -700 + 65*3);
        
        

    }
    
    
    
    // Adds a white key image requiring some values in order for the method to be called
    public void whiteKeyPlacement(String note, String file)
    {
        whiteKey key = new whiteKey(note,file);
        addObject(key, 50 + change, getHeight()/3);
        change = change + 65;
    }
    
    
    
    // Adds a black key image requiring some values in order for the method to be called
    public void blackKeyPlacement(String note, String file, int width)
    {
        blackKey key = new blackKey(note,file);
        addObject(key, width + change, getHeight()/4);
        change = change + 65;
    }
    
    
    
    public void keyPlacer()
    {
        
    }
}
    
If I were to ever increase the super values, I would like it so it auto-generates more piano keys until it can't fit anymore. I thought that if I could make an "if statement" communicating to Greenfoot when the super values are increased, it will run a specific code that will perform this task. So what would I have to do in order to achieve this?
danpost danpost

2020/2/21

#
DaRafster wrote...
If I were to ever increase the super values
How would you do that?
DaRafster DaRafster

2020/2/21

#
danpost wrote...
DaRafster wrote...
If I were to ever increase the super values
How would you do that?
Going into the code itself and changing the values. Sorry if I'm not being clear enough
danpost danpost

2020/2/22

#
DaRafster wrote...
Going into the code itself and changing the values. Sorry if I'm not being clear enough
Then, what do you need an if statement for? Just build the world based on their values. The width of the world divided by the width of a white key should give the number of white keys to add. You can add the black keys up to, but not more right than the last white key. Using for loops to add the keys into the world would be the way to go.
You need to login to post a reply.