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

2018/3/20

Problem dabei etwas an einem zufälligem Ort spawnen zu lassen aber auf bestimmten koordinaten nicht

1
2
Iiuz Iiuz

2018/3/20

#
Ich will Healkits zufällig auf der Map spawnen lassen, allerdings sind auf dieser Hindernisee und auf diesen sollen diese nicht spawnen. Ich habe dazu einen Quellcode entwickelt aber die Healkits spawnen trotzdem auf den Hindernissen, ich hoffe ihr könnt meinen Code verbessern. import greenfoot.*; /** * Write a description of class Healkit here. * * @author (your name) * @version (a version number or a date) */ public class Healkit extends Actor { long lastAdded = System.currentTimeMillis(); int zufallsX; int zufallsY; public Healkit() { zufallsX = Greenfoot.getRandomNumber(18)+1; zufallsY = Greenfoot.getRandomNumber(14)+1; } /** * Act - do whatever the Healkit wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { long curTime = System.currentTimeMillis(); if (curTime == lastAdded + 20000) //20000ms = 20s { if ((!((zufallsX == 2) && (zufallsY == 3))) && (!((zufallsX == 3) && (zufallsY == 1))) && (!((zufallsX == 3)&& (zufallsY == 5))) && (!((zufallsX == 3) && (zufallsY == 8))) && (!((zufallsX == 4) && (zufallsY == 14))) && (!((zufallsX == 6) && (zufallsY == 6))) && (!((zufallsX == 6) && (zufallsY == 10))) && (!((zufallsX == 6) && (zufallsY == 14))) && (!((zufallsX == 7) && (zufallsY == 2))) && (!((zufallsX == 8) && (zufallsY == 7))) && (!((zufallsX == 9) && (zufallsY == 5))) && (!((zufallsX == 9) && (zufallsY == 11))) && (!((zufallsX == 10) && (zufallsY == 2))) && (!((zufallsX == 11) && (zufallsY == 10))) && (!((zufallsX == 12) && (zufallsY == 6))) && (!((zufallsX == 13) && (zufallsY == 2))) && (!((zufallsX == 13) && (zufallsY == 9))) && (!((zufallsX == 14) && (zufallsY == 13))) && (!((zufallsX == 15) && (zufallsY == 4))) && (!((zufallsX == 15) && (zufallsY == 8))) && (!((zufallsX == 16) && (zufallsY == 9))) && (!((zufallsX == 16) && (zufallsY == 11))) && (!((zufallsX == 17) && (zufallsY == 2))) ) { getWorld().addObject(new Healkit(), zufallsX, zufallsY); lastAdded = curTime; } else {lastAdded = curTime; } } } }
Vercility Vercility

2018/3/20

#
Was zur hölle ist das, hast du echt jedes einzelne hinderniss hard gecoded hahahaha? Du kannst leicht abfragen ob die koordinaten ein hinderniss überdecken
Healkit healkit = new Healkit() ;
getWorld().addObject(healkit, zufallsX, zufallsY);

if(healkit.getOneIntersectingObject(Hinderniss) != null) {

getWorld().removeObject(healkit);
//neues healkit platzieren

}
Iiuz Iiuz

2018/3/21

#
das System gibt mir einige Syntax Fehler aus wenn ich deinen Code hineinfüge
danpost danpost

2018/3/21

#
Add the following into the Healkit class:
protected void addedToWorld(World world)
{
    if(healkit.getOneIntersectingObject(Hinderniss) != null) getWorld().removeObject(this);
and use the following to spawn one:
Healkit healkit = new Healkit();
while (healkit.getWorld() == null)
{
    healkit = new Healkit();
    int zufallsX = Greenfoot.getRandomNumber(getWorld().getWidth());
    int zufallsY = Greenfoot.getRandomNumber(getWorld().getHeight());
    getWorld().addObject(healkit, zufallsX, zufallsY);
}
@Vercility, you cannot do any collision detection for an object that does not belong to the class at hand. Collision methods have protected access.
Iiuz Iiuz

2018/3/21

#
ich habe jetzt auch schon herausgefunden dass es ohne if (curTime == lastAdded + 20000) //20000ms = 20s funktioniert allerdings brauch ich den teil des codes damit nicht alle healkits sofort spawnen kann ich das viellecht irgendwie umstellen oder umschreiben oder so damit es funktioniert ??
Iiuz Iiuz

2018/3/21

#
cannot find symbol variable healkit cannot find symbol variable hindernisse - hindernisse is just the overclass the real objectives are Baum and Stein
Iiuz Iiuz

2018/3/21

#
that is my code now import greenfoot.*; /** * Write a description of class Healkit here. * * @author (your name) * @version (a version number or a date) */ public class Healkit extends Actor { long lastAdded = System.currentTimeMillis(); int zufallsX; int zufallsY; public Healkit() { zufallsX = Greenfoot.getRandomNumber(18)+1; zufallsY = Greenfoot.getRandomNumber(14)+1; } protected void addedToWorld(World world) { if(healkit.getOneIntersectingObject(Baum) != null) getWorld().removeObject(this); } /** * Act - do whatever the Healkit wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Healkit healkit = new Healkit(); while (healkit.getWorld() == null) { healkit = new Healkit(); int zufallsX = Greenfoot.getRandomNumber(getWorld().getWidth()); int zufallsY = Greenfoot.getRandomNumber(getWorld().getHeight()); getWorld().addObject(healkit, zufallsX, zufallsY); } } }
danpost danpost

2018/3/21

#
Here is your code in a better forrmat
import greenfoot. *;

public class Healkit extends Actor
{
    long lastAdded = System.currentTimeMillis();
    int randomX;
    int randomly;

    public Healkit()
    {
        randomX = Greenfoot.getRandomNumber (18) +1;
        randomY = Greenfoot.getRandomNumber (14) +1;
    }
    
    protected void addedToWorld (World world)
    {
        if (healkit.getOneIntersectingObject (Tree) ! = null) getWorld (). removeObject (this);
    }
    
    public void act()
    {
        Healkit healkit = new Healkit ();
        while (healkit. getWorld () == null)
        {
            healkit = new Healkit();
            int randomX = Greenfoot.getRandomNumber(getWorld (). getWidth ());
            int randomlyY = Greenfoot.getRandomNumber (getWorld (). getHeight ());
            getWorld (). addObject (healkit, randomX, randomY);
        }
    }
}
Now, for a better view.
danpost danpost

2018/3/21

#
The Healkit objects should be spawned in your world subclass. This class should only be like this:
import greenfoot. *;
 
public class Healkit extends Actor
{
    protected void addedToWorld(World world)
    {
        if (getOneIntersectingObject(Tree.class) != null) getWorld().removeObject(this);
    }
}
Iiuz Iiuz

2018/3/21

#
first I thank you but there are still some errors in this part if (healkit.getOneIntersectingObject (Tree) ! = null) getWorld (). removeObject (this); and this part if (curTime == lastAdded + 20000) //20000ms = 20s need to be there so only all 20 seconds spawn a healkit
Iiuz Iiuz

2018/3/21

#
so she healkit shouldnt spawn when there is no, it should spawn every 20 seconds a new healkit
danpost danpost

2018/3/21

#
Iiuz wrote...
first I thank you but there are still some errors in this part if (healkit.getOneIntersectingObject (Tree) ! = null) getWorld (). removeObject (this);
The code was corrected in my last post.
and this part if (curTime == lastAdded + 20000) //20000ms = 20s need to be there so only all 20 seconds spawn a healkit
Stuff like this will be in your world subclass (called MyWorld maybe).
Vercility Vercility

2018/3/21

#
danpost wrote...
@Vercility, you cannot do any collision detection for an object that does not belong to the class at hand. Collision methods have protected access.
Am I missing something here lol. Healkit is a subclass of actor so it should have access to getOneIntersectingObject which should then return any objects of the Hindernis class?
Yehuda Yehuda

2018/3/21

#
Healkit can access them but not the World, just like the World can't access the private variables from Healkit.
Iiuz Iiuz

2018/3/21

#
sorry but i am confused now which part should come now in which class?
There are more replies on the next page.
1
2