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

2014/10/17

possible to combine objects by glueing them together?

11770 11770

2014/10/17

#
so i have a cube object that i made as a base for a wall but the wall is a bunch of different shapes, so i was wondering if i could stick them end to end without doing it manually as there's two of each shape in the mapped area.
Alwin_Gerrits Alwin_Gerrits

2014/10/17

#
with without doing it manually do you mean without having to put in every object as a new object or do you mean you don't want to have to make an image of the collection of shapes with a differend program? Because the way I see it you might just want to try making a small code for it you can call upon. Maybe a class called 'wall' which asks for an x and an y-coördinate then simply add the objects from that position on out? Lets say for example you want to make a wall with a cube, then a triangle and then a circle you could make them objects and use something like this:
1
2
3
4
5
6
7
8
9
10
11
12
public void wall(int x, int y)
{
  World.addObject(new cube(), x, y);
 
  y= y-cube.getHeight();
 
  World.addObject(new tirangle(), x, y);
 
  y= y-triangle.getHeight();
 
  World.addObject(new circle(), x, y);
}
anyway, this would be a quick fix, but if this is what you meant by 'manually' then I'm affraid I'm to inexperienced myself to give you a better solution. In which case somebody is bound to come up with a better solution. Anyway, I'm not sure this code would work perfectly if you simply copy-pasted it and made the objects (cube, triangle, circle). But I guess making the code for you would not be the right thing to do anyway. Hope this helped nevertheless.
Super_Hippo Super_Hippo

2014/10/18

#
It wouldn't work as you suggested it. What you tried to do would look like this:
1
2
3
4
5
6
7
8
9
10
11
public void wall(int x, int y)
{
  Cube cube = new Cube();
  getWorld().addObject(cube, x, y);
  y-=cube.getImage().getHeight();
  Triangle triangle = new Triangle();
  getWorld().addObject(triangle, x, y);
  y-=triangle.getImage().getHeight();
  Circle circle = new Circle();
  getWorld().addObject(circle, x, y);
}
What you are doing there is, you always subtract the height of the image of the actor which was just placed to place the next actor above the last one. Actually you should subtract the half of the image of this actor and the half of the image which the next actor image has. @11770: What exactly are you trying to do? Do you want to have them bounded, so if one 'moves', then the other will follow or do you just want to create a wall once and you don't want to place the shapes with right-clicking the class?
11770 11770

2014/10/27

#
the idea was to create a wall a boundary that the main object couldn't go through i'm making pacman for a class project and as theres different shaped walls I wanted to stick these tiny 32-32 blocks together and just place the 1 object atm I have roughly 200 of them placed and it took me forever the first 100 were done with manually placing and saving the world, the others were placed using an array.
danpost danpost

2014/10/27

#
An array is the way to go; however, what type of value is stored in the array would make a big difference as to how easy or difficult it would be to create the array. Probably, the best type of value to use would be character strings and the second best would be ints. One value or character would be used as a code for one type of object (or wall). Once the array (or double-array, if using ints) is created, you can use a loop (or two) to process the data in the array.
11770 11770

2014/10/27

#
1
2
private int [] xLocations = {491, 507, 523, 539, 555, 571,...
     private int [] yLocations = {402, 402, 402, 402, 402, 402,...
this is what I have atm obv I shortened it for space as the line is quite long ie the
1
2
3
4
5
6
7
8
9
10
11
private void makeWalls()
{int q = 0;
    while(q <75)
    {
        addObject(new Wall(),xLocations[q],yLocations[q]);
        q = q + 1;
 
 
 
 
    }
Super_Hippo Super_Hippo

2014/10/27

#
What danpost probably meant was that you have your map saved in an array, so every different character (or int) is displaying a different kind of object. Every "grid" is 16x16 in your example. This will be translated later. The array could look like this:
1
2
3
4
5
6
7
8
9
private int[][] map ={
{1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,0,0,1},
{1,0,1,0,0,0,1,0,1},
{1,0,1,1,1,1,1,0,1},
{1,0,1,0,0,0,1,0,1},
{1,0,1,0,1,0,1,0,1},
{1,0,0,0,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1}};
Every 1 would represent a wall for example.
danpost danpost

2014/10/27

#
Instead of putting the coordinates in the arrays, you want something that represents the different type of walls (or objects) that you need at each location. Use a loop to iterate through each possible location and have something that represents no object at a location also. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private int[][] map = { { 9, 1, 1, 1, 1, 1, 1, 1, 3 },
                        { 8, 0, 0, 0,10, 0, 0, 0, 2 },
                        { 8, 0,15, 0,14, 0,15, 0, 2 },
                        { 8, 0, 0, 0, 0, 0, 0, 0, 2 },
                        { 8, 0,15, 0,11, 0,15, 0, 2 },
                        { 8, 0, 0, 0,10, 0, 0, 0, 2 },
                        {12, 4, 4, 4, 4, 4, 4, 4, 6 } };
// with the following in a method
for (int y=0; y<map.length; y++)
    for (int x=0; x<map[].length; x++)
{
    Actor actor = null;
    switch (map[y][x])
    {
        case 0: break;
        case 1: actor = new Wall("top"); break;
        case 2: actor = new Wall("right"); break;
        case 3: actor = new Wall("top-right"); break;
        // etc.
    }
    if (actor != null) addObject(actor, 8+x*16, 8+y*16);
}
I used an encoding system that is not too difficult to see. Each side of the wall is given a value (top = 1, right = 2, bottom = 4, and left = 8). Just sum the sides needed for the different types of walls
11770 11770

2014/10/27

#
ok I see this and I have seen this type of array in the source of another game, but my game is 600,650 would this still work? or would it have 600x650 digits... in the array
Super_Hippo Super_Hippo

2014/10/27

#
You placed your walls 16 cells from each other. You only have to write a number for every of these 16x16 squares, not 390000 digits. ;)
11770 11770

2014/10/27

#
alright although I have run out of time for the project as I have no more free time until its due haha i'd like to finish it I already meet the req to get 100% thank you guys
You need to login to post a reply.