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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // 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); } } |
1 2 3 4 5 | // 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 |
1 2 3 4 | private int minX = 5 ; private int maxX = 100 ; private int minY = 5 ; private int maxY = 56 ; |
1 2 3 4 5 6 7 | 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 ; } |
1 | setImage( "target" + Greenfoot.getRandomNumber( 4 ) + ".png" ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 ; } } |