I’m attempting to get this actionListener to work correctly for my current project however i’ve ran into a couple of logic issues. I’ve been playing with it for a while and this is the closest I’ve gotten after approaching it from 2 different angles. Everything seems fine however their tends to be a slight issue with certain button sequences for example tapping Left,Down,Right quickly should produce Left,Left-Down,Down,Right-Down,Right, however i get [Left, Down, Down-Right, Right]
going further Left,Down,Right,Attack4+Attack3 will produce [Left, Down, Attack4-Right]
any assistance will be appreciated and i posted in case someone could find this snippet useful
IT SHOULD BE KNOWN THIS ISN’T THE WHOLE CLASS AnimationInputController.java ONLY THE PROBLEM SECTIONS
also player is passed from another class its jst the string of the name of the account to the user who controls the character
[java]
//////////////////////////CONTROLS/////////////////
private float time;
private final java.util.ArrayList buttonList = new java.util.ArrayList();
private final float delayInterval = .2f;
public void setActiveStyle(Style newStyle){
activeStyle = newStyle;
idle=false;
performAction(null);
activeStyle.getAnimController().addListener(new AnimEventListener(){
@Override
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName){
if(!idle){
performAction(null);
}
}
@Override
public void onAnimChange(AnimControl control, AnimChannel channel, String animName){
curAnim=animName;
}
});
}
/**
- Recieves and analyzes input
*/
private ActionListener actionator = new ActionListener(){
private int caseNumber=0; //An integer to determine which case to perform
private int prevCaseNumber=0; //The previous case Number
private String currentButton=""; //The button to be evaluated
private String previousButton=""; //The previous button evaluated
@Override
public void onAction(String name, boolean value, float tpf){
if(name.equals(player+"-comboMod")) {comboMod=value;return;}
//////This Section Evaluates once a button is pressed//////////
else if(value == true)
{
caseNumber = caseNumber(name);
if(caseNumber!=0)buttonPressed();
/////This Section Evaluates once a button is released//////////
}else{
caseNumber = caseNumber(name);
if(caseNumber!=0)buttonReleased();
}
//This resets all variables
reset();
}
/**
*
*/
private int caseNumber(String name){
if(name.equals(player+"-Attack1")){
currentButton=“Attack1”;
return 1;
}else if(name.equals(player+"-Attack2")){
currentButton=“Attack2”;
return 2;
}else if(name.equals(player+"-Attack3")){
currentButton=“Attack3”;
return 3;
}else if(name.equals(player+"-Attack4")){
currentButton=“Attack4”;
return 4;
}else if(name.equals(player+"-Up")){
currentButton=“Up”;
return 5;
}else if(name.equals(player+"-Down")){
currentButton=“Down”;
return 6;
}else if(name.equals(player+"-Left")){
currentButton=“Left”;
return 7;
}else if(name.equals(player+"-Right")){
currentButton=“Right”;
return 8;
}else if(name.equals(player+"-specialMod")){
currentButton=“SpecialMod”;
return 9;
}
return 0;
}
private void reset(){
prevCaseNumber=caseNumber;
if(caseNumber != 0)caseNumber = 0;
previousButton=currentButton;
currentButton="";
}
private void buttonPressed(){
if(buttonList.isEmpty()){
buttonList.add(currentButton);
time=timer.getTimeInSeconds();
} else {
float curTime=timer.getTimeInSeconds();
if(time>=curTime-delayInterval/2){
String button = prevCaseNumber<caseNumber?
previousButton+"-"+currentButton:currentButton+"-"+previousButton;
//This is special block for directional buttons
if(caseNumber>4){
buttonList.add(button);
time=timer.getTimeInSeconds();
//This Is special block for attack Buttons
} else if (caseNumber < 5){
int size= buttonList.size();
if(size>1&&buttonList.get(size-2).contains(“Attack”)) {
buttonList.set(size-2, button);
buttonList.remove(size-1);
buttonList.trimToSize();
} else
buttonList.set(size-1, button);
//Because Two Attack Button Pressed Automatically perform animation
System.out.println(“what is going in:”+buttonList);
performAction(buttonList);
buttonList.clear();
}
}else if(time>curTime-delayInterval){
buttonList.add(currentButton);
time=timer.getTimeInSeconds();
}
}
}
public void buttonReleased(){
if(!buttonList.isEmpty()&& caseNumber>4
&& time>timer.getTimeInSeconds()-delayInterval){
String lastButton = buttonList.get(buttonList.size()-1);
if(lastButton.contains("-")&&lastButton.contains(currentButton)
&& !lastButton.contains(“Attack”)){
String[] buttons = lastButton.split("-");
buttonList.add(
currentButton.equals(buttons[0])?
buttons[1]:buttons[0]
);
}
}
}
};
[/java]