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

2015/11/27

Combine classes?

phaeleh phaeleh

2015/11/27

#
Hey guys, so I have a grid, where my camel can walk on. Each part of the grid (overall 4) has one class. Does someone know, how I can combine these into one class? I guess I need to use setRotation. Does someone has any ideas? best regards, amy :-)
danpost danpost

2015/11/27

#
The images you currently have will be of no use after combining the four classes. At any rate, it seems excessive to have four images when NE is always paired with SW and NW is always paired with SE. Combining the pairs of images would also cut in half the number of grid actors in the world (there would only be two directions to deal with as well -- instead of four). It is possible to create the image needed programmatically and rotate the actor the image is set to either in a positive or a negative direction depending on what is required. You can create a class image by adding the following to the class:
// class image constants for length of line and size of end-point nodes
public static final int HYPOT = 100; // should be a multiple of 5
private static final int DIAM = 12; // best as a multiple of 4
// the class image reference
private static final GreenfootImage IMAGE;

// creating the class image
static
{
    IMAGE = new GreenfootImage(HYPOT+DIAM, DIAM);
    IMAGE.fillOval(0, 0, DIAM, DIAM); // first node
    IMAGE.fillOval(HYPOT, 0, DIAM, DIAM); // other node
    IMAGE.fillRect(DIAM, DIAM/2, HYPOT, 2); // the grid line
    IMAGE.setColor(java.awt.Color.WHITE);
    IMAGE.fillOval(DIAM/4, DIAM/4, DIAM/2, DIAM/2); // hole in first node
    IMAGE.fillOval(HYPOT+DIAM/4, DIAM/4, DIAM/2, DIAM/2); // other hole
};
Then, all the class needs now is an indicator as to which way to tilt the image (rotate the actor) -- in a positive direction ( '1' ) or a negative direction ( '-1' ). Using numeric values makes it easier as multiplying it by the turn in degrees gives the exact rotation the actor should take on. For example, if the turn was to be 37 degrees either way (from horizontal) and the indicator was named 'direction', then:
turn(direction*37);
would set the proper rotation for the actor ('setRotation' would work also). This direction indicator needs to be supplied to the class constructor by way of using a parameter. If the class was named 'Grid' (in place of 'GridSE', 'GridSW', 'GridNE' and 'GridNW'), then:
public Grid(int direction)
would allow you to create a Grid object with either 'new Grid(1)' or 'new Grid(-1)'. You will need to set the image of each actor created with the image created above with:
setImage(IMAGE);
within this constructor. To line up the Grid objects in your world, it is good to know how far apart horizontally and vertically that they need to be placed from one another. To keep the numbers as simple as possible, using an angle that produces triangles similar to a 3-4-5 triangle might be best. The smaller angle in a 3-4-5 triangle is about 37 degrees and simple algebra can be used to determine the horizontal and vertical lengths from the length of the hypotenuse (first two lines below). The world could then line up the Grid object similar to the following (this does not produce the set-up as shown above):
int h = Grid.HYPOT*4/5;
int v = Grid.HYPOT*3/5;
int baseX = 100;
int baseY = 150;
addObject(new Grid(-1), baseX, baseY);
addObject(new Grid(1), baseX, baseY+v);
addObject(new Grid(1), baseX+h, baseY);
addObject(new Grid(-1), baseX+h, baseY+v);
addObject(new Grid(-1), baseX+2*h, baseY);
addObject(new Grid(1), baseX+2*h, baseY+v);
addObject(new Grid(1), baseX+3*h, baseY+2*v);
phaeleh phaeleh

2015/11/29

#
danpost, thank you so much, I got it! :)
You need to login to post a reply.