Alice Community  

Go Back   Alice Community > General Discussion > The Lounge

Reply
 
Thread Tools Display Modes
Assign 3
Old
Stahelin18287
Junior Member
 
Status: Offline
Posts: 6
Join Date: Jul 2014
Location: Irving, TX
Default Assign 3 - 07-13-2014, 11:11 PM

Assignment 3: Chapter 2 - Methods and Data

This assignment was a little more difficult to complete than the previous due to the (method) variables and parameters as discussed in Chapter 2. I've struggled a little with setting the parameters in the (Try This) exercises towards the end of this chapter. That said, I will need to review again in the coming days in order to gain a better understanding of how to set variables and parameters.
   
Reply With Quote
assignment 3
Old
BUMPYEE
Junior Member
 
Status: Offline
Posts: 3
Join Date: Jul 2014
Default assignment 3 - 07-14-2014, 07:14 PM

same here i cant get him walk
   
Reply With Quote
Assign3
Old
mslizguinnwallace
Junior Member
 
Status: Offline
Posts: 7
Join Date: Jul 2014
Location: North Garland, TX
Default Assign3 - 07-16-2014, 12:57 AM

I feel so much better now that I've learned from reading posts that I am not alone. I don't get how to create custom method or how to incorporate into my program.

I wish someone could explain.
   
Reply With Quote
Old
chickentree
Super Moderator
 
Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
Default 07-18-2014, 02:35 PM

Quote:
Originally Posted by mslizguinnwallace View Post
I feel so much better now that I've learned from reading posts that I am not alone. I don't get how to create custom method or how to incorporate into my program.

I wish someone could explain.
Creating Methods:
Methods
  1. Have a name - User methods are named by the programmer (you) and should begin with a lower case letter.
  2. Optionally have parameters
    1. Parameters are "place holders" for data that is "passed in" each time the method is called.
    2. In general, any changes to the parameter while the method is executing do not effect the value passed in
  3. Optionally have their own variables
    1. Variables inside a method are used to hold information, typically information needed by the method.
    2. Method Variables are only available (valid) when the method is executing and should only be referenced directly in that method.
    3. If the value of a method's variable is to be used in another method, it should be passed into that other method as a parameter.
  4. Perform a group of one or more instructions
  5. Can effect the Alice World and objects in it.
  6. Do not answer questions or return information upon completion
  7. Are associate with the currently selected object or sub-object
This last point is important! it is possible to spend a lot of time writing a method and then not be able to find it or to accidentally delete it. For instance if when you created the method you had a bird selected in the object list instead of the word object. The method will work but you will not know where it went or why it is necessary to call bird1.flutterAround to get it to work.
There are some rules for deciding where to place a method that I will not go into right now because, for the most part, a method will work whether it is place in the world or in another object. Problems start happening when you want to save an enhanced object to use in another Alice movie.

So lets say I have a bird and I want it to flutter around several things during the movie. I select the bird in the object list and then click on the methods tab, and click the "Create new method" Clicking this will bring up a dialog asking for the name of the method. The name should be descriptive, in other words it should say what the method will do. In the present case I want to write a method to make my bird flutter around a given object (think humming bird) so I need a name for my method like "flutterAround" Clicking OK creates the method. Now think about what information is needed before one thing can flutter around another.
Tick - Tock Tick Tock ...
I need to know two things before I can start to make my bird flutter. First I need to know what object I am going to flutter around and second I need to know how far away I want the bird to be from the object it is going to flutterAround. Since the object and distance can change each time I call flutterAround I need to tell the method what object and how far away to be each time, this is where parameters come in!
In the method click on the "Create new parameter" button (right side of the method box) make sure to select Object as the type and give it a descriptive name. I called mine objCenter this tells me its an object that will be the center around which the bird will flutter. Click the "Create new parameter button" again, select the number type for the parameter and give it a name, something like "dist" (for distance) should work.
The method now looks like this:

bird1.flutterAround obj objCenter, 123 dist

The easiest way to proceed from here is to write code to make the bird flutter around some object - a tree or person or whatever. Once the bird is successfully fluttering around your tree, click on the objCenter parameter and drag it to each place that the tree object appears in your method.
Warning: If you do not do this your bird will flutter around the tree each time it is called regardless of what you fill in for the parameter!
Now drag the dist parameter into everywhere you used a number to specify how far away from the tree the bird should be. You should be done except for testing at this point.
In world.my first method drag bird.flutterAround into world.my first method and replace objCenter with the tree and dist with whatever distance you were using to flutter around the tree. Next try something else, for instance
bird1.flutterAround Alice, 0.5
Finally what happens if you put in different objects like the ground or the camera? What about numbers does it work with a number less than zero? What about 0 itself?


Mark Henwood
mhenwood@ieee.org
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2021, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.