touchEvent列表错误

时间:2013-06-03 10:01:14

标签: android frameworks

我已经按照本教程http://www.kilobolt.com/day-7-creating-an-android-game-from-start-to-finish.html了解了它,但是在尝试使用它的框架时遇到错误,

我有一个MainMenuScreen和一个LogoScreen

LogoScreen代码在这里

public class LogoScreen extends Screen
{
public LogoScreen(Game game)
{
    super(game);
}

@Override
public void update(float deltaTime)
{
    Graphics g = game.getGraphics();
    Assets.logoScreen = g.newImage("Logo_Screen.png", ImageFormat.RGB565);
    Assets.MainMenu = g.newImage("MainMenu.png", ImageFormat.RGB565);
    Assets.fiveBtn = g.newImage("5_btn.png", ImageFormat.RGB565);
    Assets.fiveBtnOff = g.newImage("5_btn_off.png", ImageFormat.RGB565);
    Assets.threeBtn = g.newImage("3_btn.png", ImageFormat.RGB565);
    Assets.threeBtnOff = g.newImage("3_btn_off.png", ImageFormat.RGB565);
    Assets.onBtn = g.newImage("On_btn.png", ImageFormat.RGB565);
    Assets.onBtnOff = g.newImage("On_btn_off.png", ImageFormat.RGB565);
    Assets.offBtn = g.newImage("Off_btn.png", ImageFormat.RGB565);
    Assets.offBtnOff = g.newImage("Off_btn_off.png", ImageFormat.RGB565);

    List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

    int len = touchEvents.size();
    for(int i = 0; i < len; i ++)
    {
        TouchEvent event = touchEvents.get(i); //<---error is here
        if(event.type == TouchEvent.TOUCH_DOWN)
        {
            game.setScreen(new MainMenuScreen(game));
            this.dispose();
        }
    }
}

@Override
public void paint(float deltaTime) 
{
    Graphics g = game.getGraphics();
    g.drawImage(Assets.logoScreen, 0, 0);
}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

@Override
public void backButton() 
{
    android.os.Process.killProcess(android.os.Process.myPid());
}
}

这是MainMenuScreen

public class MainMenuScreen extends Screen
{

Button soundOn = new Button();
Button soundOff = new Button();

public MainMenuScreen(Game game) 
{
    super(game);
    //Setup buttons
    soundOn.xPos = 438;
    soundOn.yPos = 138;
    soundOn.width = Assets.onBtn.getWidth();
    soundOn.height = Assets.onBtn.getHeight();
    soundOff.xPos = 529;
    soundOff.yPos = 138;
    soundOff.width = Assets.onBtn.getWidth();
    soundOff.height = Assets.onBtn.getHeight();
}

//Collision detection
private boolean inBounds(TouchEvent event, int x, int y, int width,int height) 
{
    if (event.x > x && event.x < x + width - 1 && event.y > y
            && event.y < y + height - 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

@Override
public void update(float deltaTime)
{
    //Update game
    Graphics g = game.getGraphics();
    List touchEvents = game.getInput().getTouchEvents();

    int len = touchEvents.size();
    for(int i = 0; i < len; i ++)
    {
        TouchEvent event = (TouchEvent) touchEvents.get(i);
        if(event.type == TouchEvent.TOUCH_DOWN)
        {
            if(inBounds(event, soundOn.xPos, soundOn.yPos, soundOn.width, soundOn.height))
            {
                if( Assets.sound == true )
                {
                    Assets.sound = false;
                }
                else
                {
                    Assets.sound = true;
                }
            }
        }
    }
}

@Override
public void paint(float deltaTime) 
{
    Graphics g = game.getGraphics();
    g.drawImage(Assets.MainMenu, 0, 0);
    if( Assets.sound == true )
    {
        g.drawImage(Assets.onBtn, soundOn.xPos, soundOn.yPos);
        g.drawImage(Assets.offBtnOff, soundOff.xPos, soundOff.yPos);
    }
    else
    {
        g.drawImage(Assets.onBtnOff, soundOn.xPos, soundOn.yPos);
        g.drawImage(Assets.offBtn, soundOff.xPos, soundOff.yPos);
    }
}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

@Override
public void backButton() 
{
    android.os.Process.killProcess(android.os.Process.myPid());
}

}

一切正常,但当用户按下屏幕进入MainMenuScreen时,我收到此错误

06-03 09:50:16.778: E/AndroidRuntime(317): FATAL EXCEPTION: Thread-10
06-03 09:50:16.778: E/AndroidRuntime(317): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
06-03 09:50:16.778: E/AndroidRuntime(317):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
06-03 09:50:16.778: E/AndroidRuntime(317):  at java.util.ArrayList.get(ArrayList.java:311)
06-03 09:50:16.778: E/AndroidRuntime(317):  at com.CC.ballO.LogoScreen.update(LogoScreen.java:39)
06-03 09:50:16.778: E/AndroidRuntime(317):  at com.CC.framework.implementation.AndroidFastRenderView.run(AndroidFastRenderView.java:55)
06-03 09:50:16.778: E/AndroidRuntime(317):  at java.lang.Thread.run(Thread.java:1096)

在我看来,与List有关,如果有人需要更多的代码只是评论,我会在这里更新。

干杯。

画布。

1 个答案:

答案 0 :(得分:0)

由于框架在更新中加载图像并检查触摸事件导致崩溃,我只是简单地将资源加载移动到构造函数中

public class LogoScreen extends Screen
{

int i = 0;

public LogoScreen(Game game)
{
    super(game);

    Graphics g = game.getGraphics();
    Assets.logoScreen = g.newImage("Logo_Screen.png", ImageFormat.RGB565);
    Assets.MainMenu = g.newImage("MainMenu.png", ImageFormat.RGB565);
    Assets.fiveBtn = g.newImage("5_btn.png", ImageFormat.RGB565);
    Assets.fiveBtnOff = g.newImage("5_btn_off.png", ImageFormat.RGB565);
    Assets.threeBtn = g.newImage("3_btn.png", ImageFormat.RGB565);
    Assets.threeBtnOff = g.newImage("3_btn_off.png", ImageFormat.RGB565);
    Assets.onBtn = g.newImage("On_btn.png", ImageFormat.RGB565);
    Assets.onBtnOff = g.newImage("On_btn_off.png", ImageFormat.RGB565);
    Assets.offBtn = g.newImage("Off_btn.png", ImageFormat.RGB565);
    Assets.offBtnOff = g.newImage("Off_btn_off.png", ImageFormat.RGB565);
}

@Override
public void update(float deltaTime)
{

    List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

    int len = touchEvents.size();
    for(int i = 0; i < len; i ++)
    {
        TouchEvent event = (TouchEvent) touchEvents.get(i);
        if(event.type == TouchEvent.TOUCH_DOWN)
        {
            game.setScreen(new MainMenuScreen(game));
        }
    }

    //i++;
    //if(i == 60)
    //{
    //  game.setScreen(new MainMenuScreen(game));
    //}
}

@Override
public void paint(float deltaTime) 
{
    Graphics g = game.getGraphics();
    g.drawImage(Assets.logoScreen, 0, 0);
}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

@Override
public void backButton() 
{
    android.os.Process.killProcess(android.os.Process.myPid());
}
}