gogldp.blogg.se

Greenfoot music player
Greenfoot music player











greenfoot music player
  1. Greenfoot music player how to#
  2. Greenfoot music player code#
  3. Greenfoot music player Offline#

Greenfoot music player Offline#

If (!isDown & Greenfoot.Dictionary is a free offline English dictionary containing over 200,000 words and definitions and no ads. The & double ampersand means AND and the ! exclamation mark means NOT. Both of these symbols are logical operators.

Greenfoot music player code#

The code also has two new symbols: & the double ampersand, and ! the exclamation mark. In both cases we make sure that the isDown field is set to a new state if detect a change. The code below what the act method should look like: In those two situations, we will change the image. But if the isDown field is true, but the “g” key is not down, then it must have been just released. You see if the isDown field is false but the “g” key is being pushed on the keyboard it must have just been pressed a second ago.

greenfoot music player

This lets us check if the keyboard key has just now been pressed. Put private boolean isDown in the correct spot in Key. Look for where the other variables are declared like: private int wormsEaten in the Crab class. If the key is down, then the value stored in isDown will be true, and if the key is not, the value stored in isDown will be false.ĥ.4 A Do this: You will have to add private boolean isDown to the Key class in the right spot, if you don’t remember where it should go, then just check out your Crab class. 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.

Greenfoot music player how to#

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.

greenfoot music player

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. Now we are going to start a new project, a piano that you can play with the computers keyboard.













Greenfoot music player