This site requires JavaScript, please enable it in your browser!
Greenfoot back
Gingervitis
Gingervitis wrote ...

2013/2/15

World Question.

1
2
Gingervitis Gingervitis

2013/2/15

#
Is it possible for me to have 1 class have a different function for each world it is in? For example, when I start making Turtle Adventure 2, I want to have less classes in the game so I can have more stuff.
danpost danpost

2013/2/15

#
Yes it is. The initial state of the object will have to be determined in the 'addedToWorld(World world)' method; and both this method and the 'act' method will need to determine which world it is in to do whatever it needs to do for that world:
if (world instanceof WorldOne) { ... }
if(world instanceof WorldTwo) { ... }
// etc.
The 'addedToWorld' method would have the different constructions performed in it and the 'act' method would have the different actions performed in it.
Gingervitis Gingervitis

2013/2/15

#
How exactly does the addedToWorld method work?
danpost danpost

2013/2/15

#
The 'addedToWorld' method is an Actor class method that is empty by default (it does nothing until you override it). It is called by the Greenfoot system (you do not programmatically call it yourself) when the object is placed into the world with the World class 'addObject' method. Note: it is not executed when the object is created, but when placed in the world. Greenfoot supplied this method so that any object initialization that could not be accomplished before it was in the world (such as location tweaking, adding other objects, initialization of states that could not be carried out prior to the object being in the world) could be executed. Let us say you wanted a class that could be a turtle, a bird, or a fish and your worlds are named 'Land', 'Air', and 'Sea'. You could have a class like this:
import greenfoot.*;

public class Morph extends Actor
{
    private int speed;

    public Morph()
    {
    }

    public void addedToWorld(World world)
    {
        if (world instanceof Land)
        {
            setImage("turtle.png");
            speed=1;
        }
        if (world instanceof Air)
        {
            setImage("bird.png");
            speed=5;
        }
        if (world instanceof Sea)
        {
            setImage("fish.png");
            speed=3;
        }
    }

    public void act()
    {
        if (world instanceof Land)
        {
            // actions for turtle here
        }
       // etc.
     }

    // any other methods for this class
}
Gingervitis Gingervitis

2013/2/15

#
Ohhhh ok. I understand now. I have another question. Is there a website that show examples on how to use specific methods? I don't know how to use all the methods and I need to see examples to understand how it fully works.
danpost danpost

2013/2/15

#
There are very, very few people (I would think ... none) that know all the methods available (there are literally thousands of them). The basic workings of a method is set, however; and understanding how this works is the important thing. Also, knowing where to look for a (possible) method is helpful. For information on methods and how they work, refer to Learning the Java Language. For listing of methods for each class in the greenfoot package, refer to the Greenfoot API documentation. For listing of methods supplied by Java, refer to the Java API documentation. To be totally overwhelmed refer to Java's method index.
Gingervitis Gingervitis

2013/2/15

#
I didn't mean ALL of them. It is impossible to know all of them. I should have been more clear. I meant the Greenfoot methods. I look at them frequently, but because I am not a computer programmer I don't understand how to actually use the method just by looking at the Greenfoot API documentation. That is why I asked about a website that shows examples. I actually started looking at the Learning the Jave Language today. You had told me about that before.
danpost danpost

2013/2/15

#
Please give an example of what you mean. Like, name a class and a method from that class that you have trouble understanding.
Gingervitis Gingervitis

2013/2/15

#
I'll only name 1 for now because there are a lot that I never used before. In the actor class getNeighbours. I don't understand how the parameters would work.
danpost danpost

2013/2/16

#
Greenfoot API - Actor class wrote...
getNeighbours protected java.util.List getNeighbours(int distance, boolean diagonal, java.lang.Class cls) Return the neighbours to this object within a given distance. This method considers only logical location, ignoring extent of the image. Thus, it is most useful in scenarios where objects are contained in a single cell. All cells that can be reached in the number of steps given in 'distance' from this object are considered. Steps may be only in the four main directions, or may include diagonal steps, depending on the 'diagonal' parameter. Thus, a distance/diagonal specification of (1,false) will inspect four cells, (1,true) will inspect eight cells. Parameters: distance - Distance (in cells) in which to look for other objects. diagonal - If true, include diagonal steps. cls - Class of objects to look for (passing 'null' will find all objects). Returns: A list of all neighbours found.
How the parameters work: using 'getNeighbours(1, false, Rock.class)' The cls parameter determines what class of object to search for. The given usage would only add neighbouring Rock objects to the returned list. The diagonal parameter determines whether the search area is circular (false; distance is radius) or square (true; distance is half the length of one side). The distance parameter determines how far from the cell (getX(), getY()) to look. The given usage would only look at the four cells located at (getX()+1, getY()), (getX(), getY()+1), (getX()-1, getY()), and (getX(), getY()-1). If the diagonal parameter was set to true, then all eight cells surrounding (getX(), getY()) would be checked for a Rock object.
Gingervitis Gingervitis

2013/2/16

#
Could you please post an example? Everything will make more sense when I see an example.
danpost danpost

2013/2/16

#
If you were programming a Checkers scenario, you might use 'getNeighbours' to determine all possible jumps for a 'kinged' man. Using 'getNeighbours(1, true, Man.class)' would return a list of all Man objects in jumping range (from here determining if the landing square is empty or even if the object being jump belongs to the opponent needs to be dealt with). If you were programming a simple horizontal-vertical jumping game (that you might see at a table in a restaurant, used to pass time as you wait), you might use 'getNeighbous(1, false, Peg.class)' to get a list of surrounding pegs that could possibly be jumped (again, determining if the landing hole is empty needs dealt with). If you were programming a chess game, you might use 'getNeighbours(1, true, Piece.class)' to check for any possible captures for the player's King (determining if the piece belongs to the opponent needs dealt with). I have programmed some chess scenarios, as well as others that I had opportunity to use this method. However, I have only found one instance where the method actually comes in handy. That is with the colliding of same-sized orbs (such as billiard balls), where 'getNeighbours(getImage().getWidth(), true, Ball.class)' could detect whether two balls are in contact with each other or not.
if (speed>0) speed--; // slow the ball down
if (!getNeighbours(getImage().getWidth(), true, Ball.class).isEmpty()) collide();
where 'collide' will perform the exchanging of forces and determine the new directions and speeds for the two colliding balls.
Gingervitis Gingervitis

2013/2/16

#
I just used your example of the addedToWorld method in one of my classes, but I got an error.
public void addedToWorld(World world)
    {
        if (world instanceof Boss2)
        {
            if (getY()<370) setLocation(getY(), getX() + 1);
            //if (getX()<370) setLocation(getX()+1, getY());
        }
    }
    
    /**
     * Act - do whatever the SpaceTurtle2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      
            if (world instanceof Boss2)
            {
                 if (getY()<370) setLocation(getY(), getX() + 1);
                 //if (getX()<370) setLocation(getX()+1, getY());
          }       
        move(3);
        checkKeys();
        ifCanSeeArrow();
        ifCanSeeFactory();
        
    } 
Gingervitis Gingervitis

2013/2/16

#
Gingervitis wrote...
I just used your example of the addedToWorld method in one of my classes, but I got an error.
public void addedToWorld(World world)
    {
        if (world instanceof Boss2)
        {
            if (getY()<370) setLocation(getY(), getX() + 1);
            //if (getX()<370) setLocation(getX()+1, getY());
        }
    }
    
    /**
     * Act - do whatever the SpaceTurtle2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      
            if (world instanceof Boss2)
            {
                 if (getY()<370) setLocation(getY(), getX() + 1);
                 //if (getX()<370) setLocation(getX()+1, getY());
          }       
        move(3);
        checkKeys();
        ifCanSeeArrow();
        ifCanSeeFactory();
        
    } 
at line 17, it said 'world is not public in Greenfoot.Actor;cannot be accessed from outside package
Gingervitis Gingervitis

2013/2/16

#
Gingervitis wrote...
Gingervitis wrote...
I just used your example of the addedToWorld method in one of my classes, but I got an error.
public void addedToWorld(World world)
    {
        if (world instanceof Boss2)
        {
            if (getY()<370) setLocation(getY(), getX() + 1);
            //if (getX()<370) setLocation(getX()+1, getY());
        }
    }
    
    /**
     * Act - do whatever the SpaceTurtle2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      
            if (world instanceof Boss2)
            {
                 if (getY()<370) setLocation(getY(), getX() + 1);
                 //if (getX()<370) setLocation(getX()+1, getY());
          }       
        move(3);
        checkKeys();
        ifCanSeeArrow();
        ifCanSeeFactory();
        
    } 
at line 17, it said 'world is not public in Greenfoot.Actor;cannot be accessed from outside package'
There are more replies on the next page.
1
2