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

2022/5/26

spawning actors on top of moving actors

tedyy tedyy

2022/5/26

#
Im trying to make it so that the Laser spawn periodically on the Ship. I almost have it down, but for some reason it keeps saying getX() and getY() are undeclared methods and idk how to fix it. Here is my current code: public class Space extends World { private int spawnTimer; private void checkForSpawning() { spawnTimer = (spawnTimer+1)%200; if (spawnTimer == 0) { addObject(new Laser(),getObjects(Ship.class).getX(), getObjects(Ship.class).getY()-10); } } public Space() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); prepare(); } private void prepare() { Ship ship = new Ship(); addObject(ship,320,194); ship.setLocation(321,219); ship.setLocation(313,293); } }
Roshan123 Roshan123

2022/5/26

#
Declare ship variable as a global variable and then use this global variable to get its coordinates by writing ship.getX() and ship.getY()
danpost danpost

2022/5/26

#
The error occurs because getObjects(Shit.class) returns a List object, not a specific Actor object, and getX() and getY() are only declared in the Actor class as instance methods. That is, you cannot use those methods on a List instance. You must first make sure that the list is not empty. Then, if not, extract a ship from the list and use the methods on it.
You need to login to post a reply.