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

2021/1/13

removing object from array on world by clicking at it

1
2
3
Zurah Zurah

2021/1/13

#
i dont really know what to do or where to start, but i want to remove certain objects in the world (which i created using an array of 100 by 100 objects) by clicking at it. i dont really know how to get the mouse position and getting the object at that exact position at the same time. furthermore i dont really know how to remove that object from the world then after clicking on it. i dont have any code to provide as i basically dont know anywhere or anyhow to start, but if some of u have an idea how to achieve this, let me know ^^
danpost danpost

2021/1/13

#
In the act method of objects being clicked on, add the following:
if (Greenfoot.mouseClicked(this))
{
    getWorld().removeObject(this);
}
Zurah Zurah

2021/1/13

#
the objects im talking about are blocks, and they are all generated inside the world class during a world generation that i set up. im gonna try this solution out now ^^
Zurah Zurah

2021/1/13

#
actually no, they are all individual actors, im just creating the array in the world class and referncing them from the actor classes there
Zurah Zurah

2021/1/13

#
im just gonna share all the relevant code in here so that people can further troubleshoot this as i really dont see any way of doing this currently my "MyWorld.class":
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.Collection;

public class MyWorld extends World
{
    static int playerX;
    static int playerY;

//Generelle Infos für korrekte Erstellung der Welt / general infos concerning the world
public MyWorld()
    {   
        //Größe der Welt definieren / define size of the screen
        super(27, 15, 60);
        //Generierung der Welt / generation of the world
        setup();
        //Startkoordinaten vom Spieler innerhalb des Arrays / starting coordinates of the player
        playerX = 50;
        playerY = 23;
        //Spieler spawnen / spawn the character
        addObject(new Spieler(), 13, 7);
} 

//Alle Interkationen, die innerhalb der Welt passieren sollen / all interactions that should happen inside the world
public void act()
    {
        setPaintOrder(Spieler.class, Boden.class);
        removeObjects(getObjects(Boden.class));
        for(int x = 0; x <= 27; x++){
            for(int y = 0; y<= 15; y++){
            //ka was das macht habs kopiert / copied this from a friend lol
            int a = world [playerX - 13 + x][playerY - 7 + y];
            generateWorld(a, x, y);
            }
        }
        //Charakterbewegungen innerhalb des Arrays / character movement inside of the array
        if(Greenfoot.isKeyDown("A") && playerX >= 20)
            {
                playerX--;
                Greenfoot.delay(5);
        }
        if(Greenfoot.isKeyDown("D") && playerX <= 80)
            {
                playerX++;
                Greenfoot.delay(5);
        }
        if(Greenfoot.isKeyDown("W") && playerY >= 13)
            {
                playerY--;
                Greenfoot.delay(5);
        }
        if(Greenfoot.isKeyDown("S") && playerY <= 85)
            {
                playerY++;
                Greenfoot.delay(5);
        }
}

//Array für die Welt erstellen / create array to form the world with
static int[][] world = new int [100][100];

//Eigentlicher Part der Weltgenerierung / actual process of generating the world 
public void setup()
{
      //Gras als oberste Schicht setzen / Set grass as first layer
      for(int x = 0; x < 100; x++)
       {
            for(int y = 24; y <= 24; y++)
            world[x][y] = 1;
       }
      //Stein überall hinsetzen und dann später durch Erze ersetzen / Generate Stone first for every other block, then later replace it by the following blocks
      for(int x = 0; x < 100; x++)
       {
            for(int y = 25; y < 100; y++)
            world[x][y] = 3;
       }
      //Erde / dirt
      for(int r = 0; r < 1300; r++)
       {
           int x = Greenfoot.getRandomNumber(100);
           int y = Greenfoot.getRandomNumber(8)+25;
           world[x][y] = 2;
        }
      //Kohle / coal
      for(int r = 0; r < 333; r++)
       {
           int x = Greenfoot.getRandomNumber(100);
           int y = Greenfoot.getRandomNumber(20)+33;
           world[x][y] = 4;
        }
      //Eisen / iron
      for(int r = 0; r < 200; r++)
       {
           int x = Greenfoot.getRandomNumber(100);
           int y = Greenfoot.getRandomNumber(20)+50;
           world[x][y] = 5;
        }
      //Gold / gold
      for(int r = 0; r < 150; r++)
       {
           int x = Greenfoot.getRandomNumber(100);
           int y = Greenfoot.getRandomNumber(20)+70;
           world[x][y] = 6;
        }  
      //Diamant / diamond
      for(int r = 0; r < 100; r++)
       {
           int x = Greenfoot.getRandomNumber(100);
           int y = Greenfoot.getRandomNumber(20)+80;
           world[x][y] = 7;
        }  
}

//Liste aller zu generiernden Blöcke / list of all blocks to be generated
public void generateWorld(int a, int x, int y)
    {   
        switch(a)
             {
                case 1:
                    gras grass = new gras();
                    addObject(grass, x, y);
                    break;
                case 2:
                    erde dirt = new erde();
                    addObject(dirt, x, y);
                    break;
                case 3:
                    stein stone = new stein();
                    addObject(stone, x, y);
                    break;
                case 4:
                    kohle coal = new kohle();
                    addObject(coal, x, y);
                    break;
                case 5:
                    eisen iron = new eisen();
                    addObject(iron, x, y);
                    break;
                case 6:
                    gold gold = new gold();
                    addObject(gold, x, y);
                    break;
                case 7:
                    diamant diamond = new diamant();
                    addObject(diamond, x, y);
                    break;
        }           
    }   
}



My "Boden.class" (english: floor.class):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Boden here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Boden extends Actor
{
    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
            {
                getWorld().removeObject(this);
        }
    }    
}
(as suggested by @danpost all other classes are empty / irrelevant
danpost danpost

2021/1/13

#
Actually, it may seem like it does not work; but, they are really being removed when clicked on. The problem is that you are generating and re-generating the world from the array in your MyWorld act method. So, as is, you have a whole new set of actors (except the Spieler object) every act step and removing one does not matter. It just keeps getting replaced by another one. To test this, insert the following before line 15 in the Boden class:
MyWorld.world[getX()][getY()] = 0;
The code, lines 27 thru 35, in your MyWorld class should be in the constructor of the class (after "setup();" -- line 16).
Zurah Zurah

2021/1/14

#
thanks a lot, i can now finally remove objects from the world ^^ now the only issue im having is that i cannot move anymore, despite me not having changed any of the movement code
danpost danpost

2021/1/14

#
Zurah wrote...
thanks a lot, i can now finally remove objects from the world ^^ now the only issue im having is that i cannot move anymore, despite me not having changed any of the movement code
Show revised MyWorld class codes.
Zurah Zurah

2021/1/14

#
public MyWorld()
    {   
        //Größe der Welt definieren / define size of the world
        super(27, 15, 60);
        //Generierung der Welt / generation of the world
        setup();
        //Startkoordinaten vom Spieler innerhalb des Arrays / starting coordinates of the player
        playerX = 50;
        playerY = 23;
        //Spieler spawnen / spawn the character
        addObject(new Spieler(), 13, 7);
        
        setPaintOrder(Spieler.class, Boden.class);
        removeObjects(getObjects(Boden.class));
        //Nur den sichtbaren Spielbereich rendern
        for(int x = 0; x <= 27; x++){
            for(int y = 0; y<= 15; y++){
            int a = world [playerX - 13 + x][playerY - 7 + y];
            generateWorld(a, x, y);
            }
        }
} 
danpost danpost

2021/1/14

#
Please show entire class codes.
Zurah Zurah

2021/1/14

#
sorry, i misunderstood u MyWorld.class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.Collection;

public class MyWorld extends World
{
    static int playerX;
    static int playerY;

//Generelle Infos für korrekte Erstellung der Welt / general infos concerning the world
public MyWorld()
    {   
        //Größe der Welt definieren / define size of the world
        super(27, 15, 60);
        //Generierung der Welt / generation of the world
        setup();
        //Startkoordinaten vom Spieler innerhalb des Arrays / starting coordinates of the player
        playerX = 50;
        playerY = 23;
        //Spieler spawnen / spawn the character
        addObject(new Spieler(), 13, 7);
        
        setPaintOrder(Spieler.class, Boden.class);
        removeObjects(getObjects(Boden.class));
        //Nur den sichtbaren Spielbereich rendern
        for(int x = 0; x <= 27; x++){
            for(int y = 0; y<= 15; y++){
            int a = world [playerX - 13 + x][playerY - 7 + y];
            generateWorld(a, x, y);
            }
        }
} 

//Alle Interkationen, die innerhalb der Welt passieren sollen / all interactions that should happen inside the world
public void act()
    {
        //Charakterbewegungen innerhalb des Arrays / character movement inside of the array
        if(Greenfoot.isKeyDown("A") && playerX >= 20)
            {
                playerX--;
                Greenfoot.delay(5);
        }
        if(Greenfoot.isKeyDown("D") && playerX <= 180)
            {
                playerX++;
                Greenfoot.delay(5);
        }
        if(Greenfoot.isKeyDown("W") && playerY >= 13)
            {
                playerY--;
                Greenfoot.delay(5);
        }
        if(Greenfoot.isKeyDown("S") && playerY <= 185)
            {
                playerY++;
                Greenfoot.delay(5);
        }
}

//Array für die Welt erstellen / create array to form the world with
static int[][] world = new int [200][100];

//Eigentlicher Part der Weltgenerierung / actual process of generating the world 
public void setup()
{
      //Gras als oberste Schicht setzen mit zufälliger Chance zu spawnen (kleine Höhenunterschiede)
      for(int r = 0; r < 75; r++)
       {
            int x = Greenfoot.getRandomNumber(200);
            int y = Greenfoot.getRandomNumber(1)+24;
            world[x][y] = 1;
      }
      //Stein überall hinsetzen und dann später durch Erze ersetzen / Generate Stone first for every other block, then later replace it by the following blocks
      for(int x = 0; x < 200; x++)
       {
            for(int y = 25; y < 100; y++)
            world[x][y] = 3;
      }
      //Erde / dirt
      for(int r = 0; r < 1600; r++)
       {
           int x = Greenfoot.getRandomNumber(200);
           int y = Greenfoot.getRandomNumber(8)+25;
           world[x][y] = 2;
      }
      //Kohle / coal
      for(int r = 0; r < 333; r++)
       {
           int x = Greenfoot.getRandomNumber(200);
           int y = Greenfoot.getRandomNumber(20)+33;
           world[x][y] = 4;
      }
      //Eisen / iron
      for(int r = 0; r < 200; r++)
       {
           int x = Greenfoot.getRandomNumber(200);
           int y = Greenfoot.getRandomNumber(20)+50;
           world[x][y] = 5;
      }
      //Gold / gold
      for(int r = 0; r < 150; r++)
       {
           int x = Greenfoot.getRandomNumber(200);
           int y = Greenfoot.getRandomNumber(20)+70;
           world[x][y] = 6;
      }  
      //Diamant / diamond
      for(int r = 0; r < 100; r++)
       {
           int x = Greenfoot.getRandomNumber(200);
           int y = Greenfoot.getRandomNumber(20)+80;
           world[x][y] = 7;
      }  
}

//Liste aller zu generiernden Blöcke / list of all blocks to be generated
public void generateWorld(int a, int x, int y)
    {   
        switch(a)
             {
                case 1:
                    gras grass = new gras();
                    addObject(grass, x, y);
                    break;
                case 2:
                    erde dirt = new erde();
                    addObject(dirt, x, y);
                    break;
                case 3:
                    stein stone = new stein();
                    addObject(stone, x, y);
                    break;
                case 4:
                    kohle coal = new kohle();
                    addObject(coal, x, y);
                    break;
                case 5:
                    eisen iron = new eisen();
                    addObject(iron, x, y);
                    break;
                case 6:
                    gold gold = new gold();
                    addObject(gold, x, y);
                    break;
                case 7:
                    diamant diamond = new diamant();
                    addObject(diamond, x, y);
                    break;
        }           
    }   
}



(changed the world size from 100 to 200 on the x axis but that doesnt have to do anything with my problem)
danpost danpost

2021/1/14

#
Remove line 28 and change line 29 to:
generateWorld(world[x][y], x, y);
Change lines 18 thru 21 to be:
playerX = 13;
playerY = 7;
addObject(new Spieler(), playerX, playerY);
In lines 38, 43, 48 and 53, change conditions to "> 0", "< 26", "> 0" and "< 14", respectively.
danpost danpost

2021/1/14

#
Zurah wrote...
changed the world size from 100 to 200 on the x axis but that doesnt have to do anything with my problem
Changing the size of your world array does not change the size of your world, which is currently set to 27 by 15, as per line 14.
Zurah Zurah

2021/1/14

#
i did those changes but now the world wont generate at all and my character is just floating in a void edit: my MyWorld.class now:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.Collection;

public class MyWorld extends World
{
    static int playerX;
    static int playerY;

//Generelle Infos für korrekte Erstellung der Welt / general infos concerning the world
public MyWorld()
    {   
        //Größe der Welt definieren / define size of the world
        super(27, 15, 60);
        //Generierung der Welt / generation of the world
        setup();
        //Startkoordinaten vom Spieler innerhalb des Arrays / starting coordinates of the player
        playerX = 13;
        playerY = 7;
        //Spieler spawnen / spawn the character
        addObject(new Spieler(), playerX, playerY);
        
        setPaintOrder(Spieler.class, Boden.class);
        removeObjects(getObjects(Boden.class));
        //Nur den sichtbaren Spielbereich rendern
        for(int x = 0; x <= 27; x++){
            for(int y = 0; y<= 15; y++){
            //int a = world [playerX - 13 + x][playerY - 7 + y];
            generateWorld(world[x][y], x, y);
            }
        }
} 

//Alle Interkationen, die innerhalb der Welt passieren sollen / all interactions that should happen inside the world
public void act()
    {
        //Charakterbewegungen innerhalb des Arrays / character movement inside of the array
        if(Greenfoot.isKeyDown("A") && playerX > 0)
            {
                playerX--;
                Greenfoot.delay(5);
        }
        if(Greenfoot.isKeyDown("D") && playerX < 26)
            {
                playerX++;
                Greenfoot.delay(5);
        }
        if(Greenfoot.isKeyDown("W") && playerY > 0)
            {
                playerY--;
                Greenfoot.delay(5);
        }
        if(Greenfoot.isKeyDown("S") && playerY < 14)
            {
                playerY++;
                Greenfoot.delay(5);
        }
}

//Array für die Welt erstellen / create array to form the world with
static int[][] world = new int [100][100];

//Eigentlicher Part der Weltgenerierung / actual process of generating the world 
public void setup()
{
      //Gras als oberste Schicht setzen mit zufälliger Chance zu spawnen (kleine Höhenunterschiede)
      for(int r = 0; r < 75; r++)
       {
            int x = Greenfoot.getRandomNumber(100);
            int y = Greenfoot.getRandomNumber(1)+24;
            world[x][y] = 1;
      }
      //Stein überall hinsetzen und dann später durch Erze ersetzen / Generate Stone first for every other block, then later replace it by the following blocks
      for(int x = 0; x < 100; x++)
       {
            for(int y = 25; y < 100; y++)
            world[x][y] = 3;
      }
      //Erde / dirt
      for(int r = 0; r < 1600; r++)
       {
           int x = Greenfoot.getRandomNumber(100);
           int y = Greenfoot.getRandomNumber(8)+25;
           world[x][y] = 2;
      }
      //Kohle / coal
      for(int r = 0; r < 333; r++)
       {
           int x = Greenfoot.getRandomNumber(100);
           int y = Greenfoot.getRandomNumber(20)+33;
           world[x][y] = 4;
      }
      //Eisen / iron
      for(int r = 0; r < 200; r++)
       {
           int x = Greenfoot.getRandomNumber(100);
           int y = Greenfoot.getRandomNumber(20)+50;
           world[x][y] = 5;
      }
      //Gold / gold
      for(int r = 0; r < 150; r++)
       {
           int x = Greenfoot.getRandomNumber(100);
           int y = Greenfoot.getRandomNumber(20)+70;
           world[x][y] = 6;
      }  
      //Diamant / diamond
      for(int r = 0; r < 100; r++)
       {
           int x = Greenfoot.getRandomNumber(100);
           int y = Greenfoot.getRandomNumber(20)+80;
           world[x][y] = 7;
      }  
}

//Liste aller zu generiernden Blöcke / list of all blocks to be generated
public void generateWorld(int a, int x, int y)
    {   
        switch(a)
             {
                case 1:
                    gras grass = new gras();
                    addObject(grass, x, y);
                    break;
                case 2:
                    erde dirt = new erde();
                    addObject(dirt, x, y);
                    break;
                case 3:
                    stein stone = new stein();
                    addObject(stone, x, y);
                    break;
                case 4:
                    kohle coal = new kohle();
                    addObject(coal, x, y);
                    break;
                case 5:
                    eisen iron = new eisen();
                    addObject(iron, x, y);
                    break;
                case 6:
                    gold gold = new gold();
                    addObject(gold, x, y);
                    break;
                case 7:
                    diamant diamond = new diamant();
                    addObject(diamond, x, y);
                    break;
        }           
    }   
}



danpost danpost

2021/1/14

#
Change "25" in lines 76 and 83 to "8". Change "33" in line 90 to "9". Change "50" in line 97 to "11". Change "70" in line 104 to "13". Change "80" in line 111 to "14".
There are more replies on the next page.
1
2
3