Split Images Using createImage() on Sagem My600v

I've been working a lot with a game developed by a company who are trying there hand at J2ME for the first time. Despite having a tight code review document, the game still somehow managed to show up on my desk using methods that are known to cause issues with portability. Image.createImage(Image, int, int, int, int, int) is one of these methods ... and as it turns out, the Sagem My600v is one of the devices affected.

The observed issue is that the method will incorrectly parse a sprite sheet and return an unexpected image. In the case of this application the sprite sheet was an image containing three rows of three ball, all equispaced. When any ball other than that from the top row is required, the function returns the ball from the top row directly above it. This would suggest that the y value either defaults to zero or to the value that was used when the function was first called. Either way, it doesn't work and shouldn't be used to increase portability.

A more portable method is to use a typical clipping implementation. This would involve loading a sprite sheet at initialization time and when one of the sprites is required, set the clip area at the correct size and render the entire image at an offset that positions the required sprite to be drawn in the clipping area.

// Calculate the top left corner of the clip
int x = (getWidth() >> 1) - ( spriteWidthOnSheet >> 1);
int y = ( spriteHeightOnSheet >> 1 );
// Set clip according to images dimensions
g.setClip( x, y, spriteWidthOnSheet, spriteHeightOnSheet );
// Shift x and y values to position the image so the correct ball is shown
x -= spritePositionOnSheetX;
y -= spritePositionOnSheetY;
// Render the image
g.drawImage( spriteSheetImage, x, y , 0 );

- FIN -


About this entry