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

2012/11/10

Random Target

1
2
Wavesludge Wavesludge

2012/11/10

#
Hello I want an object to appear at random coordinates, but only on part of the world. How would you go about with doing this? And if the object gets hit, and disapear, a new object appears at random coordinates at the same part of the world.
danpost danpost

2012/11/10

#
In your world class:
// add the following line to the act method
checkTarget();
// and with the following values
int minX is lower x limit to spawn
int maxX is upper x limit to spawn
int minY is lower y limit to spawn
int maxY is upper y limit to spawn
// add the following method
private void checkTarget()
{
    if (getObjects(Target.class).isEmpty())
    {
        int x = minX + Greenfoot.getRandomNumber(maxX - minX);
        int y = minY + Greenfoot.getRandomNumber(maxY - minY);
        addObject(new Target(), x, y);
    }
}
Wavesludge Wavesludge

2012/11/11

#
Thank you very much!
Wavesludge Wavesludge

2012/11/11

#
When I compile it, it wants a semicolon after minX.
SPower SPower

2012/11/11

#
You didn't copy this code exactly right?:
// and with the following values
int minX is lower x limit to spawn
int maxX is upper x limit to spawn
int minY is lower y limit to spawn
int maxY is upper y limit to spawn
he just told what you should create, so an example can be:
private int minX = 5;
private int maxX = 100;
private int minY = 5;
private int maxY = 56;
Wavesludge Wavesludge

2012/11/11

#
Oh my bad. Thanks!
Wavesludge Wavesludge

2012/11/12

#
I have one more question. If I want the target to be a ball, and I want it to be 4 different colors. How would you do that? Have 4 pictures that gets randomly selected as the one to show as the target
danpost danpost

2012/11/12

#
One way is putting this in the constructor of your target class:
switch (Greenfoot.getRandomNumber(4))
{
    case 0: setImage("filename1.png"); break;
    case 1: setImage("filename2.png"); break;
    case 2: setImage("filename3.png"); break;
    case 3: setImage("filename4.png"); break;
}
Another way, is to name the image files similarly (something like "target0.png", "target1.png", etc.) and put the following in the constructor of your target class:
setImage("target" + Greenfoot.getRandomNumber(4) + ".png");
Wavesludge Wavesludge

2012/11/12

#
OK, I have five pictures, target0, target1, target2, traget3, target4. The ball cycles through all 5 pictures all the time. All 5 pictures gets shown about every second.
Wavesludge Wavesludge

2012/11/12

#
I have a method that removes the ball and makes a message if the the ball hits the edge, but I can only get it to work on the bottom and right edge.
    public void out()
    {
        if(getX() == getWorld().getWidth()-1) 
        {
            getWorld().removeObject(this);
            JOptionPane.showMessageDialog(null, "Miss" );
            return;
        }    
        
        if(getY() == getWorld().getHeight()-1)
        {
            getWorld().removeObject(this);
            JOptionPane.showMessageDialog(null, "Miss" );
            return;
        } 
    }
I've tried changing -1 to +500, but no success. Got any tips? If I can get this to work I'm finished with this scenario.
danpost danpost

2012/11/12

#
For the other two edge, where the coodinates are zero (hint)?
Wavesludge Wavesludge

2012/11/12

#
I probably wasn't clear enough, but what I meant with "I've tried changing -1 to +500, but no success." is that I've made another if statement(so I had 4 of them) and changed the value of those 2 extra to +500 adn +800. Those 2 up there are working for the 2 edges.
danpost danpost

2012/11/12

#
Let me put it to you this way, those 2 up there (where the coordinates are getWorld().getWidth() - 1 and getWorld().getHeight() - 1) should work fine. Do you see the what I am getting at?
Wavesludge Wavesludge

2012/11/12

#
Yeah I see what you mean, but if the ball hits the left side or the top, it just rolls with the edge until it stops or hits one of the other edges.
Wavesludge Wavesludge

2012/11/12

#
I was thinking. Is there a way to make it so the power and angle value is printed in text and is dynamically changing?
There are more replies on the next page.
1
2