Tuesday, March 30, 2010

Introduction to Programming: Starting Out With Alice: Chapter 5; Methods, Functions & More About Variables

Starting Out with Alice: A Visual Introduction to Programming

1. Writing Custom Class-Level Methods

Along with the primitive methods in Alice, there are custom methods, called class-level methods. If a method is not available to do what you want it to do, it is possible to build one of your own.

  1. Create an instance of the desired class
  2. Select the instance
  3. In the detail method tab click create new method
  4. Give it a name and click OK
  5. Create the instructions for the method in method editor

Using Trial-and-Error Experimentation in Method Design

There is no way to know what will work right when you create a method in Alice. You will need to test it each time by right clicking and from the methods menu run it to see if you get the desired results. If you do not then modify it again.

2. Saving an Object to a New Class

When you create a custom class-level method it only stays in the world that you created. If you wish to be able to use it again:

  1. Change the name of the object that you want the new class to have
  2. Right click on the object in the world view window, choose save object, add the name in the dialogue box and click OK.

To use it:

  1. Click file the import
  2. Locate the file then click import. An instance of it is now in your world.

Inheritance

In the example in the book we took a FlyingPtrodactyl class and added a flap wing object. The new class had all the old objects with it. It has inheritance, or it inherited the objects as well as added a new one. This is a way of reusing code once created.

Keep Class-Methods Independent of Other Objects

As you learn to make objects, make sure that when you make that they do not rely on the existence of other objects. If they do then trying to reuse them in other worlds will result in failure.

3. Stepwise Refinement

In writing an algorithm in pseudocode it is possible that one could write steps that code is not available for. In that case we would break down the steps and write them out. Since this could make the code harder to read an alternative would be to break them out of the main code and write them as methods. It is even possible that we would break parts of them out into other methods. This refining of the code is called stepwise refinement. It is breaking the code into smaller more manageable pieces.

4. Passing Arguments

Just like we have methods that require a parameter sent to it, we also can write methods that require passing a parameter. These passed values are called arguments. To create one, in the method editor click the create new parameter button and fill in what you need. This now becomes available to use as a variable in your method.

Object Parameters

It is possible to pass objects as a parameter. You would use the object type when you create a new parameter.

5. Using Class-Level Variables as Properties

Each object in Alice programs have properties, things that we can change. They are also referred to as class-level variables, variables that belong to an object and can be changed. To add one to an object, under the objects details property tab choose create new variable. Fill in the dialogue box and click OK.

6. Writing Class-Level Functions

Just as we can write our own methods for objects like the ones built in, we can also write our own function for an object to compliment the ones that it already has. Under the object's function tab click create new function and then follow the steps like we have before to create a function.

7. World-Level Methods and Variables

World-Level Methods

Just as we can break steps into functions that we can call over and over, we can break our big world method into several smaller methods that can be called to do the jobs needed. This allows for code reuse.

World-Level Variables

By selecting the world object we can go to its property tab and create a new variable. Since this is a world variable it will be available to the whole program.

8. Using Clipboards

In Alice a clipboard is an icon that you can drag instructions into. Then when needed you can drag the clipboard onto the area you need those instructions. There is only one clipboard by default but it can be modified by the preferences off the edit menu.

9. Tips for Visual Effects and Animation

Billboards

A billboard is an image (jpeg, gif, or tif) that is imported into a world. It has height and width but no depth. Like any object it will have properties and methods you can use. To add a billboard, click the file then make billboard and look for the file that you want to use.

Fog

In an Alice world, the world objects fogStyle allows you adjust the density of fog from 0 (off) on up. The higher the number the more of a mist will appear and as objects move away in the world they disappear into the cloud.

Moving Objects Together with the Vehicles Property

If we need to objects to move at the same time we can use the vehicle property. By making one object the vehicle for another object, when we move the first object the second object will move along with it.

Making an Object Circle around another Object

By using an object's turn method we can rotate it around its center point. If we want an object to circle around another object we would use the asSeenBy argument (under the more), so that the object will circle the other.

Circling an Invisible Object

If we just want an object go around in a circle, we can have it circle another object that has had its isShowing property set to false (making it invisible).

Capturing Poses

After using the scene editor to move an object into position we want, we can then click the capture pose button on its properties. This will allow us to use the set pose method to bring the object back to that state.

Programming the Camera

Just like anything else in Alice, the camera (the view we see) is an object. We can use its methods to circle around things, move to things, and point at other objects as well as many other things.

Creating Dummy Objects for the Camera

A dummy object is an invisible reference point for the camera. By creating several of these we can use set point of view method and have it use one of these dummy objects.

Beginning Programming For Dummies



No comments:

Post a Comment