The rock is a subclass of an other class now, which contains the addedtoWorld method i sent. The xcoord and ycoord variables are also created there.
protected void addedToWorld(World w)
{
while (!getObjectsInRange(100, Rocket.class).isEmpty())
{
setLocation(Greenfoot.getRandomNumber(850), Greenfoot.getRandomNumber(750));
}
xCoord=getX();
yCoord=getY();
drawMe();
}public void placeRocksBeginning()
{
for (int i = 0; i < 200; i++) {
int x = Greenfoot.getRandomNumber(3200);
int y = Greenfoot.getRandomNumber(3200);
addObject (new Rock(), x , y);
rock.getRotation();
rock.setRotation(Greenfoot.getRandomNumber(260));
}
}public Spielfeld()
{
super(900, 900, 1,false);
AddShip();
prepareHitCounter();
FPS();
setPaintOrder(DeathScreen.class,ResetInfo.class,HitCounter.class,FPS.class,explosion.class,Rock.class, Spaceship.class, Cloud1.class, Cloud2.class, Border.class, Border2.class);
placeRocksBeginning();
placeRocks();
placeClouds();
placeBorders();
}private void AddShip()
{
addObject(Spaceship,1600,1600);
Spaceship.setRotation(90);
}import greenfoot.*;
/**
*
*
* @author
* @version 16.12.17
*/
public class Rock extends ScrollingFixedObjects
{
private static final int NUM_FRAGMENTS = 40;
GreenfootSound Pling = new GreenfootSound("Pling.wav");
public Rock()
{
GreenfootImage image = getImage();
image.scale(55,55);
setImage(image);
}
public void act()
{
drawMe();
if (isTouching(Laser.class))
{
explodeRock();
}
}
/*private void Safespace(){
if (!getWorld().getObjects(Spaceship.class).isEmpty())
{
Actor rocket = (Actor)getWorld().getObjects(Spaceship.class).get(0);
int range = 100;
int x = (Greenfoot.getRandomNumber(850-range*2)+getWorld().getWidth()+range+rocket.getX())%getWorld().getWidth();
int y = (Greenfoot.getRandomNumber(750-range*2)+getWorld().getHeight()+range+rocket.getY())%getWorld().getHeight();
getWorld().addObject(new Rock(), x, y);
}
}
protected void addedToWorld(World world) //Safe space for the spaceship
{
while (!getObjectsInRange(70, Spaceship.class).isEmpty())
{
setLocation(Greenfoot.getRandomNumber(3200), Greenfoot.getRandomNumber(3200));
}
}*/
public boolean hitsShip(Spaceship spaceship)
{
return getObjectsInRange(getImage().getWidth()*8/10, Spaceship.class).contains(spaceship);
}
public void explodeRock()
{
placeDebris (getX(), getY(), NUM_FRAGMENTS);
World Spielfeld = getWorld();
Spielfeld spielfeld = (Spielfeld)Spielfeld;
HitCounter hitcounter = spielfeld.getHitCounter();
hitcounter.addhitScore();
Pling.play();
getWorld().removeObject(this);
}
private void placeDebris(int x, int y, int numFragments)
{
for (int i=0; i < numFragments; i++) {
getWorld().addObject (new Debris(), x ,y);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Abstract superclass to all scrolling objects, manages the locations on screen for all objects.
* Scrolling is obtained by moving a fictive camera and painting the objects
* relative the cameras coordinates. A key method in this class is drawMe(), which all
* scrolling objects must call to update their coordinates relative to the cameras
*
* @author Kalle Larsson, karlars@live.se
* @version 2014-07-21
*/
public abstract class ScrollingFixedObjects extends Actor
{
protected static double camX,camY;
protected double xCoord,yCoord;
private static double defaultCamX=0, defaultCamY=0;
private static double camMaxX,camMaxY;
/**
* sets the starting position of the camera
*@param x the x coordinate where the camera (as in top left corner of visible screen) should stand
*@param y the y coordinate where the camera (as in top left corner of visible screen) should stand
*/
public static void setDefaultCamCoords(double x,double y)
{
defaultCamX=x;
defaultCamY=y;
}
/**
* set the maximum x and y coordinate of the camera, the camera will not pan
* out of these coordinates. the minimum coordinates are 0,0 in x and y respectively
* But there's nothing preventing to add arbitrary minimum coordnates to.. :)
* @param xMax the maximum x coordinate the camera will move to
* @param yMax the maximum y coordinate the camera will move to
*/
public static void setCamLimits(double xMax,double yMax)
{
camMaxX=xMax;
camMaxY=yMax;
}
/**
* drawMe- draws the object in camera coordnates,
* MUST be called by all scrolling objects!!!
*/
protected void drawMe()
{
double screenX=Math.round(xCoord-camX);
double screenY=Math.round(yCoord-camY);
setLocation((int) screenX,(int) screenY);
}
/**
* retrives the objects coordinates expressed in the world coordinate system
* (the window we can see), places all objects in their position relative the camera
* to avoid everything jumping to place when "run" is pressed
*/
@Override
protected void addedToWorld(World w)
{
while (!getObjectsInRange(100, Spaceship.class).isEmpty())
{
setLocation(Greenfoot.getRandomNumber(900), Greenfoot.getRandomNumber(900));
}
xCoord=getX();
yCoord=getY();
drawMe();
}
/**
* Reset the camera coordinates to default.
*/
public static void resetCam()
{
camX=defaultCamX;
camY=defaultCamY;
}
/**
* Update camera coordinates, must me called when we want scrolling
* to occur, but the camera can only be positioned in:
* 0 <= x <= camMaxX
* 0 <= y <= camMaxY
* (But it's possible to add arbitrary camera min coords)
*
* @param dx is the change in camera coordinates in x direction
* @param dy is the change in camera coordinates in y direction
*/
public void moveCam(double dx, double dy)
{
camX+=dx;
if (camX<=0) camX=0;
else if (camX>=camMaxX) camX=camMaxX;
camY+=dy;
if(camY<=0) camY=0;
else if (camY>=camMaxY) camY=camMaxY;
}
/**
* Set camera coordinates
*
* @param x is the cameras x position
* @param y is the cameras y position
*/
public void setCam(double x, double y)
{
camX=x;
camY=y;
}
/**
* Get the cameras current x-coordinate
* @return the cameras current x-coordinate
*/
public double getCamX()
{
return camX;
}
/**
* Get the cameras current y-coordinate
* @return the cameras current y-coordinate
*/
public double getCamY()
{
return camY;
}
}