全屏网格无法使用降落功能

时间:2018-11-09 17:05:28

标签: android libgdx

你好,所以我正在做一个像记忆矩阵之类的游戏,我被困在必须触摸瓷砖的地方。有时它可以工作,但不完全一样,例如当我触摸上方两块瓷砖时,底部的一块点亮。全部搞砸了!抱歉,我是编码和libgdx的初学者。这是逻辑:

package ksmd.tiles.Screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;
import ksmd.tiles.Tiles;
import ksmd.tiles.UI.Tile;

public class PlayScreen implements Screen, InputProcessor {

private Tile[][] tiles;
private OrthographicCamera camera;
private SpriteBatch batch;
private TextureRegion tex;
private float boardOffset;
private int TILE_SIZE;
private Tiles game;
private Vector3 mouse;
private final int MAX_FINGERS = 2;
private int boardHeight;

public PlayScreen(Tiles game) {
    this.game = game;
}

@Override
public void show() {
    Gdx.input.setInputProcessor(this);
    camera = new OrthographicCamera();
    camera.setToOrtho(false, Tiles.WIDTH, Tiles.HEIGHT);
    batch = new SpriteBatch();
    tiles = new Tile[5][5];
    TILE_SIZE = Tiles.WIDTH / tiles[0].length;
    boardHeight = TILE_SIZE * tiles.length;
    boardOffset = (Tiles.HEIGHT - (TILE_SIZE * tiles.length)) / 2;

    //Create the tiles
    for (int row = 0; row < tiles.length; row++) {
        for (int col = 0; col < tiles[0].length; col++) {
            tiles[row][col] = new Tile(col * TILE_SIZE + TILE_SIZE / 2, row * TILE_SIZE  + TILE_SIZE +boardOffset/ 2, TILE_SIZE, TILE_SIZE);
        }
    }

}

//render the Tiles
@Override
public void render(float delta) {
    batch.setProjectionMatrix(camera.combined);
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    for (int row = 0; row < tiles.length; row++) {
        for (int col = 0; col < tiles[0].length; col++) {
            tiles[row][col].render(batch, delta); // call the render method of each tile
        }
    }
    batch.end();
}

@Override
public void resize(int width, int height) {

}

@Override
public void dispose() {
    //dispose disposable objects
    batch.dispose();
}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void hide() {
}

@Override
public boolean keyDown(int keycode) {
    return false;
}

@Override
public boolean keyUp(int keycode) {
    return false;
}

@Override
public boolean keyTyped(char character) {
    return false;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    mouse = new Vector3(screenX, screenY, 0);
    for (int i = 0; i < MAX_FINGERS; i++) {
        if (Gdx.input.isTouched(i)) {
            mouse.x = Gdx.input.getX(i);
            mouse.y = Gdx.input.getY(i);
            camera.unproject(mouse);
            if (mouse.x > 0 && mouse.x < Tiles.WIDTH && mouse.y >= boardOffset && mouse.y < boardOffset + boardHeight) {
                int col = (int) (mouse.x/TILE_SIZE);
                int row = (int) ((mouse.y - boardOffset) / TILE_SIZE);
                tiles[row][col].setSelected(!tiles[row][col].getSelected());
            }
        }
    }
    return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    return false;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
    return false;
}

@Override
public boolean scrolled(int amount) {
    return false;
}

}

package ksmd.tiles.UI;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

import ksmd.tiles.Tiles;

public class Tile extends Box {

private TextureRegion lit;
private TextureRegion dark;
private boolean selected;

public Tile(float x, float y, int width, int height) {
    this.x = x;
    this.y = y * 1.55f;
    this.width = width - 8;
    this.height = height - 8;
    lit = Tiles.res.getAtlas("pack").findRegion("lit");
    dark = Tiles.res.getAtlas("pack").findRegion("dark");
    // set bounds of Sprite
}

public void setSelected (boolean b) {
    selected = b;
}

public boolean getSelected() {return selected; }

public void render(Batch batch, float delta) {
    if (selected) {
        batch.draw(lit, x - width / 2, y - width * 3, width, height * 1.60f);
    } else {
        batch.draw(dark, x - width / 2, y - width * 3, width, height * 1.60f);
    }

}

}

我想使研磨物充满整个屏幕,并在触摸时亮起瓷砖。目前,我已将其强行使用,使其充满了整个屏幕,但触摸时未正确选择瓷砖。谢谢!

1 个答案:

答案 0 :(得分:0)

使用网格之间的间隙处理瓷砖比看起来要困难。

首先,您需要决定一些事情。整个木板的尺寸是由单个瓷砖的尺寸决定的,还是瓷砖的尺寸取决于木板的尺寸?

  1. 平铺不应该扩展Box,平铺可以更好地用矩形表示,Box可以三维显示,而平铺则不能。 瓷砖应扩展com.badlogic.gdx.math.Rectangle。

2。我想Tiles.WIDTHTiles.HEIGHT指定了董事会的规模。 TILE_SIZE不等于Tiles.WIDTH / tiles[0].length,这不能是因为图块之间的间隙导致木板更大。

@Override
public void show() {
    Gdx.input.setInputProcessor(this);
    camera = new OrthographicCamera();
    batch = new SpriteBatch();
    tiles = new Tile[5][5];
    float gapSize = 10f;
    TILE_SIZE = ((Tiles.WIDTH - ((tiles[0].length-1) * gapSize ))) / tiles[0].length;
    boardOffset = 2;
    camera.viewportHeight = Tiles.HEIGHT + boardOffset;
    camera.viewportWidth = Tiles.WIDTH + boardOffset;
    camera.position.x = Tiles.WIDTH/2;
    camera.position.y = Tiles.HEIGHT/2;
    camera.update();
    //Create the tiles
    for (int row = 0; row < tiles.length; row++) {
        for (int col = 0; col < tiles[0].length; col++) {
            tiles[row][col] = new Tile(col * (TILE_SIZE + gapSize), row * (TILE_SIZE + gapSize) + boardOffset , TILE_SIZE, TILE_SIZE);
        }
    }
    Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 0);
}

我将Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 0);移到了show方法中,因为我看不到每个帧都设置相同颜色的原因。 另外,如果您已经操纵了camera.update()的组件,也请不要忘记。只需进行所有操作,然后在使用前进行更新。

3。我更改了Tile类

public class Tile extends Rectangle {

    private TextureRegion lit;
    private TextureRegion dark;
    private boolean selected;
    public Tile(float x, float y, float width, float height) {
        this.x = x;
        this.y = y;
        this.width = width ;
        this.height = height;
        lit = Tiles.getLit();
        dark = Tiles.getDark();
        // set bounds of Sprite
    }

    public void setSelected(boolean b) {
        selected = b;
    }

    public boolean getSelected() {
        return selected;
    }

    public void render(Batch batch, float delta) {
        if (selected) {
            batch.draw(lit, x, y , width, height);
        }
        else {
            batch.draw(dark, x, y , width, height);
        }

    }
}

图块的大小和位置由矩形表示,因此请在与矩形相同的位置并以相同的大小绘制。

与此处相同:

public Tile(float x, float y, float width, float height) {
    this.x = x;
    this.y = y;
    this.width = width ;
    this.height = height;
    lit = Tiles.res.getAtlas("pack").findRegion("lit");
    dark = Tiles.res.getAtlas("pack").findRegion("dark");
    // set bounds of Sprite
}

不更改数据。 4.我还修改了输入

@Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        if (pointer < 2) {
            mouse.x = screenX;
            mouse.y = screenY;
            camera.unproject(mouse);
            if (mouse.x > 0 && mouse.x < Tiles.WIDTH && mouse.y >= boardOffset && mouse.y < boardOffset + Tiles.HEIGHT) {
                for (int r = 0; r < tiles.length; r++) {
                    for (int c = 0; c < tiles.length; c++) {
                        if (tiles[r][c].contains(mouse.x, mouse.y)) {
                            tiles[r][c].setSelected(true);
                        }
                    }
                }
            }
        }
        return false;

    }

我检查每个图块的每个矩形,因为触碰点位于矩形内部。 也许有一种更快的方法,但是计算机是过去的日子。

另外,您的鼠标vector3可以是最终的。