Events
description
Transcript of Events
![Page 1: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/1.jpg)
EventsChapter 7 Part 2
![Page 2: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/2.jpg)
While a Key is Pressed Event Specialized event An event occurs when you press a key and
continues until you take your finger off of the key
Choose “When a Key is Typed”, right click, choose “change to”
Event is called BDE
![Page 3: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/3.jpg)
BDE Begin- what is executed when key is
pressed During – what happens while key is kept
downo If key is released quickly During will not execute
End – what executes when the key is released
![Page 4: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/4.jpg)
Example While Key is Pressed BDE Tutorial 7-2
o Jump Jet moves up when space key is hito Jump Jet goes forward if key is held downo Jump Jet moves down when space key is released
Notice nothing is in world.animation
![Page 5: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/5.jpg)
Mouse Movement Let mouse move object
o Can move any object with a click and drag Let mouse move the camera
o Moves the camera location with click and drag Let mouse move orient the camera
o Rotates orientation with click and drag When Mouse is clicked on an object
o Same thing happens if mouse is clicked briefly or long
![Page 6: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/6.jpg)
When World is Running Will do method while world is active
o Continuously runs When World Starts only executes when world starts While is like an infinite loop since world is always
running Uses BDE
![Page 7: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/7.jpg)
When Condition Becomes True Does method when condition is true
Useful when object is moving and you want to see if it is close to another object
Example: Have person fall when 10 meters from drop zone
Choose “While something is true”, right click, choose change to
![Page 8: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/8.jpg)
Example Events in Game See IslandRescue Demonstrates
o How to show user instructions at beginning of game
When mouse is clicked on anything instructions disappear
o While the world is runningo While key is pressedo When condition is true
![Page 9: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/9.jpg)
IslandRescue Events
![Page 10: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/10.jpg)
IslandRescue Alice Methods
![Page 11: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/11.jpg)
Random Number
Number that is unpredictable o Uniformly distributed over range of numberso Not possible to predict where next number will
be Programs random numbers are used in
o Security for web applicationso Encryption for satellite transmissionso Gaming programso Scientific simulations
![Page 12: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/12.jpg)
Built-in Random Function
Alice provides World-level built-in functions for generating random numbers
In World object choose functions o Then random
![Page 13: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/13.jpg)
Random Number Function Returns a floating point value
o Fraction between 0 and 1 The integer only option
o True: only integers are useo False: floating point numbers are used
Change range of values by using the minimum and maximum parameters
![Page 14: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/14.jpg)
Demo Penguin moves forward random
distance
Concepts illustratedo Range of values can be changed with
minimum and maximum parameterso Integer only option
![Page 15: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/15.jpg)
Random Penguin Moves
![Page 16: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/16.jpg)
Random 3D Motion Penguin moved in only one direction In random 3D motion
o Object moves to random location in 3D Forward, backward, left and right, up and down
o Ex: goldfish below swims in random motion, fish can move in any direction except backward
![Page 17: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/17.jpg)
Six Possible Directions in 3D
Movement in six directions are possibleo forward, backward, left, right, up, down
Eliminate backward because goldfish don’t swim backward
To simplify code, we can use negative numberso Positive numbers move goldfish to left and upo Negative numbers move goldfish right and
down
![Page 18: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/18.jpg)
Algorithm Only three move instructions are needed
o up (will move down if random number is negative)o left (will move right if random number is negative)o forward (no backward motion)
Two parameters (min, max) define the fish’s range of motion to look like swimming
randomMotion
Parameters: min, max
Do together fish moves up a random distance fish moves left a random distance fish moves forward a random distance
![Page 19: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/19.jpg)
Goldfish Alice Program
![Page 20: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/20.jpg)
Demo
Concepts illustratedo 3D random movement accomplished by
three simultaneous move instructionso Minimum distance of move forward
instruction is 0 Goldfish always moves forward never bacward
o To call randomMotion method, min and max values are sent as arguments to parameters
![Page 21: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/21.jpg)
Debugging With Print
o Print statement allows you to See what values variables are during program
execution Print a text message to identify where program is
o Print statement is a diagnostic messageo Drag print from bottom of Method Editor
Causes a menu to appear asking if you want to Print a text message Print an object or variable under expression
![Page 22: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/22.jpg)
Output of Print Statement
Output is sent to bottom of world when running
Output of print statements
![Page 23: Events](https://reader035.fdocuments.us/reader035/viewer/2022062520/56815987550346895dc6c882/html5/thumbnails/23.jpg)
Homework Read chapter 6 sections 2 - 4 Answer questions in handout Due one week after assigned with 1 week
grace