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

2017/7/1

Get Location from another actor

Aendi Aendi

2017/7/1

#
I'm programming a game, in which you move the rocket with the mouse up and down, that means the x-coordinate is always the same. When you press space a laser shot should come out of the rocket. How can I get the y-coordinate of the rocket in the laser shot class?
Super_Hippo Super_Hippo

2017/7/1

#
Your laser shot doesn't need the rocket's location. The rocket should just spawn the shot at its location using getX and getY.
Aendi Aendi

2017/7/1

#
yes but how can I do this?
danpost danpost

2017/7/2

#
Answer this: when does the laser need the y-coordinate of the rocket ... and why does it need it at that time?
Aendi Aendi

2017/7/2

#
when i press space a moving laser appears. I did this with a method in the world class, that works well. The problem is, that I'm not able to set the y (now on 400) to the same value as the rocket is.
 if ("space".equals(Greenfoot.getKey()))
        {
            Laser laser = new Laser();
            addObject(laser, 1288, 400);
        }
Super_Hippo Super_Hippo

2017/7/2

#
Put the code into the rocket's act method and you can do:
addObject(new Laser(), getX(), getY());
Aendi Aendi

2017/7/2

#
thank you very much it worked! with this code:
 if ("space".equals(Greenfoot.getKey()))
        {
            Laser laser = new Laser();
            getWorld().addObject(new Laser(), getX(), getY());
        }
Super_Hippo Super_Hippo

2017/7/2

#
You can remove line 3 without changing anything.
You need to login to post a reply.