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

2014/5/13

Actor spawning

1
2
runsngoses runsngoses

2014/5/13

#
hey guys i really need your help! i'm totaly new in programming and i started with greenfoot it seems to be easy to start with wombats :3 i want to spawn one coal-unit on the map(random place), but on the map is a hardcoded tree row.. if the coal-unit spawns randomly on one tree i want to remove the coal-unit and let the coal-unit spawn again randomly until it spawns on a empty place. and i want to spawn the coal unit 5px from the world edge, my world is 800x600 this is now where i stuck i dont get it how i can go one... thaaanks
public class level_1 extends World
{
public level_1()
    {    
        super(160, 120, 5);
        
        spawn_coal();
        
        spawn_treerow();
    }
    
    public void spawn_treerow()
    {
       addObject(new tree(), 12, 10);
       addObject(new tree(), 31, 10);
       addObject(new tree(), 50, 10);
    }
    
    public void spawn_coal()
    {
        addObject(new coal(), Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
    }
}
runsngoses runsngoses

2014/5/13

#
i really need your help!
Zamoht Zamoht

2014/5/13

#
    public void spawn_coal()  
    {  
        boolean spawned = false;
        while(!spawned) {
            int x = 5 + Greenfoot.getRandomNumber(getWidth()-10);
            int y = 5 + Greenfoot.getRandomNumber(getHeight()-10);          
            coal myCoal = new coal();
            addObject(myCoal, x, y);
            spawned = true;
            if(myCoal.getOneIntersectingObject(tree.class) != null) {
                removeObject(myCoal);
                spawned = false;
            }
        }
    }  
Code isn't tested.
runsngoses runsngoses

2014/5/13

#
now it says: "getOneIntersectingObject(java.lang.Class) has protected access in greenfoot.Actor"
danpost danpost

2014/5/13

#
Because 'getOneIntersectingObject' (like all collision methods) has protected access, you can only use it within the class of that actor (you cannot use it, in this case, outside the 'coal' class on a coal object). You can create a 'public' method inside the coal class to do the work and return the Boolean value and call that method from outside the class.
runsngoses runsngoses

2014/5/13

#
so i need to go in the coal-unit and place the code there? and than i just call it in actor level_1?
runsngoses runsngoses

2014/5/13

#
still doesnt work for me can u give me a code tip? i dont get it
Zamoht Zamoht

2014/5/13

#
Make sure that coal is a subclass of the Actor class. Please show the whole code of your coal class.
runsngoses runsngoses

2014/5/13

#
coal is a subclass of Actor class.. here is the full code:
import greenfoot.*;

public class coal extends Actor
{
public void spawn_coal()
    {
        boolean spawned = false;
        while(!spawned) {
            int x = 5 + Greenfoot.getRandomNumber(getWidth()-10);
            int y = 5 + Greenfoot.getRandomNumber(getHeight()-10);
            coal myCoal = new coal();
            addObject(myCoal, x , y);
            spawned = true;
        }
        if(myCoal.getIntersectingObjects(tree.class) != null) {
                removeObject(myCoal);
                spawned = false;
            }
        }
    }    
and than greenfoot says: "cannot find symbol - method getWidth()
Zamoht Zamoht

2014/5/13

#
Oh my bad. Remove the spawn_coal() method from you coal class. Add the following method instead:
public boolean spawnedOnTree()
{
    return (getOneIntersectingObject(tree.class) != null);
}
After adding this method to your coal class you should change the spawn_coal() method in your world class to the following:
public void spawn_coal()    
{    
    boolean spawned = false;  
    while(!spawned) {  
        int x = 5 + Greenfoot.getRandomNumber(getWidth()-10);  
        int y = 5 + Greenfoot.getRandomNumber(getHeight()-10);            
        coal myCoal = new coal();  
        addObject(myCoal, x, y);  
        spawned = true;  
        if(myCoal.spawnedOnTree()) {  
            removeObject(myCoal);  
            spawned = false;  
        }  
    }  
}  
runsngoses runsngoses

2014/5/14

#
thanks but it still dont works.. it still spawns on tree and it still spawns on world edge.. i want that my complete picture from coal dont hit any objects or the edge..
danpost danpost

2014/5/14

#
Remove the spawn_coal method from the coal class and put the following method in it
public void addedToWorld(World world)
{
    int wide = getImage().getWidth();
    int high = getImage().getHeight();
    int x = 0, y = 0;
    while (!getIntersectingObjects(null).isEmpty())
    {
        x = (Greenfoot.getRandomNumber(world.getWidth()-wide)+wide/2;
        y = (Greenfoot.getRandomNumber(world.getHeight()-high)+high/2;
    }
    setLocation(x, y);
}
Then, your level_1 code should look like this:
import greenfoot.*;

public class level_1 extends World
{
    public level_1()
    {    
        super(160, 120, 5);
        spawn_treerow();
        spawn_coal();
    }
    
    public void spawn_treerow()
    {
       addObject(new tree(), 12, 10);
       addObject(new tree(), 31, 10);
       addObject(new tree(), 50, 10);
    }
    
    private void spawn_coal()
    {
        addObject(new coal(), 0, 0);
    }
}
runsngoses runsngoses

2014/5/16

#
it still dont works for me.. i want that nothing from my coal picture hits the edge or a tree
danpost danpost

2014/5/16

#
Sorry -- change line 6 in the 'addedToWorld' method to this:
while (x == 0 || !getIntersectingObjects(null).isEmpty())
runsngoses runsngoses

2014/5/16

#
now it just spawns in the middle of the map and moves a bit to the left and a bit to the right
There are more replies on the next page.
1
2