1 / 28

Event Driven Programming

Event Driven Programming. Chapter 5. Sequential Programming. Computer-Centric Program Runs as Programmer Intended Programmer Sets the Order of Action Programmer Controls the Program’s Flow. Event Driven Programming. User-Centric Computer User Determines the Order of Actions

Download Presentation

Event Driven Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Event Driven Programming Chapter 5

  2. Sequential Programming • Computer-Centric • Program Runs as Programmer Intended • Programmer Sets the Order of Action • Programmer Controls the Program’s Flow

  3. Event Driven Programming • User-Centric • Computer User Determines the Order of Actions • Programs are Interactive • Flow of Control is Determined at Runtime • User Clicks Mouse / Presses Key • Object in Scene Moves to create a Condition

  4. Event • Something that happens • Can be caused by computer or user • Mouse Click • Key Press • System Clock Tick • Can occur differently each time the program is executed • Events are WORLD Level

  5. Event Handling • Response to an event • Action or Sequence of Actions carried out when Event Occurs (Methods) • Events TRIGGER response • Programmer must think of all possible events • Plan a Response for Each (Event Handler)

  6. Linking • Event Handling Methods must be linked to the Specific Event Event Occurs which causes the Event Handling Method to execute

  7. Testing • Testing of Event Driven Programs is much more difficult • Programmer does not know flow of program • Flow of Program changes every time program is executed • User’s Actions are difficult to pre-determine • Use Incremental Development and Testing • Write a Method • Test it • Write a Method • Test it • Etc.

  8. Example Program • Create a “Whack A Mole” program. Use the whackAmoleBooth object from the amusement park gallery. • Pressing the “M” key makes the mole pop up • Clicking the mouse on the mole makes the bopper try to hit the mole

  9. Design Storyboards Event: Key Press Letter M Response: Make mole lift out of booth Mole says “Get me if you can!” Make mole go back into booth

  10. Design Storyboard Event: Mouse Click on Mole Object Response: Lift Bopper off Booth Move Bopper to Booth towards Mole Lift Bopper off Booth

  11. Create Methods • World Level or Class Level Methods? • Why • Create moleAppear method

  12. Linking Event to Method • Create New Event • When a Key is Typed

  13. Linking Events to Methods • Select Specific Key • M  Make Mole Appear

  14. Link Event to Method • Select Method to Link • wackAmoleBooth, moleAppears • TEST

  15. Create Method • Create bopMole method • Link to Mouse Click on Mole Event • Test

  16. Run the Program • Try to Whack The Mole • Click “M” to make moleAppear Event Handling Method execute • Mouse Click on Mole to make bopMole Event Handling Method execute

  17. Homework • Chapter 5 • 5-1 Exercises • 1  Flight Simulator • 4  Typing Tutor

  18. Events with Parameters • Remember: Parameters are how we send information to the methods • Customize Methods for different objects/values

  19. Create Scenes/Methods • Example: (Textbook page 125) A firetruck has been called to an emergency in a burning building. A person and a fire object has been placed on each floor. When each person is selected, the truck will need to extend its ladder so that each person can climb down to safety.

  20. Design • Event 1 – Click on 1st person • Responding Method – Save person on 1st floor • Event 2 – Click on 2nd person • Responding Method – Save person on 2nd floor • Event 3 – Click on 3rd person • Responding Method – Save person on 3rd floor

  21. Design 2 • Events: Click on Person • Responding Method: SavePerson Point the ladder at the correct floor (parameter – Which floor) Extend the ladder correct amount (parameter – How far) Specific person (parameter – Which person) slides down ladder to the firetruck Pull the ladder back in correct amount (parameter – How far)

  22. Create Scene • Burning Building (building) • Firetruck (vehicles) • 3 People – use whichever you want • RandomGirl1 • RandomGirl2 • RandomGuy2 • FireAnim – www.alice.org • Gallery • Save As name.a2c (not ZIP file) • Put in correct Alice gallery folder: Alice\Alice\Required\Gallery\

  23. Create Method as Before • World Method or Class Method? • Parameters: • whichFloor (object) • whichPerson (object) • howFar (number)

  24. SavePerson Method

  25. Link Method to Event

  26. Test the Program • Run the program • VERY Important when using parameters • Run several times • Number Parameters • Small Number • Large Number • Negative Number

  27. Complex Problems • Responses to events may involve multiple actions • Methods can call other methods • Parameters can be used when methods call methods

  28. Homework • 5-2 • Problems 12 & 14

More Related