Ok, I’ll put the code first hopefully to make it clearer.
***Edit:
Problem is solved, by passing the instance of the sequence to the dialog when the sequence gets created, then the dialog has the internal reference to call.
public abstract class RavelSequence {
protected Dialog dialog; //every subclass uses one of these objects
public abstract void hit();
}
public class BattleSequence extends RavelSequence {
public void init(){ //this is the edited fix
dialog.setSequence(this); //
} //
public void hit(){ //the effect of a 'hit' in battle
doSomething();
}
}
public class OutOfBattleSequence extends RavelSequence {
public void init(){ //this is the edited fix
dialog.setSequence(this); //
} //
public void hit(){ //the effect of a 'hit' outside battle
doSomethingElse();
}
}
public class Dialog extends Container implements Runnable {
private RavelSequence sequence; //this is the edited fix
public void run (){
if (somethingHappens)
sequence.hit();
}
public void setSequence (RavelSeqence sequence){ //this is the edited fix
this.sequence = sequence; //
} //
}
What I want to happen is for the Dialog to be able to call the method hit() implemented in whichever class owns the instance of Dialog. I am using IntelliJ IDEA and it tells me that the ‘non-static method hit cannot be referenced from a static context.’
The whole thing is run inside an application which creates instances of the Sequence objects depending on the context of the game, so hit will need to reference non-static objects within the Sequence.
You’re Dialog object can’t know which RavelSequence it’s part of. So what you’re trying to do is not possible that way. Include a RavelSequence in your Dialog and it will work fine.
for example: