1
\$\begingroup\$

So I'm trying to make a game in which you have to tap things fast, but the thing is - after you touch something (the area I chose), it takes like 2 more seconds to register another touch. Now, I think the problem is that I have too much things going on under the update method of some several classes at the same time, which causes this problem.

So to sum it up, there's a texture region that I want to detect if the user touches, and I do that using a method called "contains" (you'll see that in a second). But as I said above, I think that the delay is caused by too many things going on every second, and I'm looking for a way to get around that.

That's the input handling method:

public void handleInput() {
    if (Gdx.input.justTouched()) {
        mouse.x = Gdx.input.getX();
        mouse.y = Gdx.input.getY();
        cam.unproject(mouse);

        for (int x = 0; x < tiles.length; x++) {
            for (int y = 0; y < tiles[0].length; y++) {
                if (tiles[x][y].contains(mouse.x, mouse.y)) {
                    if (tiles[x][y].getLit() == true) {
                        tiles[x][y].setLit(false);
                        time = 0;
                        shouldGen = true;
                        shouldLoop = true;
                    }
                }
            }
        }
    }
}

And that's the Tile class:

public class Tile extends Box {

public static final int SIZE = 50;

public static final int TYPE_BLANK = 0;
public static final int TYPE_1 = 1;
public static final int TYPE_2 = 2;
public static final int TYPE_3 = 3;
public static final int TYPE_4 = 4;
public static final int TYPE_5 = 5;

private int type;
private boolean lit;

private ColorsHandler colorsHandler;
private TextureRegion blank, blue, green, orange, pink, red;

public Tile(int x, int y) {
    this.x = x;
    this.y = y;
    this.width = SIZE;
    this.height = SIZE;

    type = TYPE_BLANK;
    lit = false;

    colorsHandler = new ColorsHandler();
    blank = colorsHandler.getAtlas().findRegion("blank");
    blue = colorsHandler.getAtlas().findRegion("blue");
    green = colorsHandler.getAtlas().findRegion("green");
    orange = colorsHandler.getAtlas().findRegion("orange");
    pink = colorsHandler.getAtlas().findRegion("pink");
    red = colorsHandler.getAtlas().findRegion("red");
}

public int getType() {
    return type;
}

public void setType(int type) {
    this.type = type;
}

public boolean getLit() {
    return lit;
}

public void setLit(boolean lit) {
    this.lit = lit;
    this.type = TYPE_BLANK;
}

public void setPosition(float x, float y) {
    this.x = x;
    this.y = y;
}

public void render(SpriteBatch sb) {
    if (type == TYPE_BLANK) { // Blank
        sb.draw(blank, x, y);
    } else if (type == TYPE_1) { // Blue
        sb.draw(blue, x, y);
    } else if (type == TYPE_2) { // Green
        sb.draw(green, x, y);
    } else if (type == TYPE_3) { // Orange
        sb.draw(orange, x, y);
    } else if (type == TYPE_4) { // Pink
        sb.draw(pink, x, y);
    } else if (type == TYPE_5) { // Red
        sb.draw(red, x, y);
    } else {
        type = TYPE_BLANK;
    }
}

}

That's the Box class which has the "contains" method in it:

public class Box {

protected float x;
protected float y;
protected float width;
protected float height;

public boolean contains(float x, float y) {
    return x > this.x - width / 2 &&
            x < this.x + width / 2 &&
            y > this.y - height / 2 &&
            y < this.y + height / 2;
}

}

That's an "update" method which might cause lag:

public void update(float dt) {
    handleInput();

    int randx = 0;
    int randy = 0;

    time += dt;

    if (shouldLoop) {
        if (difficulty == 1) {
            if (time < 5.0f) {
                randx = MathUtils.random(tiles.length - 1);
                randy = MathUtils.random(tiles.length - 1);
                if (tiles[randx][randy].getLit() == false && shouldGen == true) {
                    tiles[randx][randy].setLit(true);
                    tiles[randx][randy].setType(Tile.TYPE_1);
                    shouldGen = false;
                }
            } else {
                time = 0;
                shouldLoop = false;
            }
        } else if (difficulty == 2) {

        } else if (difficulty == 3) {

        } else if (difficulty == 4) {

        } else if (difficulty == 5) {

        } else {
            difficulty = 1;
        }

        numLoops++;
        if (numLoops == 5) {
            if (difficulty < 5) {
                difficulty++;
            }
            numLoops = 0;
        }
    }
}

I also use Texture Regions in a ".pack" file to render out 64 boxes (50px by 50px rectangles) every frame - that might cause the delay (?)

If you'd like to see more of the code in case you have any ideas, just ask for it.

Thank you!

\$\endgroup\$
1
  • \$\begingroup\$ I reverted the edit deleting the question contents, since that would make it essentially useless to future visitors. If you wanted to signal that your question is solved, you can do that by marking an answer below as the accepted answer. If you wanted this question to disappear, there's a deletion link below it. \$\endgroup\$
    – Anko
    Commented Dec 30, 2014 at 16:19

1 Answer 1

0
\$\begingroup\$

does your FPS drop when pressing?

if so, if you are using Eclipse you should try using the DDMS perspective. see what part of your code takes most of the cpu time.

if the slow part is passing on every box to check 'contains', dont do it. just calculate what box is in the position pressed.

i dont know if your game shows boxes in a grid or not, but for grids it simple. just figured out what cell is in the position pressed. otherwise you might want to look into Spatial Partition

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Thanks. I solved it by calculating the position pressed in a different way rather than the "contains" method. Appreciate the tip :) \$\endgroup\$
    – Orel
    Commented Dec 26, 2014 at 11:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .