As you come in… DOWNLOADS FOR TODAY: CarGameTeacherStarter.a2w Online student textbook (at bottom of each page below) Module 9.4 SpaceShipGame.a2w
Today’s plan Functions Compound boolean expressions and if statements – can solve same problem multiple ways Or – car magnet And – space ship take off Methods and Parameters
Functions: Calculate a Value and “Return” it. Hides away complex calculation When program executes that line/tile: change of control flow! Jump to code/function that does the calculation It “returns” the calculated value, which replaces the function call tile Finally, that line/tile is executed
Examples you know (but can’t see/edit the code for) Object functions Some calculate numbers Distance To Height Some calculate boolean values Is within X of Y Is above
Examples you know (but can’t see/edit the code for) World functions Some calculate numbers Random number Some calculate boolean values Both a and b Either a or b or both a == b a > b
Compound Boolean Expressions – OR Car Driving Game (watch video 9.2) Modify: Car should only turn to head towards MAGNETS One of the 4 magnets Download from ce21sandiego.org/2013 CarGameTeacherStater.a2w Let’s create a function to hide away complex boolean expression
Create function: isMagnet Returns: boolean value Create new World function Choose returns boolean Call it isMagnet (we often use “is” for boolean returning functions – Yes or No, true or false) Drag boolean expression in if to clipboard Go to isMagnet function definition replace “true” with expression from clipboard BACK in my first method Drag complex expression to trash Replace (default) true with new isMagnet function (look under World, functions – it’s at top)
Compound Boolean Expressions – OR Car Driving Game (watch video 9.2) In online text – used to show Two ways you could make this if… Compound boolean expression
Compound Boolean Expressions – OR Car Driving Game Two ways if…
Compound Boolean Expressions – OR Car Driving Game Two ways if… nested if
Compound Boolean Expressions – AND Spaceship takes off Space ship only takes off if all spheres are blue Download from online textbook module 9.4 SpaceShipGame.a2w (bottom of page) Click on World object Methods – edit Flip Switch Build this if statement When true – make dropShip object move up 30 meters
Compound Boolean Expressions – AND Spaceship takes off Two ways if…
Compound Boolean Expressions – AND Spaceship takes off Two ways if…
Coming Up… Week 7: Methods, parameters, and events Week 8: Lists