我已遵循本教程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)
在我看来,这与列表有关,如果有人需要更多代码,只需发表评论,我会在这里更新。
干杯。
帆布。