190 likes | 354 Views
Events. Chapter 7 Part 2. While a Key is Pressed Event. Specialized event Choose “When a Key is Typed”, right click, choose “change to” An event occurs when you press a key and continues until you take your finger off of the key Event is called BDE. BDE.
E N D
Events Chapter 7 Part 2
While a Key is Pressed Event • Specialized event • Choose “When a Key is Typed”, right click, choose “change to” • An event occurs when you press a key and continues until you take your finger off of the key • Event is called BDE
BDE • Begin- what is executed when key is pressed • During – what happens while key is kept down • If key is released quickly During will not execute • End – what executes when you key is released
Example While Key is Pressed BDE • Tutorial 7-2 • Jump Jet moves up when space key is hit • Jump Jet goes forward if key is held down • Jump Jet moves down when space key is released Notice nothing is in world.animation
Mouse Movement • Let mouse move object • Can move any object with a click and drag • Let mouse move the camera • Moves the camera location with click and drag • Let mouse move orient the camera • Rotates orientation with click and drag • When Mouse is clicked on an object • Occurs when mouse is quickly clicked over object
When World is Running • Will do method while world is active • Continuously runs • When World Starts only starts when world starts • Uses BDE
When Condition Becomes True • Does method when condition is true • Good to do something when object is moving and you want to see if it is close to another object • Choose “While something is true”, right click, choose change to • Example: Have person fall when 10 meters from drop zone
Example Events in Game • See IslandRescue • Demonstrates • How to show user instructions with when mouse is clicked on anything • While the world is running • While key is pressed • When condition is true
Random Number • Unpredictable number • Uniformly distributed over range of numbers • Not possible to predict where next number will be • Used in certain kinds of computer programs • Security for web applications • Encryption for satellite transmissions • Gaming programs • Scientific simulations
Built-in Random Function • Alice provides World-level built-in functions for generating random numbers • In World object choose functions • Then random
Random Number Function • Returns a floating point value • Fraction between 0 and 1 • Different range of values can be obtained by using the minimumand maximum parameters • The integer only option • True allows selection of whole numbers • False floating point numbers are used
Demo • Penguin moves forward random distance • Ch06Lec3PenguinRandomMoves.a2w • Concepts illustrated • Range of values can be changed with minimumand maximum parameters • Integer only option
Random 3D Motion • Penguin moved in only one direction • Random 3D motion • Object moves to random location in 3D • Ex: goldfish below swims in random motion, fish can move in any direction
Six Possible Directions • Movement in six directions are possible • forward, backward, left, right, up, down • Eliminate backward because goldfish does not swim backward • To simplify code, we can use negative numbers • To move goldfish right and down
Storyboard • Only three move instructions are needed • up (will move down if random number is negative) • left (will move right if random number is negative) • forward (no backward motion) • Two parameters (min, max) will be used to restrict motion of fish to nearby location -- to look like swimming randomMotion Parameters:min, max Do together fish moves up a random number distance fish moves left a random number distance fish moves forward a random number distance
Demo • Ch06Lec3GoldfishRandom3DMotion • Concepts illustrated • 3D random movement accomplished by three simultaneous move instructions • Minimum distance of move forward instruction is 0 • Goldfish always moves forward • To call randomMotion method, min and max values are sent as arguments to parameters
Debugging With Print • Print statement allows you to • See what values variables are during program execution • Print a text message to identify where program is • Print statement is a diagnostic message • 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
Output of Print Statement • Output is sent to text console at bottom of world when running Output of print statements
Homework • Read chapter 6 sections 2 - 4 • Answer questions in handout • Do lab assignments after handing in answered questions • Due one week after assigned with 1 week grace