gpt4 book ai didi

java - libgdx 制作触摸事件

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:47 25 4
gpt4 key购买 nike

我目前正在关注 this书来创建我的 libgdx 游戏..
到目前为止,我有这些类来创建我的游戏:

  • AbstractGameScreen.java - 实现 libgdx 的屏幕
  • AbstractGameObject.java - 扩展 libgdx 的 Actor

  • GamePlay.java - 这是我的游戏屏幕
  • GameController.java - 我初始化游戏对象的地方
  • GameRenderer.java - 当我渲染所有对象时
  • Assets.java - 组织我的游戏 Assets 的类
  • Ring.java - 我的游戏中的一个对象


这是我的代码..

抽象游戏屏幕

public abstract class AbstractGameScreen implements Screen {
protected Game game;

public AbstractGameScreen(Game game){
this.game = game;
}

public abstract void render(float deltaTime);
public abstract void resize(int width, int height);
public abstract void show();
public abstract void hide();
public abstract void pause();

public void resume(String bg, String ring){
Assets.instance.init(new AssetManager(), bg, ring); // kosongin, jadinya default
}

public void dispose(){
Assets.instance.dispose();
}
}

抽象游戏对象

public abstract class AbstractGameObject extends Actor{ //implements EventListener{  //extends Sprite{
public Vector2 position;
public Vector2 dimension;
public Vector2 origin;
public Vector2 scale;
public float rotation;

public AbstractGameObject(){
position = new Vector2();
dimension = new Vector2(1, 1);
origin = new Vector2();
scale = new Vector2(1, 1);
rotation = 0;

}

public void update (float deltaTime){

}

public abstract void render(SpriteBatch batch);
}

游戏玩法

public class GamePlay extends AbstractGameScreen implements InputProcessor{

private GameController gameController;
private GameRenderer gameRenderer;
private String dummyBg, dummyRing;
private boolean paused;

// for touch purposes
private static final int appWidth = Constants.VIEWPORT_GUI_WIDTH_INT;
private static final int appHeight = Constants.VIEWPORT_GUI_HEIGHT_INT;

public GamePlay(Game game) {
super(game);

// still dummy.. nantinya ngambil dari database nya
this.dummyBg = "bg-default";
this.dummyRing = "ring-default";

Gdx.input.setInputProcessor(this);
}

@Override
public void render(float deltaTime) {
if(!paused){
gameController.update(deltaTime);
}

//Gdx.gl.glClearColor(0x64 / 255.0f, 0x95 / 255.0f,0xed / 255.0f, 0xff / 255.0f);
//Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

// render game nya
gameRenderer.render();
}

@Override
public void resize(int width, int height) {
gameRenderer.resize(width, height);
}

@Override
public void show() {
Assets.instance.init(new AssetManager(), "bg-default", "ring-default");
Gdx.app.log("GamePlay", "After show() method");

gameController = new GameController(game);
gameRenderer = new GameRenderer(gameController);
Gdx.input.setCatchBackKey(true);
}

@Override
public void hide() {
gameRenderer.dispose();
Gdx.input.setCatchBackKey(false);
}

@Override
public void dispose(){
gameRenderer.dispose();
Assets.instance.dispose();
}

@Override
public void pause() {
paused = true;
}

@Override
public void resume() {
super.resume(this.dummyBg, this.dummyRing);
//Assets.instance.init(new AssetManager(), this.dummyBg, this.dummyRing);

paused = false;
}

@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) {

return true;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {

return true;
}

// for touch purposes
private float getCursorToModelX(int screenX, int cursorX) {
return (((float)cursorX) * appWidth) / ((float)screenX);
}

private float getCursorToModelY(int screenY, int cursorY) {
return ((float)(screenY - cursorY)) * appHeight / ((float)screenY) ;
}

@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;
}
}

游戏 Controller

public class GameController extends InputAdapter{
// game objects
public Array<Tiang> tiangs;
public Array<Ring> rings;
//private Game game;

// game decorations
public Background background; public Sprite[] testSprite;
private Game game;

public GameController(Game game){
this.game = game;
init();
}

private void init(){
// preparing variables
rings = new Array<Ring>();
tiangs = new Array<Tiang>();

initObjects();
initDecorations();
initGui();
}

private void initObjects(){
AbstractGameObject obj = null;
obj = new Ring(1, "default");

obj.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
//super.clicked(event, x, y);
Gdx.app.log("tag", "test clisk");
}
});

rings.add((Ring)obj);
}

private void initDecorations(){

}

private void initGui(){

}

private void handleInput(float deltatime){

}

public void update(float deltaTime){
//if(Gdx.input.isTouched()){
// Gdx.app.log("update", "screen touched");
//}
}
}

游戏渲染器

public class GameRenderer implements Disposable{

private OrthographicCamera camera;
private GameController controller;
private SpriteBatch batch;

public GameRenderer(GameController controller){
this.controller = controller;
init();
}

private void init(){
batch = new SpriteBatch();
camera = new OrthographicCamera(Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT); //diambil dari class "Constants" (di package util)
camera.position.set(0, 0, 0);
camera.update();
}

public void resize(int width, int height){
camera.viewportWidth = (Constants.VIEWPORT_HEIGHT / height) * width;
camera.update();
}

public void render(){
renderGui();
renderDecorations();
renderObjects();
}

private void renderGui(){

}

private void renderDecorations(){

}

private void renderObjects(){
batch.setProjectionMatrix(camera.combined);
batch.begin();

for(Ring rings : controller.rings){
rings.render(batch);
}

batch.end();
}

@Override
public void dispose() {

}
}

Assets

public class Assets implements Disposable, AssetErrorListener{
public static final String TAG = Assets.class.getName();
public static final Assets instance = new Assets();

private AssetManager assetManager;

// inner class objects
public AssetTiang tiang;
public AssetBackgroud bg;
public AssetTombol tombol;
public AssetTombolBg tombolBg;
public AssetRing ring;

//singleton pattern, buat mencegah instansiasi dari class yang lain
private Assets(){}

//public void init(AssetManager assetManager){
public void init(AssetManager assetManager, String jenisBg, String jenisRing){
this.assetManager = assetManager;
assetManager.setErrorListener(this);

//load texture atlas yang udah dibikin pake TexturePacker nya (liat ebook page 167)
assetManager.load(Constants.TEXTURE_ATLAS_OBJECTS, TextureAtlas.class);
assetManager.load(Constants.TEXTURE_ATLAS_DECORATION, TextureAtlas.class);
assetManager.load(Constants.TEXTURE_ATLAS_GUI, TextureAtlas.class);

// inner class objects
tiang = new AssetTiang(atlasObject);
bg = new AssetBackgroud(atlasDecoration, jenisBg);
tombol = new AssetTombol(atlasGui);
tombolBg = new AssetTombolBg(atlasDecoration);
ring = new AssetRing(atlasObject, jenisRing);
}

@Override
public void error(AssetDescriptor asset, Throwable throwable) {
// TODO Auto-generated method stub

}

@Override
public void dispose(){
assetManager.dispose();
}

public class AssetRing{
public final AtlasRegion ring;

// jenis ring dimasukin disini, karena jenis ring bisa diganti-ganti sesuai yang dipilih
public AssetRing(TextureAtlas atlas, String jenisRing){
if(!jenisRing.equals("")){
ring = atlas.findRegion(jenisRing);
}
else{
ring = atlas.findRegion("ring-default");
}
}
}
}

最后,戒指(对象)

public class Ring extends AbstractGameObject{
private TextureRegion ringOverLay;
private float length;

// jenis ring nya
public String jenis;

public Ring(float length, String jenis){
init();

setLength(length);
setJenis(jenis);
}

// getters
public float getLength(){
return this.length;
}

public String getJenis(){
return this.jenis;
}

public Vector2 getPosition(){
return position;
}

// setters
public void setLength(float length){
this.length = length;
dimension.set(5.0f, 1.0f);
}

public void setJenis(String jenis){
this.jenis = jenis;
}

public void setPosition(float x, float y){
position.set(x, y);
}

private void init(){
ringOverLay = Assets.instance.ring.ring; // Assets.instance.namaobjek.atlasregion

origin.x = dimension.x/2; // -dimension.x/2;
origin.y = dimension.y/2;

position.x = -5.0f;
position.y = -2.5f;
}

@Override
public void render(SpriteBatch batch) {
TextureRegion reg = null;
reg = ringOverLay;

batch.draw(reg.getTexture(), position.x, position.y, origin.x, origin.y, dimension.x, dimension.y, scale.x, scale.y, rotation, reg.getRegionX(), reg.getRegionY(), reg.getRegionWidth(), reg.getRegionHeight(), false, false);
}

}

那么问题出在哪里呢?好的,问题是我无法使 Ring(游戏对象)变得可点击。Ring 正在扩展 AbstractGameObject(Actor),并且在 GameController 中我向 Ring 对象添加了一个 ClickListener,但该对象仍然不可点击。

请谁告诉我我的错误是什么?

最佳答案

您错误地使用了 Actor(在您的例子中为Ring):ActorsScene2d 的一部分,因此必须遵循精确的规则才能正确工作。

为了更具体地回答您的问题,需要将Ring添加到本身是一个InputProcessorStage中。 Stage 负责将输入事件(例如触摸事件)分发给其 Actor。如果不定义Stage,您的Actors将不会响应任何输入事件。

从上面的链接阅读Scene2d。这个video tutorial也很有帮助。

关于java - libgdx 制作触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34083918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com