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

2012/3/4

How to add an object with a specific image.

murphys murphys

2012/3/4

#
I am trying to extend the piano project in the Greenfoot book. I have a Key class method that plays a specific note depending on the key pressed. The Key class method also adds an object for a different class (called Note) when the key is pressed. To do this it calls a method from the World subclass (Piano). Currently, the same object image is added whatever the key pressed. Is there a way of specifying which image will be added when the Key class method adds a Note class object? Many thanks in advance. This forum is very helpful towards, and patient with, beginners like me.
danpost danpost

2012/3/4

#
You can create a constructor in the Note class that takes a parameter which tells it which image to use (you can have more that one constructor, but they all have to either except a different number of parameters or parameters of different types; i.e. int, String, Color, float, boolean, etc). In your case, you already have a variable that has a distinct value in each of the Key class objects (whether it is called value, tone, key, or note). So, if you add a parameter to the method signature to recieve that 'key' information (sorry for the pun), you can pass it along to the Note constructor by adding a parameter there also. The constructor can then, depending on the value of the parameter, determine what image to use. If you use a String variable, then if an empty string is passed, create one image, else create another. Just one question: does the Key class have an image of its own (or two, does it extend Actor)?
murphys murphys

2012/3/5

#
Thank you danpost for your reply. Yes, the Key class does provide specific images (white key or black keys) when the keys are constructed. I guess I should look at that code and adapt it for my purposes. That "one last question" was Colombo-esque in its pertinence. Thank you again.
You need to login to post a reply.