CompSci 4Chap 5 Sec 1Oct 11, 2007Prof. Susan RodgerAnnouncements• Read Chapter 5 Sec 2 for next Tuesday• New groups today• Assignment 5 out– Part 1 and Part 2 Due Oct. 24• Test 1 back today• Today– Interactive programmingControl of Flow• Control of flow – how the sequence of actions in a program is controlled– What action happens first, second, third, ….• In movie-style programs (Chaps 1-4) the sequence of actions is determined by the programmer– Creating a storyboard design– Writing program methods to carry out the designed sequenceInteractive Animations• In interactive programs, the sequence of actions is determined at runtime, when the user provides input– Clicks the mouse– Presses a key on the keyboard• Other sources of input are possibleInteractive Games• In a video game where the user is guiding a spaceship, the sequence of actions …– Depends on what direction the user guides the ship– How fast the user presses the controls• Each time the program runs, user input may cause a different sequence of actions• Control of flow is “in the hands of the user”You Already Saw Events• Each time the user provides some sort of input, an event is generatedFrom AppendixWhen spacebar pressed,Bee turns aroundEvent Handlers• An event may– Trigger a response, or– Move objects into positions that create some condition (e.g. a collision) that triggers a response•An event handler is a method that is called to carry out the response.• When an event is linked to an event handler, a behavior is created.How does this effect your program?• Our goal is to write interactive programs.• The approach is the same as before, but the difference is now must be concerned with behaviors.– input from the user (events)– How objects respond to an event (event handler methods)Example• Build an air show flight simulator. The pilot (user) uses the biplane controls to perform acrobatic stunts.• Problem: How do we write program code to provide a guidance system that allows the user to be the pilot?Solution• Use keyboard input– “F” key to move the biplane forward– Spacebar to make the biplane do a barrel roll– Note: other keys could be chosen• Write event handler methods that respond to each key press• Storyboards (next slide) and DEMO building worldStoryboards• Since two keys are used, two events are possible – so two storyboards are needed• Each storyboard outlines and event handler– Responds to a particular eventEvent: Spacebar pressResponse:Do togetherroll biplane a full revolutionplay biplane engine soundEvent: F-key pressResponse:Do togethermove biplane forwardplay biplane engine soundbiplane.flyForward• Do not modify the length of the sound – use “as is”• Coordinate duration of move and play sound– Match duration of move to duration of soundEvents Editor - Linking• Each event handler method must be linked to an event1) Select “create new event”Then choose the type of event2) A template linking is createdEvents Editor – Linking (cont)3) Select type of key for event 4) Select event handler methodFinal result:More FunctionalityClasswork today• Create 4 buttons and a spider robot• Press green button and spider moves forward• Press red button and spider moves backward• Other two buttons?• Event for
View Full Document