You should use interface
s.
Make an interface something like:
public interface Something
{
void doSomething(GameObject object);
}
Where doSomething
is the method which will be called (In this case could be objectCollision
) same for interface name (ObjectCollider
(?)).
then let GameScene implement it
public class GameScene implements Something
and implement the method.
In the Player class, add a listener to this:
public class Player {
private Something listener;
And in the constructor ask for the listener:
public Player(Something listener) { this.listener = listener; }
Then to invoke it, just use
listener.doSomething(object);
Example:
public void Collision(GameObject object) {
if(object instanceof Coin) {
//throws an event here
listener.doSomething(object);
}
}
To create the Player object with this constructor you just need to do:
Player player = new Player(this); // if you implement the method in the class
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…