let’s call that field isDown and the declaration is below: We will use a boolean field that we add to the class to remember if the class is actually down at the moment. Therefore, lets make the code better so that the image is set only when it actually changes. A more important reason is that when we add sound it will make a big difference if the sound of the key is heard once or over and over again. One is that it is bad programming practice to waste your computers CPU resources doing unnecessary work. There are a few reasons why we have to fix this though. Why is this a big problem? Setting the image to itself is definitely unnecessary, but doesn’t seem incorrect. Looking at the code, you can see that even if the “g” key is not being pressed, the image will be reset to white-key.png although it is already showing that image. Every time that the act method executes, the image is set again to one of the two images even though it might be on the correct image anyhow. ![]() The image is not set only once as it is changed but all the time. While this code does work, there is a problem. ![]() When the key is not pressed, the musical keyboard key goes to the up position.ĥ.3 Do This: Put this version of the code in to your own act method. When g is pressed the musical keyboard key switches to the picture of it down. Later on we will choose keys on the computer keyboard with thought and purpose, right now we just want to get the code right. Take a look at this code:įor this example we choose a letter more or less at random, (the “g” key) to get the key object to react to. You just need code that will switch the image to the white-key-down.png when the right key on the computer keyboard is pressed. Since we already sort of know how to do this when we animated our crabs walking, we should be able to do this pretty easily. The white-key.png is the image that is set to default for class Key. Those are the two states that we want to show. The scenario already has two images files, one is called white-key.png and the other is called white-key-down.png. The first task is to animate the piano key, when a key is pressed on the keyboard, we want the key object in the world to change so it looks like it’s pressed down. You can start experimenting by placing down an object of class Key in the world and then running the scenario. If you looked at the code, you know that there is not much there, the Piano class only has the size and resolution of the world, and the key class only has method stubs ( an empty method) for the act and the constructor method. Make a few of them and place them all next to each other. ![]() Explain the code that is already there and what it does.ĥ.2 Do This: Create an object of class Key and put it in the world. Start by opening the scenario called piano-1 from the Handout folder.ĥ.1 Do This: Open the scenario piano-1 and look at the code that is already there for the two classes that already exist, Piano and Key. : No line matching interface SourceDataLine supporting format PCM_FLOAT 44100.0 Hz, 32 bit, stereo, 8 bytes/frame, is supported.Īt .getLine(AudioSystem.java:479)Īt (SoundStream.java:436)Īt .(SoundStream.java:102)Īt (SoundFactory.java:112)Īt .(SoundFactory.java:65)Īt (SoundFactory.java:78)Īt (GreenfootFrame.java:495)Īt .(GreenfootFrame.java:272)Īt (GreenfootFrame.java:251)Īt $2.run(GreenfootMain.java:214)Īt .dispatch(InvocationEvent.java:311)Īt (EventQueue.java:756)Īt $500(EventQueue.java:97)Īt $3.run(EventQueue.java:709)Īt $3.run(EventQueue.java:703)Īt (Native Method)Īt $JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)Īt (EventQueue.java:726)Īt (EventDispatchThread.java:201)Īt (EventDispatchThread.java:116)Īt (EventDispatchThread.java:105)Īt (EventDispatchThread.java:101)Īt (EventDispatchThread.java:93)Īt (EventDispatchThread.Now we are going to start a new project, a piano that you can play with the computers keyboard. If you have a sound card installed, check your system settings. But I get this error as soon as I open Greenfoot with my wav files in the sounds file.Ĭould not play sound file: file:/C:/Users/ASUS/Desktop/Tic%20Tac%20Toe%203.0/sounds/Exit%20Friendzone%20ft.%20Eden%20-%20Iris%20%5bNCS%20Release%5d.wav I want to have a Menu Music and different sounds for each game mode. I have 3 game modes for the solo player mode, easy medium and hard. ![]() Hey, I am currently working on a Tic Tac Toe Version which includes a duo player mode to play against your friend that sits right next to you or to play against the computer.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |