Here is some code I pieced together to reproduce the error.
There are 2 classes. CollisionTestMidlet which extends MIDlet and CollisionTestCanvas that
extends GameCanvas.Line 70 of CollisionTestCanvas is where the error occurs.
You can get the images it uses at
http://www.microdevnet.com/articles/images/cowboy.png
and
http://www.microdevnet.com/articles/images/tumbleweed.png
They are from a tutorial that has nothing to do with my game.
I am using MIDP 2.0 and J2ME Wireless Toolkit 2.1
Thanks in advance for any light that can be shed on this issue.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CollisionTestMidlet.java
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class CollisionTestMidlet extends MIDlet implements CommandListener
{
private CollisionTestCanvas collisionTestCanvas;
protected void startApp()
{
if (collisionTestCanvas == null)
{
try
{
collisionTestCanvas = new CollisionTestCanvas();
collisionTestCanvas.start();
Command exitCommand = new Command("Exit", Command.EXIT, 0);
collisionTestCanvas.addCommand(exitCommand);
collisionTestCanvas.setCommandListener(this);
}
catch (IOException ioe)
{
System.out.println(ioe);
}
}
Display.getDisplay(this).setCurrent(collisionTestCanvas);
}
protected void pauseApp()
{
}
protected void destroyApp(boolean arg0)
{
if (collisionTestCanvas != null)
collisionTestCanvas.stop();
}
public void commandAction(Command c, Displayable s)
{
if (c.getCommandType() == Command.EXIT)
{
destroyApp(true);
notifyDestroyed();
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CollisionTestCanvas.java
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class CollisionTestCanvas extends GameCanvas implements Runnable
{
private static final int COWBOY_FRAME_WIDTH = 32;
private static final int COWBOY_FRAME_HEIGHT = 48;
private static final int COWBOY_FRAME_COUNT = 4;
private static final int TUMBLEWEED_FRAME_WIDTH = 16;
private static final int TUMBLEWEED_FRAME_HEIGHT = 16;
private static final int TUMBLEWEED_FRAME_COUNT = 3;
private static final int TUMBLEWEED_COUNT = 3;
private boolean isAlive;
private Sprite cowboySprite;
private TiledLayer tumbleweedLayer;
private int animatedTileIndex;
private LayerManager layerManager;
public CollisionTestCanvas() throws IOException
{
super(true);
layerManager = new LayerManager();
createCowboy();
createTumbleweed();
}
private void createCowboy() throws IOException
{
Image image = Image.createImage("/cowboy.png");
cowboySprite = new Sprite(image, COWBOY_FRAME_WIDTH, COWBOY_FRAME_HEIGHT);
cowboySprite.setPosition(0, getHeight() - COWBOY_FRAME_HEIGHT);
layerManager.append(cowboySprite);
}
private void createTumbleweed() throws IOException
{
Image image = Image.createImage("/tumbleweed.png");
tumbleweedLayer = new TiledLayer(TUMBLEWEED_COUNT, 1, image, TUMBLEWEED_FRAME_WIDTH, TUMBLEWEED_FRAME_HEIGHT);
animatedTileIndex = tumbleweedLayer.createAnimatedTile(1);
for (int i = 0; i < TUMBLEWEED_COUNT; i++)
{
tumbleweedLayer.setCell(i, 0, animatedTileIndex);
}
tumbleweedLayer.setAnimatedTile(animatedTileIndex, 1);
tumbleweedLayer.setPosition(getWidth() - (TUMBLEWEED_FRAME_WIDTH*TUMBLEWEED_COUNT), getHeight() - TUMBLEWEED_FRAME_HEIGHT);
layerManager.append(tumbleweedLayer);
}
private void input()
{
int keyStates = getKeyStates();
if ((keyStates & RIGHT_PRESSED) != 0)
{
cowboySprite.move(3, 0);
cowboySprite.nextFrame();
}
}
private void render(Graphics g)
{
// Rectangle collision has no problem
if (cowboySprite.collidesWith(tumbleweedLayer, false))
{
System.out.println("Cowboy and tumbleweed rectangle collision");
}
// Pixel collison throws an ArrayIndexOutOfBoundsException
if (cowboySprite.collidesWith(tumbleweedLayer, true))
{
System.out.println("Cowboy and tumbleweed pixel collision");
}
g.setColor(0xFFFFFF);
g.fillRect(0, 0, getWidth(), getHeight());
tumbleweedLayer.setAnimatedTile(animatedTileIndex,
((tumbleweedLayer.getAnimatedTile(animatedTileIndex) + 1) % TUMBLEWEED_FRAME_COUNT) + 1);
layerManager.paint(g, 0, 0);
flushGraphics();
}
public void start()
{
Thread t = new Thread(this);
isAlive = true;
t.start();
}
public void run()
{
Graphics g = getGraphics();
int timeStep = 80;
while (isAlive)
{
long start = System.currentTimeMillis();
input();
render(g);
long end = System.currentTimeMillis();
int duration = (int)(end - start);
if (duration < timeStep)
{
try {Thread.sleep(timeStep - duration);}catch (InterruptedException ie){}
}
}
}
public void stop()
{
isAlive = false;
}
}