alice-teacher s Digest, Vol 32, Issue 8 - World variables and local parameters
Robert Durtschi
robert.durtschi at gcsu.edu
Fri Mar 15 10:19:09 EDT 2013
>>Does anybody knows how can I pass a value of some variable to a private method of another object?
For example, I have a variable read inside a method of an object, and then I want to make some calculation using that value but only when I click over another object.<<
Probably the easiest way is to create a World variable then all objects in that world should be able to "See" the variable.
the method that reads can update the World variable by dragging the world.variable into the method i.e.
world.global_variable set value to bunny.local_variable
and then have the calculate method use the global variable from the "expressions" list:
bunny2.local_variable set value to world.global_variable + 2
>>Another question... what is the objective of using a parameter instead of using a variable?
I thought a parameter in Alice was like a global variable, but it don't seems to be.<<
Quite the opposite. A parameter is used to modify the behavior of a method and is only visible to the method and the object that calls (invokes) the method.
For instance, I use the "Hello World" project from chapter 1 of "An Introduction to Programming using Alice 2.2" by Herbert, which has a bunny move forward, turn to face the camera and say "Hello World". We then extend it by "teaching" the bunny the "hop" method. Then we extend hop by adding height and length parameters to "tell bunny how high and how far to hop":
bunny.hop ( [123] height, [123] length)
Do together
bunny move up height meters
bunny move forward ( ( length / 2 ) )
Do together
bunny move down height meters
bunny move forward ( ( length / 2 ) )
So the bunny moves up the height specified and forward half the length then moves down that height and forward the other half of the length.
This assumes Alice 2.3, if you are using Alice 3.2 then the principle is the same (as it is for Ada, BASIC, C, C++, FORTRAN, etc.) but the actual code will be slightly different.
If you would like to email me a more detailed explanation of what you are trying to do and attach your .a2w file I will look at it and try to update it so it does what you specify. I can be reached at robert.durtschi at gcsu.edu
(warning, at least on a PC a downloaded .a2w files tries to turn into a .zip file (which underneath it is) and you will need to do a "save as" chose "all file types" and change the extension back to a2w, before Alice can open it.)
Bob Durtschi
More information about the alice-teachers
mailing list