About Global Variables
Posted: Wed Sep 25, 2013 2:30 pm
Hello everyone,
I am back to programming after several years of inactivity since the glory years of HyperCard in the early 90's. I thought it would be easy to get back into it but I realise that I forgot a lot of it since and now I have to get back into it with baby steps. Therefore my questions might look very basic to you.
The project
I have created a small stack containing one filled circle in black and 2 buttons (+ and -). when the stack starts, the circle is set to a certain size and position. If the user click on the "+" button, the circle will grow in size and the other way around if the user click on the "-" button, the circle will reduce in size.
There are two level of scripts in this stack:
one at the stack level to declare global variables and set the circle to its starting size (see below):
global PWidth, PHeight
On OpenStack
put 84 into PWidth
put 84 into PHeight
set Width of graphic "Pion" to PWidth
set Height of graphic "Pion" to PHeight
end OpenStack
The global variables PWiidth and PHeight are used to set the Width and Height of the circle (I realize that this might be obvious for some of you)
There is also a script hidden in each button that triggers the size change and reposition the circle at the center of the stack (see below the + button script):
global PWidth, PHeight
on mouseUp
add 1 to PWidth
add 1 to PHeight
set Width of graphic "Pion" to PWidth
set Height of graphic "Pion" to PHeight
set location of graphic "Pion" to 66,66
end mouseUp
The Problem:
As it is written here, the stack works fine, but there is something I don't understand. It was my assumption that once global variables were declared at the stack level, they didn't have to be declared again at the page or object level. However, If I remove the line declaring PWidth and PHeight in the button script, nothing happens. It's like if the global variables had to be declared at all level for the script to work properly.
The other problem I have, is that every time I run the stack from live code in preview mode, The OpenStack handler is bypassed and the circle doesn't reinitialize.
Can anyone explain to me what I'm doing wrong here for both problems or if it's just the way it is?
Note: Please understand that this exercise is meant to be part of an introduction course. I'm aware that many of you could have written this script much more efficiently. The aim here is to keep the structure simple for teaching not to play script wiz. suggestions to write this better are welcome, but please do it separately after you have answered my questions. Those issues have to be solved anyways.
Keep in mind that writing this post was time consuming and that solving those two issues are my main priority.
Thank you all in advance.
Michel
I am back to programming after several years of inactivity since the glory years of HyperCard in the early 90's. I thought it would be easy to get back into it but I realise that I forgot a lot of it since and now I have to get back into it with baby steps. Therefore my questions might look very basic to you.
The project
I have created a small stack containing one filled circle in black and 2 buttons (+ and -). when the stack starts, the circle is set to a certain size and position. If the user click on the "+" button, the circle will grow in size and the other way around if the user click on the "-" button, the circle will reduce in size.
There are two level of scripts in this stack:
one at the stack level to declare global variables and set the circle to its starting size (see below):
global PWidth, PHeight
On OpenStack
put 84 into PWidth
put 84 into PHeight
set Width of graphic "Pion" to PWidth
set Height of graphic "Pion" to PHeight
end OpenStack
The global variables PWiidth and PHeight are used to set the Width and Height of the circle (I realize that this might be obvious for some of you)
There is also a script hidden in each button that triggers the size change and reposition the circle at the center of the stack (see below the + button script):
global PWidth, PHeight
on mouseUp
add 1 to PWidth
add 1 to PHeight
set Width of graphic "Pion" to PWidth
set Height of graphic "Pion" to PHeight
set location of graphic "Pion" to 66,66
end mouseUp
The Problem:
As it is written here, the stack works fine, but there is something I don't understand. It was my assumption that once global variables were declared at the stack level, they didn't have to be declared again at the page or object level. However, If I remove the line declaring PWidth and PHeight in the button script, nothing happens. It's like if the global variables had to be declared at all level for the script to work properly.
The other problem I have, is that every time I run the stack from live code in preview mode, The OpenStack handler is bypassed and the circle doesn't reinitialize.
Can anyone explain to me what I'm doing wrong here for both problems or if it's just the way it is?
Note: Please understand that this exercise is meant to be part of an introduction course. I'm aware that many of you could have written this script much more efficiently. The aim here is to keep the structure simple for teaching not to play script wiz. suggestions to write this better are welcome, but please do it separately after you have answered my questions. Those issues have to be solved anyways.
Keep in mind that writing this post was time consuming and that solving those two issues are my main priority.
Thank you all in advance.
Michel