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

2012/9/30

piano scenario constructor

luna124 luna124

2012/9/30

#
Hi together- it's me again. I've got another problem now- it seems to be a never ending story... ;-) In the constructor of the class "Piano" I have this code:
/**
     * Make the piano.
     */
    public Piano() 
    {
        super(800, 340, 1);
        makeKeys();
    }
    
    /**
     * Makes keys.
     */
    public void makeKeys()
    {
        int i=0;
        while (i<12)
        {
            addObject(new Key("g", "3g.wav"),i*63+54,140);
            i=i+1;
        }
        
    }
I am asked to replace the numbers, e.g. I have to replace 63 by Key.getImage().getWidth() But if I write it in my code it says:
non-static method getImage() cannot be referenced from a static context
I don't know what to write instead because this is what my teacher said we should write. Thank you for your help, luna124
SPower SPower

2012/9/30

#
Well, that error means that you're calling an object which should be called on an object, but you're calling it on a class. And with 63, which line do you mean?
SPower SPower

2012/9/30

#
O, I see. Well, I advice this:
Key key = new Key("g", "3g.wav");
addObject(key, i *key.getImage().getWidth() +54, 140);
luna124 luna124

2012/9/30

#
Thanks, it works but I probably will have to explain it to my teacher why I wrote it this way. So what should I tell him? Are you using a variable?
SPower SPower

2012/9/30

#
Let's see if you understand this: I store the key object, so I can access it's image.
luna124 luna124

2012/9/30

#
Ok, I understand and how can I do it for 54? In the task it says something with getWidth() of piano. I tried it but it didn't work...
SPower SPower

2012/9/30

#
54 is the distance from the left key to the left of the world. You don't have to change that.
You need to login to post a reply.