sprite.collidesWith throws IOBE

Using J2ME Wireless Toolkit 2.0 with one of the default emulators -

Colliding an animated Sprite object with an animated TiledLayer object is producing an IndexOutOfBoundsException from the source line

if (mySprite.collidesWith(tiledLayer, true))

If there is no collision, the line executes normally. Anybody else come across this in MIDP ?

[366 byte] By [ianreesa] at [2007-9-28 18:46:10]
# 1
Does anybody have an answer for this?I am having the same problem and this is the only postI can find where someone else did too.
xlx303a at 2007-7-12 17:00:54 > top of Java-index,Other Topics,Java Game Development...
# 2
More code needed.
mlka at 2007-7-12 17:00:54 > top of Java-index,Other Topics,Java Game Development...
# 3

Geez! You're still using tiles! I have developed a quick, pixel perfect method of collision detection that uses fat ray casting and rectangles. However, currently, it is only implemented in my C++ library, Fcatalog. Right now I'm in the process of porting some of my videogame routines to my Java library, Speedy. When I do I will let you know where you can download it.

FrancoisSofta at 2007-7-12 17:00:55 > top of Java-index,Other Topics,Java Game Development...
# 4

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;

}

}

xlx303a at 2007-7-12 17:00:55 > top of Java-index,Other Topics,Java Game Development...
# 5
I forgot to mention that once you get the code running you just need to holddown on the right arrow key until the cowboy runs into the first tumbleweed.This should throw an ArrayIndexOutOfBoundsException.
xlx303a at 2007-7-12 17:00:55 > top of Java-index,Other Topics,Java Game Development...
# 6
When you post code, please use [code] and [/code] tags as described in [url= http://forum.java.sun.com/features.jsp#Formatting]Formatting Help[/url] on the message entry page. It makes it much easier to read.
mlka at 2007-7-12 17:00:55 > top of Java-index,Other Topics,Java Game Development...
# 7
hi guys,im getting the same problem.collideswith generating IOBE when it hits an animated tile... no problems when it hits normal tiles...
boborsona at 2007-7-12 17:00:55 > top of Java-index,Other Topics,Java Game Development...
# 8
I submitted a bug to Sun for this on 9/10/04. They only replied once saying"Your report has been assigned an internal review ID of: 305615".I haven't heard from them since..
xlx303a at 2007-7-12 17:00:55 > top of Java-index,Other Topics,Java Game Development...
# 9
Heelo, i was just wondering if you actually got round to fixing the problem? because i am stuck on the same thing and need help. reply soon
RichieCrakka at 2007-7-12 17:00:55 > top of Java-index,Other Topics,Java Game Development...
# 10
Hello, i was wondering if you found a way to get around the problem? because i am getting the exact same problem. please reply soonRichie
RichieCrakka at 2007-7-12 17:00:55 > top of Java-index,Other Topics,Java Game Development...