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

2017/1/26

Letting one actor spawn above the other

Recorsi Recorsi

2017/1/26

#
hello i have one actor that you can move left and right called "paddle" and the other one that you can spawn by pressing "space" called "ball". but i told it where to spawn with coordiantes. how can i let the ball spawn and exact amount of pixels above the paddle, no matter where it is?
  public void NeuesPaddel() 
    {
       addObject(new Paddel(), 320, 450);
    }
 public void NeuerBall(){
        if  (Greenfoot.isKeyDown("space") &&
            getObjects(Ball.class) .size() == 0)
        {
            Ball ball = new Ball();
            addObject (ball, 320, 400);
        }
    }
Super_Hippo Super_Hippo

2017/1/26

#
You can either place the second method in the Paddel class. Then you can spawn the paddle at 'getX(), getY()-50'. Or you save (or get) a reference to the Paddel in the world and then get the coordinates of that Paddel.
Recorsi Recorsi

2017/1/26

#
Super_Hippo wrote...
You can either place the second method in the Paddel class. Then you can spawn the paddle at 'getX(), getY()-50'. Or you save (or get) a reference to the Paddel in the world and then get the coordinates of that Paddel.
i dont want to spawn the paddle below the ball, i want the paddle to spawn at 320,450 at the game start and with pressing space i want to spawn a ball 50 pixels above the paddle, no matter where it is
danpost danpost

2017/1/26

#
Have the paddle check for the keystroke and check to see if any balls are in the world before adding one 50 pixels above where it is at.
Recorsi Recorsi

2017/1/26

#
danpost wrote...
Have the paddle check for the keystroke and check to see if any balls are in the world before adding one 50 pixels above where it is at.
  public void NeuesPaddel() 
    {
       addObject(new Paddel(), 320, 450);
          if  (Greenfoot.isKeyDown("space") &&
            getObjects(Ball.class) .size() == 0)
        {
            Ball ball = new Ball();
            addObject (ball, get(X), (get(Y)+50));
        }
    }
like that? it cant find X and Y.
Super_Hippo Super_Hippo

2017/1/26

#
The methods are 'getX' and 'getY' and since you can't get the coordinates from a world, these methods can only be used on actors.
Recorsi wrote...
i dont want to spawn the paddle below the ball, i want the paddle to spawn at 320,450 at the game start and with pressing space i want to spawn a ball 50 pixels above the paddle, no matter where it is
Well, the sentence should have read 'Then you can spawn the **ball** ...'. I think you only want to add one paddle at the beginning. Right now, you can only add a new ball when the 'NeuesPaddel' method is executed (which adds a new paddle), space is pressed and no ball is in the world. This doesn't make sense.
Recorsi Recorsi

2017/1/26

#
so i have to make 2 separate methods right? how do i get the coordinates of the paddle for the ball +50 if get(X) doesnt work?
Super_Hippo wrote...
I think you only want to add one paddle at the beginning.
right i want to add one paddle at the beginning of the game and then one ball at the time by pressing space.
Super_Hippo Super_Hippo

2017/1/26

#
Reduce the 'NeuesPaddel' method back to just adding the paddle and try this in the Paddels act method.
if (Greenfoot.isKeyDown("space") && getWorld().getObjects(Ball.class).isEmpty())
{
    getWorld().addObject(new Ball(), getX(), getY()-50);
}
Recorsi Recorsi

2017/1/26

#
Super_Hippo wrote...
Reduce the 'NeuesPaddel' method back to just adding the paddle and try this in the Paddels act method.
if (Greenfoot.isKeyDown("space") && getWorld().getObjects(Ball.class).isEmpty())
{
    getWorld().addObject(new Ball(), getX(), getY()-50);
}
thanks but its not compiling somehow. pressing compile does nothing edit: it works regardless of that error. thank you really much! :D
You need to login to post a reply.