alice-teacher global variables

Wanda Dann wpdann at andrew.cmu.edu
Wed Jun 4 14:14:25 EDT 2014


On 6/4/2014 9:19 AM, JOHNSON JULIE M. wrote:
>
> How do you create and use a global variable in Alice 3.1?
>
>
> Global variables are discouraged by most software engineers, due to 
> unwanted side-effects that may occur when multiple programmers are 
> working on the same software application.  However, there are some 
> cases in which a global variable is handy.  One example is in a game 
> program where you are tracking things like score, time, and 
> end-of-game conditions. These conditions often need to be checked 
> and/or updated in several different methods.  Alice 3 takes a 
> "middle-of-the-road" stance on this issue. On the one hand, Alice 3 
> discourages global variables by declaring variables as (1) local to a 
> specific method, or (2) private to a specific class.  However, the 
> Scene class is (in a sense) global in that all the objects are located 
> all the action takes place in a scene.  You can take advantage of this 
> by creating a property variable in the Scene class. This variable is 
> then "global" to all procedures and functions defined in the Scene 
> class. As an example, in the first screen capture below, a property 
> variable is declared in the Scene class to track the /score/ of a 
> game.  In the second screen capture below, the updateScore procedural 
> method increases the score by 2. We could also write other methods 
> that access the score... for whatever purpose.
>
declare score variable

update the score
>
>
> _______________________________________________
> alice-teachers mailing list
> alice-teachers at lists.andrew.cmu.edu
> https://lists.andrew.cmu.edu/mailman/listinfo/alice-teachers

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.andrew.cmu.edu/pipermail/alice-teachers/attachments/20140604/46cd5e68/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Score.jpg
Type: image/jpeg
Size: 85819 bytes
Desc: not available
Url : http://lists.andrew.cmu.edu/pipermail/alice-teachers/attachments/20140604/46cd5e68/attachment-0002.jpg 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Update.jpg
Type: image/jpeg
Size: 38243 bytes
Desc: not available
Url : http://lists.andrew.cmu.edu/pipermail/alice-teachers/attachments/20140604/46cd5e68/attachment-0003.jpg 


More information about the alice-teachers mailing list