Game Development Reference
lh]uanD]hbSe`pd and lh]uanD]hbDaecdp anywhere else in your program, the compiler will give you
an error message:
Access of undefined property ***
All the variables that you used in the programs until now are known as instance variables . They are
declared directly within the class definition (at the top of the program) and can be used anywhere
in the class by any method. Local variables can be used only inside the method in which they are
One thing you'll notice about lh]uanD]hbSe`pd and lh]uanD]hbDaecdp is that you declared them as
qejp variable types and assigned them their initial value in the same directive. So instead of writing
two directives, like this:
you combined them into one directive, like this:
This is a perfectly valid way to create variables, and you can create any instance variable this same
way. By convention, however, instance variables separate these steps into two directives because if you
make an instance of a class, you'll often want to assign a variable's value only later in the program.
Don't worry if this doesn't make any sense to you now; it will a bit a later in the topic when you look
at more practical examples.
One advantage you have by assigning a value to lh]uanD]hbSe`pd and lh]uanD]hbDaecdp variables
directly in the kjAjpanBn]ia event handler is that if the lh]uan object's width or height ever changes
in the game, the values of those variables will immediately be updated. Suppose that I designed a
game in which my pig character eats a magical daisy that makes him double in size. Because the
kjAjpanBn]ia method is updated 30 times per second, the size change will affect any calculations
involving the size of the lh]uan, such as figuring out screen boundaries or collisions with other objects.
Now that you know the lh]uan object's half-width and half-height, you can use this information to
confine it more precisely within the stage's boundaries with these eb+ahoa statements: