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

2015/3/1

It compiles with no errors, then it doesn't do its purpose D:

Slug Slug

2015/3/1

#
I am having issues with my code. What I wish for it to do is for the salt to disappear when it reaches the bottom of the world or at the coordinates 600 for the y value. I am still new to greenfoot and Java in general and some help would be appreciated. Oh the context, I am creating a game where you play a slug dodging falling salt pieces that would kill you if you touch it. Thanks in advance c: - Slug
      /**
     * Act - do whatever the salt wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
     
    int x = getX();
    int y = getY();
        setLocation( x, y + 2);
        
        
        
        if (y == 600)
        
           {
         Actor actor = getOneIntersectingObject(salt.class);
         getWorld().removeObject(actor);
        }
        
danpost danpost

2015/3/1

#
First problem: if your world height is 600 and bounded, meaning you used the world super constructor call of something like 'super(<width>, 600, 1);', then your actor will never take on a 'y' value of 600. The first row of pixels across the top of the window is at 'y = 0' and the last one across the bottom of the window is at 'y = 599' (making a total of 600 rows). Second problem: using 'getOneIntersectingObject' will never return the actor that is looking for intersectors (the actor that the act method is currently being run for). So, line 18 will never have the actor remove itself. You can just use 'getWorld().removeObject(this);' to have an actor remove itself.
Slug Slug

2015/3/1

#
Thanks for helping :D I was able to fix that issue, now I have only one problem remain. I wish for the world to spawn more salt as the previous salt falls to the bottom and removes itself, but now the world does not spawn more salt as the previous salt disappears, anything you can do to help?
import greenfoot.*;

/**
 * Write a description of class Background here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{
    /**
     * Constructor for objects of class Background.
     * 
     */
    
    public Background()
    {    
        // Create a new world with 400x600 cells with a cell size of 1x1 pixels.
        super(400, 600, 1); 
        addObject(new Playable(), 200, 500);
      
        if (getObjects(salt.class).size() < 10) {
            do
            {
        addObject(new salt(), Greenfoot.getRandomNumber(getWidth()), 0);
    } while (getObjects(salt.class).size() < 10);
    }

}
}
danpost danpost

2015/3/1

#
The code block 'public Background()' is your world constructor code. It only executes when a world of type Background is created. You need lines 22 through 28 to execute continuously after the world is created. Place that code in an 'act' method within the class. As a side note, because of the 'do-while' loop, you will be adding all 10 'salt' objects into the world at the same time. Then, they all will fall at the same rate and reach the bottom of the world at the same time. This will, in essence, cause waves of 'salt' objects (which may or may not be what you intended).
Slug Slug

2015/3/1

#
Hm, how would one be able to make it so it does not fall in waves? So the salt objects fall in a randomized way. Do I need to use delays on creating the salt objects?
Slug Slug

2015/3/1

#
Is it possible you could also show me how I could implement the delay system, I am coming from a programming background consisting of Turing so my perception of delaying a single object is much different ^.^" Thanks again for helping me out.
danpost danpost

2015/3/2

#
Instead of the 'do-while' loop, add another 'if' block using a random chance. Something like this:
if (Greenfoot.getRandomNumber(100) == 0)
By asking if the random number returned is a specific number, we are giving it a one percent chance of spawning. We could use '< 1' instead of '== 0'' for the same result; and changing the '1' to any number will actually be the percent chance. So 'if (Greenfoot.getRandomNumber(100)< 30)' would give a 30 percent chance of spawning; however, that would probably be a bit fast.
Slug Slug

2015/3/2

#
Sorry, I am a little confused by your reply. Where would I implement the code into this in order to have the randomized percentages of spawning?
 public void act()
    {
        if (getObjects(salt.class).size() < 10) {
            do
            {
        addObject(new salt(), Greenfoot.getRandomNumber(getWidth()), 0);
    } while (getObjects(salt.class).size() < 10);
    }

}
danpost danpost

2015/3/2

#
Slug wrote...
Where would I implement the code into this in order to have the randomized percentages of spawning?
My suggestion started with this:
Instead of the 'do-while' loop
That is a 'where' -- is it not?
Slug Slug

2015/3/2

#
I was able to find a new method making it randomized, and I am very happy with my results :D Thanks for the help though, and sorry for the constant questions.
danpost danpost

2015/3/2

#
You might want to show what you found for others to see. However, the following should be sufficient:
public void act()
{
    if (getObjects(salt.class).size() < 10 && Greenfoot.getRandomNumber(100) < 3)
        addObject(new salt(), Greenfoot.getRandomNumber(getWidth()), 0);
}
You need to login to post a reply.