Parameters and World-level methods - PowerPoint PPT Presentation

About This Presentation
Title:

Parameters and World-level methods

Description:

... flap its wings. Dragon.fly. Do together. dragon move forward 1 meter. dragon flap ... dragon flap its wings. In this example, we can write just one method and ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 13
Provided by: wand162
Category:

less

Transcript and Presenter's Notes

Title: Parameters and World-level methods


1
Parameters and World-level methods
  • Alice

2
Our Dragon world
  • The dragon must to take off and fly, to carry the
    princess

3
Storyboards
  • We can make the dragon takeOff and fly.

Dragon.takeOff Do together
dragon move up 1 meter dragon flap
its wings
Dragon.fly Do together dragon
move forward 1 meter dragon flap its
wings
Note that we would need very similar methods to
get the dragon to land (by flying down) or to fly
in other directions
4
A Better Solution
  • Multiple versions of very similar code seems a
    bit tedious. The only things that change is the
    direction the dragon is to fly.
  • A better solution is to write a more flexible
    method.

5
Parameters
  • Built-in methods provide flexibility by providing
    parameters such as distance and direction.
  • Parameters allow you to pass in values
    (arguments).
  • Example
  • Parameters direction, distance, duration
  • Arguments left, 0.05 revolutions, 0.5 seconds

6
Kinds of Parameters
  • Alice provides several kinds of parameters that
    can be used in your own methods.

7
The storyboard
In this example, we can write just one method
and use a parameter to specify which direction
the dragon is to fly.
  • Dragon.flyInAnyDirection
  • Parameter whichDirection
  • Do together
  • dragon move in whichDirection
  • dragon flap its wings

8
Demo
  • Concepts illustrated
  • Enter name and select the type of each parameter
  • whichDirection is a Direction parameter
  • A parameter acts as a placeholder in the
    instruction
  • Arguments are passed to the parameter in
    the call to the method

9
A Number parameter
Add a Number parameter to specify the distance
the dragon travels as it is flying. Note
that the call to the method must now include an
argument for the distance.
10
World-level methods
  • It is also possible to create world-level methods
  • These are used when there are references to
    multiple objects in the world

11
Example
  • Consider our dragon world
  • The dragon has taken off
  • Now it must fly to the princess.
  • Such a method will involve 2 objects the dragon
    and the princess

12
Demo Starting a new method
  • First, to associate the new method with the World
  • select the World tile in the Object Tree
  • select the methods tab in the details area
  • click on the "create new method" button
Write a Comment
User Comments (0)
About PowerShow.com