In alice what is the difference between a method and a function




















The ultimate goal in that block of code is to write the last statement in the block of code, which is a call to the coach's say method with the single String parameter having been constructed by embedded calls to the functions listed above. This code could have been simplified somewhat through the use of local variables, but we haven't studied that topic yet. This is a case where Alice's drag and drop programming paradigm tends to get into the way.

If this were a typical programming language where you simply type in the code, all I would need to tell you in order to explain how to write the code would be to tell you to copy the last statement in the block.

I would then need to explain how the different parts of the statement behave. The order in which you typed the characters to create the statement wouldn't matter as long as you got the statement typed correctly in the end. However, with the drag and drop paradigm, you must construct the statement by dragging in the component parts of the statement in a very specific order, and creating placeholders a couple of times along the way.

This makes it much more difficult to explain. That is why I am showing you four different versions of the call to the say method in Listing 1. Hopefully, you can analyze the comments and the differences between the statements and can understand what is required to construct the statement in terms of drag and drop procedures.

I invite you to replicate the code in Listing 1, run the program, and observe the results. Then experiment with the code, making changes and observing the results of those changes. In this program, I will show you how to write a function that returns a value.

In particular, the world that I will use in this program contains a coach and a chicken as shown in Figure 4. We will write a new function for the coach object that make it possible for code in the main method to send a message to the coach asking it how far it is from the chicken.

The coach object will get and return that value, which will be printed by the main method in the large white space at the bottom of the World Running screen as shown in Figure 4. Figure 4. The coach and the chicken in program named Aliceb.

Begin by creating a new world containing a coach and a chicken as shown in Figure 4. Then select coach in the object tree, and open the functions tab below the object tree.

Click the button labeled create new function in the functions tab. This will produce a dialog similar to that shown in Figure 5. Figure 5. Function creation dialog in program named Aliceb. Enter the name of the new function in the text field at the top of the dialog and click the Number button to specify that the function will return a value of type Number. Leave everything else as it is in Figure 5 and click the OK button.

This will create a new tab in the edit pane. A partial screen shot of the new tab is shown in Figure 6. Figure 6. Function edit tab in program named Aliceb. As you can see, the name of the new function is shown in the top of the tab. Immediately below that is the signature for the new function. Although you can't see it in Figure 6, there is a curly brace at the bottom of the edit pane that matches the one at the end of the function signature.

Everything between that pair of matching curly braces constitutes the body of the function. Initially, that body is empty except for a return statement that will, by default, return the value 1. We don't want this function to return the value 1 when it is called.

Rather, we want it to return a value that is the distance between the center of the coach and the center of the chicken.

One of the functions that is provided by the coach object can be called to provide that information. Open the functions tab for the coach object. Drag the function named distanceTo from the functions tab and drop it on top of the 1 that is showing as the return value in Figure 6. This should result in source code for the function named getDistanceToChicken that matches the source code for that function in Listing 2. Admittedly, the body of this function is very simple. However, now you know how to define a new function for an object regardless of how simple or how complex the body of the function may be.

Just follow the steps outlined above and fill in the code in the body of the function to cause it to behave the way you want it to behave and to return the value that you want it to return. The code in the main method in Listing 2 calls the getDistanceToChicken function on the coach and prints the results.

As was the case in the earlier program in this lesson, it is necessary to convert the numeric result returned from the function to type String and to concatenate it to the other text in the printed output. The procedure used to accomplish this is the same in both programs. When programming in Alice, you will frequently need to convert numeric values to type String for various purposes. I recommend that you memorize this procedure so that you will have it handy in your brain whenever you need it.

The output produced by the print statement in the main method is shown in the white area at the bottom of Figure 4. Both programs that I explained in this lesson are available for downloading see Resources. I encourage you to either download those programs, or copy the code from Listing 1 and Listing 2 into your Alice development environment and play it. Experiment with it, making changes, and observing the results of your changes. In this lesson, I taught you about functions that return values in Alice.

I taught you how to use existing functions. Returns the paint value of this object. Returns the opacity value in the range of 0. Returns a link to another object in the scene that is serving as the vehicle for this object. Almost all 3D model classes in the Gallery have a system of internal joints. The joints can be thought of as the pivot points of sub-parts of the object and can be used in the Scene editor to position sub-parts during scene setup. Procedures that can be used to animate joints are shown.

Procedural methods for an object's internal joints. These procedures perform the same actions that were described for the entire object, but the pivot point is at the joint. For example, a statement can be created to tell the alien to turn its right shoulder joint backward , as shown. As the right shoulder joint turns, the right upper arm, lower arm, and hand also turn. That is, the arm parts are attached to the body through the shoulder joint.

For this reason, the arms parts turn when the joint turns. A statement to turn the alien's right shoulder joint. Notice that the procedures in Figure A. In other words, a joint and its attached sub-part s cannot be separated from the body. The only unique procedure for joints is setPivotVisible , as described in this table. Procedure specific to internal joints. Displays the pivot position and orientation of this joint in the animation if the argument is true, hides the pivot position and orientation of this joint in the animation if the argument is false.

Almost all functional methods for an entire object are functions that access return a link to one of the joints belonging to that object.

However, there are only a few functions that can be called on an individual joint, as shown. Functional methods for a joint. The available functional methods have the same name and perform the same actions as the functions of the same name for the entire object.

Refer back to Table A. The only function that is unique to joints is the isPivotVisible function, as summarized below. A unique function for internal joints. Return Type. Returns true if the pivot position and orientation of this joint in the animation is being displayed, or else returns false if the pivot position and orientation of this joint in the animation is not being displayed.

Refer to the table for descriptions of these specialized methods. Properties methods for internal joints. Every Alice project has a scene this that is an instance of the Scene class and contains two other standard objects: the ground or water surface an instance of the Ground class , and the camera an instance of the Camera class , as shown. Each of these objects has their own procedures, functions, and properties, as defined in their respective classes. The standard components of every Alice project.

The Scene class has a few procedures, functions and property methods that are exactly the same as in other classes, as shown in the next three figures.

See previous descriptions of these procedures, functions, and properties earlier in this section. Procedural methods in common with other classes. Functional methods in common with other classes. Properties methods in common with other classes. For this reason, a scene object has need of many special methods that perform unique operations for creating the scene and animating the characters in the story or game. Unique procedures that are used for setting up a scene and managing the animation are shown.

Unique procedural methods defined in Scene. The Alice environment automatically calls the performGeneratedSetUp, performCustomSetup, and initializeEventListeners procedures in order when the user clicks on the Run button. When performGeneratedSetUp is executed, these instructions are used by the Alice system to re-create the scene in the runtime window. The performCustomSetup procedure contains instructions that may have been written to adjust the scene in a way not available in the Scene editor.

The initializeEventListeners procedure contains instructions to start listeners for events such as key presses and mouse clicks while the animation is running. Specific events and listeners are described below in the Scene Listeners section.

Th following table provides further information regarding these unique procedural methods. Procedures for thi s scene. Allows the programmer to make adjustments to the starting scene; adjustments that could not be easily made in the Scene Editor.

Add program statements to this procedure as is done in any method in Alice. However, all statements here will be executed after the Run… button is clicked, but before the runtime window is displayed. When the Run… button is clicked, Alice inspects the scene built in the Scene Editor and generates the appropriate code necessary to display the scene created by the user in the runtime window.

NOTE: the programmer should not attempt to add or modify code in this procedure, as it is always rewritten whenever the Run… button is clicked. This procedure of the Scene class is the preferred location in an Alice project for the implementation of event listeners. When the Run… button is clicked, Alice inspects this procedure and generates the appropriate code necessary to implement the listeners for the project.

See section below on listener procedures. This is where an Alice animation starts, once the runtime window is displayed. Normally this is the method where the programmer creates program statements that control the overall execution of the animation. A possible exception is performCustomSetUp, as described above. Properties methods for Scene class. The setters and getters of the Scene class are used to adjust the sky color, the lighting, and the amount of fog in a scene as an animation program is running, as summarized in the next table.

These methods are useful for changing the appearance of the scene while the animation is being performed not for setting up the scene in the Scene editor. For example, to change the scene from a daytime to a nighttime setting, the color of the sky could be made darker and the light in the scene could be decreased.

Properties setters and getters for Scene class. Sets the color of the sky in this scene. Sets the color of the primary light source in this scene. Think of it as the color of sunlight in an outdoor scene.

Used to set the density of the fog in this scene by setting the density value in the range of values from 0. Sets the color of a secondary light source from above in this scene. Sets the color of a secondary light source from below in this scene.

Returns the color of the sky in this scene. Returns the color of the primary light source in this scene; tyou might think of it as the color of sunlight in an outdoor scene. Returns the value of the density of the fog in this scene by getting the density value with a range of values from 0. Returns the color of a secondary light source from above in this scene. Returns the color of a secondary light source from below in this scene.

Listeners are used for creating interactive programs, especially games. Interactive means that the user is expected to use the keyboard, mouse, or some other input device to control the actions that occur as the program is running. For example, a mouse-click on object listener will listen for a user to mouse-click on an object in the scene.

In Alice, to create an interactive program, a Listener object must be added to the scene. A listener object is added to the scene by calling an addListener procedure, where Listener is a targeted event.

For example, addDefaultModeManipulation creates a listener object that targets a mouse-click on any object in the scene and responds by allowing the user to drag that object around the scene while the animation is running. This figure shows a list of addListener procedural methods. The table summarizes details about the addListener methods, in terms of what event is targeted and how the listener responds.

Allows the use the mouse to reposition an object in the virtual world as a program is executing. Ctrl-click turns the object, shift-click raises and lowers the object. Able to differentiate between Letter, Number, and Arrow keys. The Ground class has only a limited number of procedural, functional, and property methods, all of which behave exactly the same as those defined by other classes. The following three figures show the methods for the Ground class. These methods were summarized previously.

Procedural methods for Ground class. Functional method for Ground class. Specialized property methods for Ground class. The camera has many procedural methods that behave exactly the same as those defined by other classes, as shown in in this figure and summarized previously. Camera's procedural methods in common with other classes. One of the procedural methods shown above in Figure A. Unique Procedural method for Camera class.

Animates the reposition and reorientation of the camera from its current position to the vantage point of the entity. To return the average we calculate, just drag and drop the average parameter found at the top of the function onto the return value. Our new function now looks like this:. Our function looks good but we have to call it to and test it to make sure it is indeed calculating the average correctly. By default there is no code in the first method so let us add some to test our new function.

We need to create a few variables to do the testing. At a minimum, we need two numbers to send into the function and one number to assign the returned variable.

So let us create two numbers and call them testnumber1 and testnumber2. Next, we create the testaverage variable that will actually be holding our average returned from calling the calculateAverage function. Now we should see all of our variables for our first test for our function. Next, we need to call our new function in our first line of code. To do this we start by dragging and dropping our testaverage variable and then setting its value to the penguin.

We use the drop down arrows to do this by selection expressions and the actual function we want to use. Notice Alice assigned default values of 1 for our input parameters of number1 and number2. For our first test this might be fine as if it should display the average of 1 and 1 which should be 1.

To test this, let us add a print testaverage line by dragging and dropping the print function and assigning the testaverage variable. You use the drop down arrows to select my first method objects. We can go ahead and test this first set of data by clicking the Play button. The first test seems to yield the correct results. To do this just drag and drop the variables to the function call. Recall the box turns green when you are properly aligned with an object that can accept your substitution value.

After successfully, dropping these variables into the function call, our method in Alice looks like this. Notice testnumber1 and testnumber2 are now being used as the parameters sent to the calculateAverage function.

Now, we can use the drop down arrows for testnumber1 and testnumber2 to change to any numbers we want to test.

If we run this again by selecting the Play button our functions does indeed return out expected results of 6. You should experiment with other combinations of test numbers to convince yourself that the function is working properly. This way you could test many different combinations without having to change the code in Alice.

For example, look at this possibility:.



0コメント

  • 1000 / 1000