AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Greenfoot play and stop sound code3/20/2024 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. I know I asked a lot of questions but I honestly appreciate any help I can get with this project.Now we are going to start a new project, a piano that you can play with the computers keyboard. If MainMenuWorld starts playing "song1" and I click the play button which setWorld's Level1, how do I stop the music from MainMenuWorld? Should I be creating a Music actor that somehow plays the sounds? OR do I have to stop the sound before progressing to each new level? For each level however how am I supposed to STOP the current sound playing? For the first (main menu) I suppose I could just run it in the world class. I want my sound file to play only when the new world is loaded. My main question is for what I'm working on right now. This was how I saw my game being created, if you have any suggestions or objections to how I should be running it please tell me. When this new world loads it will populate my world according to where I want all my actors placed. When a world is completed I will be using the setWorld command to load a new one. My main question is how I should be organizing my game and where I should be initializing the sound.Ĭurrently I have a new world for each level. I've started creating a Platformer game and decided to implement music.
0 Comments
Read More
Leave a Reply. |