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

2017/12/17

#
Ah, so that's the drawMe method. The problem is that you prevent the rocks from spawning on top of the spaceship, but after that, the drawMe method will change the location again.
Recorsi Recorsi

2017/12/17

#
Is there a way to bypass this or something?
Super_Hippo Super_Hippo

2017/12/17

#
I think you have to test this out. I am not sure...
protected void addedToWorld(World w)
{
   xCoord=getX();
   yCoord=getY();
   drawMe();
   while (!getObjectsInRange(100, Spaceship.class).isEmpty())
   {
       setLocation(Greenfoot.getRandomNumber(900), Greenfoot.getRandomNumber(900));
       xCoord=getX();
       yCoord=getY();
       drawMe();
   }
}
I will be offline for a while. Maybe you can find this out.
danpost danpost

2017/12/17

#
Recorsi wrote...
the world is 3200x3200 idk why it shows 900x900 there
The 900x900 is the viewport size (what the camera sees). Maybe the 3200x3200 is the entirety of the scrolling area? What do you have for calling the 'setCamLimits' method of the ScrollingFixedObjects class? and where do you call it from?
Recorsi Recorsi

2017/12/17

#
danpost wrote...
Recorsi wrote...
the world is 3200x3200 idk why it shows 900x900 there
The 900x900 is the viewport size (what the camera sees). Maybe the 3200x3200 is the entirety of the scrolling area? What do you have for calling the 'setCamLimits' method of the ScrollingFixedObjects class? and where do you call it from?
Do you mean this?:
public static void setCamLimits(double xMax,double yMax)
    {
        camMaxX=xMax;
        camMaxY=yMax;
    }
Thats the only time it gets called
danpost danpost

2017/12/17

#
Recorsi wrote...
Do you mean this?:
public static void setCamLimits(double xMax,double yMax)
    {
        camMaxX=xMax;
        camMaxY=yMax;
    }
Thats the only time it gets called
That is the method, itself (that is not calling it). Where it is being called from to execute the code within it?
Recorsi Recorsi

2017/12/17

#
Oh sry. its called in the constructor of the world:
ScrollingFixedObjects.setCamLimits(3200,3200);
danpost danpost

2017/12/17

#
Recorsi wrote...
Oh sry. its called in the constructor of the world:
ScrollingFixedObjects.setCamLimits(3200,3200);
Okay, so 3200x3200 is your scrolling area (as I presumed above) and the world viewport is 900x900. Oh, that line was not shown in your Spielfeld constructor given previously in this discussion where you questioned the 900x900 size of the world.
danpost danpost

2017/12/17

#
It appears your understanding of a scrolling world is causing problems. It does not matter what part of the scrolling area the world is showing -- the top left corner of the viewport is always (0, 0) as far as world coordinates are concerned. If you want to deal with locations in the scrolling area, you will need to deal with the currently scrolled amounts. That is, the location of an actor would be (camX+getX(), camY+getY()) in the scrolling area.
danpost danpost

2017/12/17

#
danpost wrote...
3200x3200 is your scrolling area
Correction: 3200x3200 is your scrolling range -- and with a viewport of 900x900, the scrolling area ends up to be 4100x4100.
Recorsi Recorsi

2017/12/17

#
danpost wrote...
danpost wrote...
3200x3200 is your scrolling area
Correction: 3200x3200 is your scrolling range -- and with a viewport of 900x900, the scrolling area ends up to be 4100x4100.
i didn't know you have to add the viewport. So at 1600x1600 my spaceship isn't in the middle?
Recorsi Recorsi

2017/12/17

#
While we're at it: Since i added the scrolling world the code that spawns rocks over time doesn't work anymore. Do you know why?
    public void placeRocks()
    {
      {
        if(Greenfoot.getRandomNumber(20) == 1)
        {
          int x = Greenfoot.getRandomNumber(4100);
          int y = Greenfoot.getRandomNumber(4100);
          addObject(new Rock(), x, y);
          rock.setRotation(Greenfoot.getRandomNumber(260));
        }
      } 
    }
danpost danpost

2017/12/17

#
Recorsi wrote...
i didn't know you have to add the viewport. So at 1600x1600 my spaceship isn't in the middle?
From what I can tell, the middle of the scrolling area would be (2050, 2050).
danpost danpost

2017/12/17

#
Recorsi wrote...
the code that spawns rocks over time doesn't work anymore.
    public void placeRocks()
{
    {
        if(Greenfoot.getRandomNumber(20) == 1)
        {
            int x = Greenfoot.getRandomNumber(4100);
            int y = Greenfoot.getRandomNumber(4100);
            addObject(new Rock(), x, y);
            rock.setRotation(Greenfoot.getRandomNumber(260));
        }
    }
}
To double check, add the following line after line 9:
System.out.println("spawned a Rock object at ("+x"+", "+y")");
Compile, run, and report back.
Recorsi Recorsi

2017/12/17

#
danpost wrote...
Recorsi wrote...
the code that spawns rocks over time doesn't work anymore.
    public void placeRocks()
{
    {
        if(Greenfoot.getRandomNumber(20) == 1)
        {
            int x = Greenfoot.getRandomNumber(4100);
            int y = Greenfoot.getRandomNumber(4100);
            addObject(new Rock(), x, y);
            rock.setRotation(Greenfoot.getRandomNumber(260));
        }
    }
}
To double check, add the following line after line 9:
System.out.println("spawned a Rock object at ("+x"+", "+y")");
Compile, run, and report back.
I don't get any messages Here is the constructor if that helps:
   public Spielfeld()
    {    
        super(900, 900, 1,false);
        ScrollingFixedObjects.setCamLimits(3200,3200);
        Spaceship = new Spaceship(this);
        AddShip();
        prepareHitCounter();
        FPS();
        centerCamOnPlayer(); 
        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();
    }
There are more replies on the next page.
1
2
3
4