home *** CD-ROM | disk | FTP | other *** search
- Objective:
- Understand events and how they direct different sections of your
- program to run.
- Understand how to add state variables to your objects.
-
- Terms:
- Event: a user initiated action such as a mouse being clicked on a
- button which causes a message to be sent to the appropriate object.
-
- Action message: a message that notifies an object that an event
- has occured.
-
- Discussion:
- In traditional programming, programmers think of a program
- in which the program counter starts at the top of a pogram,
- (typically called the "main" section) and continues in a
- linear manner through the end of the program. In event based
- programming we think of a two dimensional array of "events" which
- send "action messages" to various sections of code which respond to
- these "events". The order of when each section of code is executed
- is not dependant on the order it appears in the file but on the order
- that these events occur.
-
- Method:
- Create a new application with Interface Builder.
- Drag two buttons on to the screen and label them "event 1"
- and "event 2".
- Create a new subclass of Object using the class editor. Add two
- actions "myEvent1:" and myEvent2:".
- Create an instance of that Object using the "Objects/New Custom Object"
- main menu selection.
- Connect the buttons to the events of the object by CONTROL/CLICKing
- from the buttons to the MyObject1 icon in the window created by
- the "Windows/Objects..." panel. Connect it to the icon titled
- "MyObject1". Connect the "event 1" button to the myEvent1 action
- and the "event 2" button to the "myEvent 2" button by using the
- inspector panel.
-
- Futher Suggestions:
- How would you go about initializing an internal state variable.
- Why wouldn't you put initialization code in a method done by an
- event?
- Answer: you would use a "new" or "newFrame" method. See module
- "initialization".
-
- Summary:
- You now have the ability to have any "buttons" control execution of
- any part of your objects code. The internal state variables of any
- objects you create can be changed directly by these actions.
-
-
-
-