Friday, 6 April 2012

Constants

To set the size of the sliding panel in terms of the number of icons visible, we use two constants.  One for Columns and One for Rows.

Constants are words which represent fixed values within the program.  When the program is compiled, the constant is simply replaced by the value by the compiler.

Unlike variables which are containers for values, constants are fixed and can only change by editing the source.

To start with, we will have four columns and three rows.  The constants are defined at the very start of the program with the following lines;

#constant PANEL_ROWS 3
#constant PANEL_COLS 4

It is a long established standard to use all capitals for constants so that they can easily be recognised as such.  It is important that they not be confused with variables.

Constants should always be defined at the start of the program.

We can now use the columns constant to set the size of our icons based on the width of the screen.  Obviously this must be done once we know the size of the screen.

iconSize# = physWidth# / PANEL_COLS

The result is stored in a variable and again uses the # symbol to set it to be a float.  We can't use a constant for this because the size of the screen will not be known at the time the program is compiled, which is when constants are evaluated.

The icon size is then used to set the height of the panel (the width will be the same as the screen width)

panelHeight# = iconSize# * PANEL_ROWS

Finally we need a variable to keep track of whether the panel is open or closed.

panelOpen = 0

This will only ever be 0 or 1 so an integer is used ( no # at the end of the name) 

The complete start up code now looks like this
Sadly this does not yet produce anything visible.  That comes next

No comments:

Post a Comment