I'm fairly new to Android and Java and I'm working on my first game.
The game seems to temporarily stop every 6-8 seconds for about half a second. I'm working on a platform game where timing is everything, so glitches make the game unplayable :(
Is this a known issue with garbage collection, or some background task? I've tried booting to safe mode and using a different phone but does the same thing.
Loading the sprite:
public void load() {
mysprite = new Sprite(this);
mysprite_image = new Texture(this);
if (!mysprite_image.loadFromAsset("mysprite_sprite_sheet.png")) {
fatalError("Error loading sprite_sheet");
}
mysprite.setTexture(mysprite_image);
}
The main loop of my test program just does this:
public void draw() {
canvas = getCanvas();
canvas.drawColor(Color.BLACK);
for (int i=0; i<20; i++) {
if (x[i] + vx[i] <= 0 || x[i] + vx[i] + 60 >= screenWidth) vx[i] *= -1;
if (y[i] + vy[i] <= 0 || y[i] + vy[i] + 60 >= screenHeight) vy[i] *= -1;
x[i] += vx[i];
y[i] += vy[i];
mysprite.position = new Point(x[i], y[i]);
drawSheetFrame(mysprite, 20, 29, 1, myspriteFrame, 4);
}
}
draw() is called in-between locking and unlocking the canvas.
As you already know that it is a bad behaviour to do new objects in the onDraw() method, and in this code you even call new objects in a loop! So GC will drag you slow from time to time.
To avoid this, you would create a "point poll" out side the onDraw() method, and create/cache the instances as long as you need them.