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

2012/10/20

Help

Stephon231 Stephon231

2012/10/20

#
i have an actor(PowerUp) that when destoried it is suppost to tell another actor(Ship) to execute the method gotNuke() but i cant get the right code for the for the PowerUp to tell the Ship to exectue the gotNuke() method i have tried using Ship.gotNuke(); but i keep getting an error message. please help!
danpost danpost

2012/10/20

#
You cannot execute a non-static method without an object of that type (or, you cannot execute a non-static method with a class name). You need to get a reference of an object of that class and execute the method on that object. I am hoping you only have one Ship object in the world for the following code:
Ship ship = (Ship) getWorld().getObjects(Ship.class).get(0);
ship.gotNuke();
Again, you must have one, and only one, ship in the world for this code. Otherwise, you need the following check: if (getWorld().getObjects(Ship.class).isEmpty()) Do not use the previous code if true.
You need to login to post a reply.