My key binding code is having this odd error where a message repetitively pops up saying "index is big enough" even though it keeps printing the stack saying:
java.lang.ArrayIndexOutOfBoundsException: 35
at Options.setBinding(Options.java:44)
at KeyBindingSelection.act(KeyBindingSelection.java:28)
at greenfoot.core.Simulation.actActor(Simulation.java:565)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
at greenfoot.core.Simulation.runContent(Simulation.java:213)
at greenfoot.core.Simulation.run(Simulation.java:203)
The problem is that... well the array is big enough! I didn't program that index is big enough message. Is that a debug message for greenfoot?
I would really like an explanation...
Also, here are my two classes:
import greenfoot.*;
import java.awt.Color;
import javax.swing.JOptionPane;
public class Options extends World
{
UserInfo info;
String[] keys = {"","a","b","c","d","e","f","g","h","i","j","k","l",/*"m",*/"n",/*"o","p",*/"q","r","s","t","u","v","w","x","y","z","left","right","up","down","enter","space","tab","escape","backspace","shift","control"};
String[] binds = {"a","d","w","s","q","e","up","left","right","space"};
public Options()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
info = UserInfo.getMyInfo();
loadBindings();
addObject(new MenuSelection("Player-1","","","",30,Color.WHITE,Color.YELLOW),getWidth()/4,20);
addObject(new KeyBindingSelection("Move Left",15,binds[0]),getWidth()/4,70);
addObject(new MenuSelection("Main Menu","","","MainMenu",30,Color.WHITE,Color.YELLOW),getWidth()/2,getHeight()-30);
}
public void saveBindings(){
String s="";
for (int i=0; i<binds.length; i++){
s+=binds[i]+",";
}
info.setString(0,s);
info.store();
}
String bindings="";
public void loadBindings(){
UserInfo info=UserInfo.getMyInfo();
bindings=info.getString(0);
System.out.println(bindings);
if (bindings.indexOf(",")==-1)return;
binds[0]=getBind();//left
binds[1]=getBind();//right
binds[2]=getBind();//up
binds[3]=getBind();//down
binds[4]=getBind();//left sholder
binds[5]=getBind();//right sholder
binds[6]=getBind();//jump
binds[7]=getBind();//attack
binds[8]=getBind();//dash
binds[9]=getBind();//pause/menu
}
public String getBind(){
bindings=clip(bindings);
if (bindings.indexOf(",")>=0){JOptionPane.showMessageDialog(null,"index is big enough"); return bindings.substring(0,bindings.indexOf(","));}
return "";
}
public String clip(String s){if (s.indexOf(",")>-1){return s.substring(s.indexOf(",")+1);}return "";}
public String setBinding(String action){
int i=0;
while (true){
boolean detect=false;
for (i=0; i<keys.length; i++){
if (i>0&&Greenfoot.isKeyDown(keys[i])){
detect=true;
break;
}
}
if (detect)break;
i=0;
}
if (action.equals("Left"))binds[0]=keys[i];
if (action.equals("Right"))binds[1]=keys[i];
if (action.equals("Up"))binds[2]=keys[i];
if (action.equals("Down"))binds[3]=keys[i];
if (action.equals("Left Sholder"))binds[4]=keys[i];
if (action.equals("Right Sholder"))binds[5]=keys[i];
if (action.equals("Jump"))binds[6]=keys[i];
if (action.equals("Shoot"))binds[7]=keys[i];
if (action.equals("Dash"))binds[8]=keys[i];
if (action.equals("Pause/Menu"))binds[9]=keys[i];
saveBindings();
return keys[i];
}
}import greenfoot.*;
import java.awt.Color;
import javax.swing.JOptionPane;
public class KeyBindingSelection extends MouseButtons
{
String name;
int size;
Color normal=Color.WHITE;
Color highlighted=Color.YELLOW;
String bind="";
public KeyBindingSelection(String i,int s, String k){
name=i;
size=s;
bind=k;
}
UserInfo info;
public void act()
{
info=UserInfo.getMyInfo();
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse!=null&&mouse.getX()>getX()-getImage().getWidth()/2&&mouse.getX()<getX()+getImage().getWidth()/2&&mouse.getY()>getY()-getImage().getHeight()/2&&mouse.getY()<getY()+getImage().getHeight()/2){
updateImage(highlighted);
}else{
updateImage(normal);
}
if (Greenfoot.mouseClicked(this)){
bind=((Options)getWorld()).setBinding(name);
}
}
public void updateImage(Color c){
setImage(new GreenfootImage(name+" | "+bind,size,c,new Color(0,0,0)));
}
}

