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

2012/3/26

Calling a variable from another class.

Sneaky4296 Sneaky4296

2012/3/26

#
I'm trying to call a variable from another class. What I'm trying to do, is set the image of an actor to depend on another actor's data, and to do this I made an Array in my world class. The Array has strings, and each one is a variable, and each variable is set to the string of the filename. I want to be able to change this variable through an actor class. How would I go about doing this?
Sneaky4296 Sneaky4296

2012/3/26

#
Here is my world class:
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; /** * A piano that can be played with the computer keyboard. * * @author Jonah Verner */ public class Piano extends World { public String Akey = "white-key.png"; public String Skey = "white-key.png"; public String Dkey = "white-key.png"; public String Fkey = "white-key.png"; public String Gkey = "white-key.png"; public String Hkey = "white-key.png"; public String Jkey = "white-key.png"; public String Kkey = "white-key.png"; public String Lkey = "white-key.png"; public String Semicolonkey = "white-key.png"; public String Apostrophekey = "white-key.png"; public String Slashkey = "white-key.png"; private String whiteKeys = { "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "\\" }; private String whiteNotes = { "3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d", "4e", "4f", "4g" }; private String blackKeys = { "W", "E", "", "T", "Y", "U", "", "O", "P", "", "]" }; private String blackNotes = { "3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#" }; private String keyColors = { Akey, Skey, Dkey, Fkey, Gkey, Hkey, Jkey, Kkey, Lkey, Semicolonkey, Apostrophekey, Slashkey}; /** * Make the piano. This means mostly, apart from defining the size, * making the keys and placing them into the world. */ public Piano() { super(800, 340, 1); makeKeys(); } /** * Create the piano keys (white and black) and place them in the world. */ private void makeKeys() { for(int i = 0; i < whiteKeys.length; i++) { Key key = new Key(whiteKeys, whiteNotes+".wav", keyColors, "white-key-down.png"); addObject(key, 54 + (i*63), 140); } for(int i = 0; i < whiteKeys.length-1; i++) { if( ! blackKeys.equals("") ) { Key key = new Key(blackKeys, blackNotes+".wav", "black-key.png", "black-key-down.png"); addObject(key, 85 + (i*63), 86); } } } }
And here is my class to call it:
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * A key on a piano keyboard. This key has a pair of images to show a normal * and depressed state, and it is associated with a keyboard key and a sound * file, which is played when the key is pressed. * * @author: M. Kolling * @version: 1.0 */ public class Key extends Actor { private boolean isDown; private String key; private String sound; private String upImage; private String downImage; /** * Create a new key linked to a given keyboard key, and * with a given sound. */ public Key(String keyName, String soundFile, String img1, String img2) { sound = soundFile; key = keyName; upImage = img1; downImage = img2; setImage(upImage); isDown = false; } /** * Do the action for this key. */ public void act() { int keyToPlay = 1; if (!isDown && Greenfoot.isKeyDown(key)) { play(); setImage(downImage); isDown = true; } if (isDown && !Greenfoot.isKeyDown(key)) { setImage(upImage); isDown = false; } if (keyToPlay == 1) { Jkey = "red-key.png"; } } /** * Play the note of this key. */ public void play() { Greenfoot.playSound(sound); } }
The variable it can't find is Jkey
kiarocks kiarocks

2012/3/26

#
you need an instance of Piano,
Piano p = (Piano)getWorld();
then call
p.JKey
for JKey
Sneaky4296 Sneaky4296

2012/3/27

#
Thank you!
You need to login to post a reply.