我正在使用FitViewport,以便我的" play ";和";options"当我放大游戏窗口时,按钮不会改变大小。我还创建了蓝色的PLAY_ACTIVE按钮和红色的PLAY_INACTIVE按钮。这两个都可以,但是如果我最大化窗口,颜色不会改变。例如,当指针处于"播放"状态时,它会改变颜色,但当我将屏幕加宽并将指针置于"播放"状态时,它不会改变颜色。
我代码:
1)主要:
package com.mygdx.game;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.mygdx.game.screen.MainMenuScreen;
public class NameGame extends Game {
public static final int WIDTH = 900;
public static final int HEIGHT = 780;
public SpriteBatch batch;
private OrthographicCamera camera;
private FitViewport viewport;
@Override
public void create() { //Apertura app
batch = new SpriteBatch();
camera = new OrthographicCamera();
viewport = new FitViewport(800, 800, camera);
camera.position.set(WIDTH/2,HEIGHT/2,0);
camera.update();
this.setScreen(new MainMenuScreen(this)); //Visualizza menu
}
@Override
public void render(){ //Esecuzione app
batch.setProjectionMatrix(camera.combined);
super.render();
}
@Override
public void resize(int width, int height){
viewport.update(width,height);
super.resize(width, height);
}
}
2) MainMenuScreen:
package com.mygdx.game.screen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.mygdx.game.NameGame;
public class MainMenuScreen implements Screen{
private static final int PLAY_BUTTON_WIDTH = 140;
private static final int PLAY_BUTTON_HEIGHT = 62;
private static final int OPTIONS_BUTTON_WIDTH = 140;
private static final int OPTIONS_BUTTON_HEIGHT = 60;
private static final int PLAY_BUTTON_Y = 100;
private static final int OPTIONS_BUTTON_Y = 30;
NameGame game;
Texture playButtonActive;
Texture playButtonInactive;
Texture optionsButtonActive;
Texture optionsButtonInactive;
public MainMenuScreen (NameGame game){
this.game = game;
playButtonActive = new Texture("play1.png");
playButtonInactive = new Texture("play2.png");
optionsButtonActive = new Texture("options1.png");
optionsButtonInactive = new Texture("options2.png");
}
@Override
public void show() {
}
@Override
public void render(float f) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.begin();
//Play
int x = NameGame.WIDTH / 2 - PLAY_BUTTON_WIDTH / 2 ;
if(Gdx.input.getX() < x + PLAY_BUTTON_WIDTH && Gdx.input.getX() > x && NameGame.HEIGHT - Gdx.input.getY() < PLAY_BUTTON_Y + PLAY_BUTTON_HEIGHT && NameGame.HEIGHT - Gdx.input.getY() > PLAY_BUTTON_Y){
game.batch.draw(playButtonActive, x, PLAY_BUTTON_Y, PLAY_BUTTON_WIDTH, PLAY_BUTTON_HEIGHT);
} else {
game.batch.draw(playButtonInactive, NameGame.WIDTH/2 - PLAY_BUTTON_WIDTH/2, PLAY_BUTTON_Y, PLAY_BUTTON_WIDTH, PLAY_BUTTON_HEIGHT);
}
//Options
game.batch.draw(optionsButtonInactive, NameGame.WIDTH/2 - OPTIONS_BUTTON_WIDTH/2, OPTIONS_BUTTON_Y, OPTIONS_BUTTON_WIDTH, OPTIONS_BUTTON_HEIGHT);
game.batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
按钮不会改变颜色,因为坐标是错误的。Gdx的getX和getY方法。输入将返回屏幕坐标:
触摸屏设备上最后一次触摸的x坐标和屏幕坐标中第一个指针在桌面上的当前鼠标位置. 屏幕原点在左上角。
因为你想要世界坐标而不是屏幕坐标您需要根据摄像头取消鼠标在屏幕上的投影位置:
Vector3 mousePosition = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
// change screen coordiates to world coordinates by using the camera.unproject method
Vector3 unprojectedMousePosition = game.camera.unproject(mousePosition);
// check the position in world coordinates
if(unprojectedMousePosition.x < x + PLAY_BUTTON_WIDTH && unprojectedMousePosition.x > x && NameGame.HEIGHT - unprojectedMousePosition.y < PLAY_BUTTON_Y + PLAY_BUTTON_HEIGHT && NameGame.HEIGHT - unprojectedMousePosition.y > PLAY_BUTTON_Y){
game.batch.draw(playButtonActive, x, PLAY_BUTTON_Y, PLAY_BUTTON_WIDTH, PLAY_BUTTON_HEIGHT);
} else {
game.batch.draw(playButtonInactive, NameGame.WIDTH/2 - PLAY_BUTTON_WIDTH/2, PLAY_BUTTON_Y, PLAY_BUTTON_WIDTH, PLAY_BUTTON_HEIGHT);
}
// ...