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

2017/12/9

Stop actors from spawning on top of one actor?

1
2
3
4
Recorsi Recorsi

2017/12/17

#
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.
Super_Hippo Super_Hippo

2017/12/17

#
Then you could have both in this superclass.
protected void addedToWorld(World w)
{
    while (!getObjectsInRange(100, Rocket.class).isEmpty())
    {
        setLocation(Greenfoot.getRandomNumber(850), Greenfoot.getRandomNumber(750));
    }
    xCoord=getX();
    yCoord=getY();
    drawMe();
}
Recorsi Recorsi

2017/12/17

#
Works for the new rocks thx :) but somehow the rocks i place at the beginning of the game still spawn on top of the rocket. Do you know why? This code spawns the rocks at the beginning:
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));
        }
    }
(I made the world a little bit bigger btw)
Super_Hippo Super_Hippo

2017/12/17

#
Do you add the rocket before you add the rocks? Also the rock in lines 7 und 8 is not the same as the one you add in line 6.
Recorsi Recorsi

2017/12/17

#
The rocket comes before the rock in the constructor if that is what you mean. And the rotation doesn't work i know ^^
Super_Hippo Super_Hippo

2017/12/17

#
That is what I meant, yes. Show the constructor of the world.
Recorsi Recorsi

2017/12/17

#
Constructor:
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();
    }
Addship method:
private void AddShip()
    {
        addObject(Spaceship,1600,1600);
        Spaceship.setRotation(90);
    }
Super_Hippo Super_Hippo

2017/12/17

#
I see a 'Spaceship' and not a Rocket. Did you change it in the addedToWorld method of the rocks? Btw, your world is 900×900 and you place objects with far higher coordinates.
Recorsi Recorsi

2017/12/17

#
Danpost started calling it rocket. i changed it every time so no problem ^^ Edit: And the world is 3200x3200 idk why it shows 900x900 there
Super_Hippo Super_Hippo

2017/12/17

#
Do you have an addedToWorld method in your Rock class? If yes, try to remove it. If not, add one with only 'super.addedToWord();'
Recorsi Recorsi

2017/12/17

#
i have the one you sent me in the superclass of the rock
Super_Hippo Super_Hippo

2017/12/17

#
Yes I only mean that if you have one in your Rock class (subclass of the one with the addedToWorld method), it will be overridden and not used.
Recorsi Recorsi

2017/12/17

#
Nope i dont have one in the rock class:
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);
        }
    }
}
Super_Hippo Super_Hippo

2017/12/17

#
Hmm, could you show the ScrollingFixedObjects class code? I don't know what could cause it but maybe there is something obvious when I see it.
Recorsi Recorsi

2017/12/17

#
I implemented a scrolling world, so that code isnt mine ^^
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;
    }
}
There are more replies on the next page.
1
2
3
4