![]() The notation used here is called dot notation. The limit 20 is excluded, so the number is actually in the range 0 to 19. For example, Greenfoot.getRandomNumber(20) will give us a random number between 0 and 20. ![]() It will then return a random number between 0 (zero) and the limit. This method, called getrandomnumber, expects a parameter that specifies the limit of the number. We can achieve this in Greenfoot by using random numbers: The Greenfoot environment itself has a method to give us a random number. Concept: When a method we wish to call is not in our own class or inherited, we need to specify the class or object that has the method before the method name, followed by a dot. Crabs don t always go in an exact straight line, so let us add a little random behavior: The crab should go roughly straight, but every now and then it should turn a little off course. But when it walks, it always walks exactly straight. 3.1 Adding random behavior In our current implementation, the crab can walk across the screen, and it can turn at the edge of our world. The first thing we shall look at is adding some random behavior. Adding code will get a little easier from now on, since we have seen many of the fundamental concepts. Now, we will add more interesting behavior. ![]() There were many new things that we had to look at. GetWorld().1 3 Improving the Crab more sophisticated programming topics: concepts: random behavior, keyboard control, sound dot notation, random numbers, defining methods, comments In the previous chapter, we looked at the basics of starting to program our first game. We know that buttonType > 0 (a song button) Here, the mouse was clicked, and it wasn't the pause If (buttonType = -2) // i.e., the stop button was pressed We're a playing a song that is NOT paused Here, we pressed the pause button and the song If (buttonType = -1) // i.e., was the pause button clicked? If we are here, the mouse button was clicked These occurrences will be located in the code of the control buttons when referring to the current selection. Then, you will need to make the appropriate changes within the code (like changing the appropriate occurences of 'buttonType' to 'songButton.buttonType' and change the appropriate 'gfs' occurrences to 'songButton.gfs', etc.). So, you will be removing the 'songPlaying' field altogether and replace it with a 'private static Button songButton' field. To make them accessible, however, either lines 8 and 9 need to be made 'public' instead of 'private' or public getter method will need to be added to the class to allow access to their values. This would be what is needed since you apparently need access to both those values. By holding the song button in a class field, its 'buttonType' and its 'gfs' will be available. What is left? - how about the Button itself. ![]() If you save the GreenfootSound object, you will not keep the song number. Maybe that field should be replaced with something a little more useful. ![]() A problem with acquiring that particular song is found when you realize that only the number of the song is available in the 'songPlaying' class field. So, calling 'gfs.stop()' or 'gfs.pause()' (see lines 43 and ? wherever it disappeared to ?) will not call the stop and pause methods on the song that is currently playing. GetWorld().showText("Starting song " + buttonType + " named " + song, 250, 380) Įach button created in your project is getting a 'gfs' field of its own - even the 'play', 'pause' and 'stop' buttons. GetWorld().showText("Button type: " + buttonType + ", song name: '" + song + "'", 250, 320) IsMouseClicked = Greenfoot.mouseClicked(this) Public Button(String imageName, String songName, int typeOfButton) ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |