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

2012/2/20

Error: cannot find symbol- variable image 1

sophiasturges sophiasturges

2012/2/20

#
I'm not sure why I'm getting this error for assigning the up and down image to image 1 and 2? I'm trying to allow the key class to show black or white keys... I'm not having much luck. Help? import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class Key extends Actor { private boolean isDown; private String key; private String sound; private String upImage; private String downImage; /** * Create a new key. */ public Key (String keyName, String soundFile) { key = keyName; sound = soundFile; upImage = image1; downImage = image2; setImage(upImage); isDown = false; } /** * Do the action for this key. */ public void act() { if ( !isDown && Greenfoot.isKeyDown(key)){ play(); setImage (downImage); isDown = true; } if (isDown && !Greenfoot.isKeyDown(key)){ setImage (upImage); isDown = false; } } /** * Play the note of this key. */ public void play() { Greenfoot.playSound(sound); } } Thanks, Sophie
Builderboy2005 Builderboy2005

2012/2/20

#
You say:
upImage = image1;
Where are you expecting image1 to come from? You havent defined it anywhere in your code, and therefore Greenfoot is throwing an error because it doesn't know what you are reffering to when you say image1.
danpost danpost

2012/2/20

#
You have declared 'upImage' and 'downImage' as 'String's, which need to be used to create the 'GreenfootImage image's, which, in turn, are used in the 'setImage(image)' method.
upImage = "filename.png"; // replace with actual name of image file
GreenfootImage image1;
image1 = new GreenfootImage(upImage);
setImage(image1);
Repeat the previous code (except the last line) for the second image. HOWEVER, instead of saving the String name of the image file, why not save the image itself! Declare 'GreenfootImage upImage;' and 'GreenfootImage downImage;' (instead of 'String upImage' and ...). Then in the constructor
upImage = new GreenfootImage("filename.png"); // replace with actual name of image file
setImage(upImage);
Repeat the first line for the 'downImage'. The advantage of the latter, is you do not have to create a new image each time; you only need set the image of the object to the proper image.
sophiasturges sophiasturges

2012/2/21

#
Thank you danpost for the detailed reply. I really appreciate it! @builderboy2005--thanks for pointing that out. Sophie
You need to login to post a reply.