CS 320 n Visual Programming Classlevel Methods and

  • Slides: 22
Download presentation
CS 320 n –Visual Programming Class-level Methods and Inheritance – Part 2 Mike Scott

CS 320 n –Visual Programming Class-level Methods and Inheritance – Part 2 Mike Scott (Slides 4 -3 -2) Thanks to Wanda Dann, Steve. Class Cooper, and Susan for slides. Visual Programming Level Methods - Part Rodger 2

What We Will Do Today • • Work on more class level methods Learn

What We Will Do Today • • Work on more class level methods Learn about built in functions Learn to use expressions Review the as. Seen. By option Visual Programming Class Level Methods - Part 2 2

Guidelines for Class-Level Methods • Should a method be a class-level method or a

Guidelines for Class-Level Methods • Should a method be a class-level method or a world-level method? • For class level methods – avoid hard coded references to other objects – avoid calls to world level methods – Play a sound only if has been imported and saved as part of the new class. (sounds can be imported to world or individual objects) • If these guidelines are not followed an instance of a class is added to another world, an error message is generated Visual Programming Class Level Methods - Part 2 3

Bad Example 1 A class level method should not call world-level methods. Visual Programming

Bad Example 1 A class level method should not call world-level methods. Visual Programming Class Level Methods - Part 2 4

Bad Example 2 Visual Programming Class Level Methods - Part 2 5

Bad Example 2 Visual Programming Class Level Methods - Part 2 5

But I want to skate around things • Is it reasonable to want a

But I want to skate around things • Is it reasonable to want a class-level method for the advanced skater where she skates around things? – In this particular case, a penguin? Visual Programming Class Level Methods - Part 2 6

Solution • A solution is to write a class-level method with an object parameter

Solution • A solution is to write a class-level method with an object parameter that allows you to pass in the specific object. clever. Skater. skate. Around Parameter: which. Object Do in order Do together clever. Skater turn to face which. Object clever. Skater lift right leg clever. Skater move to which. Object clever. Skater turn around which. Object Visual Programming Class Level Methods - Part 2 7

Parameterized Version of skate. Around • Making the object to skate around a parameter

Parameterized Version of skate. Around • Making the object to skate around a parameter now removes the tie to the penguin in this world calling skate. Around Visual Programming Class Level Methods - Part 2 8

Translating design into code • Most of the skate. Around storyboard design is straightforward

Translating design into code • Most of the skate. Around storyboard design is straightforward and easy to code. • The last two steps, however, require some thought: advanced. Skater move to target -What distance should the clever. Skater move? advanced. Skater turn around target -how do we tell clever. Skater to turn (in a circle) around another object? Visual Programming Class Level Methods - Part 2 9

The distance to move • We can work out how far to move the

The distance to move • We can work out how far to move the skater based on initial position of objects and a little experimentation • Pick a distance, play the movie, more or less, refine until it looks good • Is this a very general solution though? • only works for one initial set up • look to make it work for many set ups Visual Programming Class Level Methods - Part 2 10

Built-in Functions. (a. k. a. questions) • The built-in function, distance to can be

Built-in Functions. (a. k. a. questions) • The built-in function, distance to can be used to determine the distance the skater must move. Visual Programming Class Level Methods - Part 2 11

Calling the Function • drag and drop the function advanced. Skater distance to from

Calling the Function • drag and drop the function advanced. Skater distance to from the function menu to the distance portion of the move method and pick expressions -> target • call to move looks like this now Visual Programming Class Level Methods - Part 2 12

Problem? • What will happen when the skate. Around method is called? Visual Programming

Problem? • What will happen when the skate. Around method is called? Visual Programming Class Level Methods - Part 2 13

Too Much • Skater intersects penguin • told to move forward the distance to

Too Much • Skater intersects penguin • told to move forward the distance to the target • moves skater all the way so centers of objects coincide • distance measured center to center Visual Programming Class Level Methods - Part 2 14

Expressions • We don’t want the skater to move all the way to the

Expressions • We don’t want the skater to move all the way to the target • to avoid collision use math operator and create an expression that moves to within 2 meters • Math operators is Alice: +, -, *, / • Example: Visual Programming Class Level Methods - Part 2 15

Creating an Expression 1 3 2 4 Visual Programming Class Level Methods - Part

Creating an Expression 1 3 2 4 Visual Programming Class Level Methods - Part 2 16

More on Expressions • Creating expressions with the pop up menus can be a

More on Expressions • Creating expressions with the pop up menus can be a pain • In this case, text based programming languages may be easier to use Visual Programming Class Level Methods - Part 2 17

Result of Expression Stops before running over penguin And skates around What if already

Result of Expression Stops before running over penguin And skates around What if already less than 2 meters from an object? Visual Programming Class Level Methods - Part 2 18

as. Seen. By • To have the skater skate around another object, pass which.

as. Seen. By • To have the skater skate around another object, pass which. Object as an argument to the as. Seen. By parameter in a turn instruction. • yes, passing a parameter to another method as a parameter • For more details on as. Seen. By, review Tips & Techniques 2. Visual Programming Class Level Methods - Part 2 19

More on As. Seen. By • Use invisible objects or dummy objects (is. Showing

More on As. Seen. By • Use invisible objects or dummy objects (is. Showing property set to false) to have objects fly around in circles • Example: Have gunfighters circle each other Visual Programming Class Level Methods - Part 2 20

Circling Gunfighters Visual Programming Class Level Methods - Part 2 21

Circling Gunfighters Visual Programming Class Level Methods - Part 2 21

Testing • Each time you create a new class, test it: – Add an

Testing • Each time you create a new class, test it: – Add an instance of the new class to a new world. – Write a short testing program to test each new method. • Testing increases your confidence in the ability to reuse your code in other worlds. Visual Programming Class Level Methods - Part 2 22