gpt4 book ai didi

android - libgdx - 触摸和拖动不起作用(滞后并显示在 2 个地方)

转载 作者:行者123 更新时间:2023-11-30 01:51:33 25 4
gpt4 key购买 nike

所以我编辑了这篇文章并更新了当前代码。当我在 LetterActor 中将 unproject 设置为 false 时,我可以拖动字母但不正确,它们滞后于我的手指并且不会在我释放触摸的地方结束。当我触摸它时,字母/ Actor 也会被绘制在两个地方。它在两个位置之间闪烁。

当我将 unproject 设置为 true 时,屏幕上根本没有显示任何内容。

我错过了什么?

如果我理解正确,unproject 会将您从屏幕坐标带到游戏坐标,其中游戏坐标是游戏的内部表示。但是你必须定义游戏坐标和屏幕坐标如何相互关联,或者 unproject 如何知道要做什么?这是怎么做到的?当你再次绘制到屏幕上时,你不应该再次投影吗?

我认为它是:1.Unproject获取事件内部游戏坐标2.用新事件更新内部游戏表示3.投影再次获取屏幕坐标

package com.xxxx.yyyy;

import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;

public class LetterActor extends Actor
{
private Texture texture;
private Vector3 touchPosition = new Vector3();
private Camera camera;
private boolean unproject = true;

public LetterActor(Texture letterTexture, Camera theCamera)
{
texture = letterTexture;
camera = theCamera;

touchPosition.set(0, 0, 0);
camera.unproject(touchPosition);

setSize(texture.getWidth(), texture.getHeight());

addListener(new InputListener()
{
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
touchPosition.set(x, y, 0);
if (unproject)
{
camera.unproject(touchPosition);
}
setPosition(touchPosition.x, touchPosition.y);

return true;
}

@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button)
{
touchPosition.set(x, y, 0);
if (unproject)
{
camera.unproject(touchPosition);
}
setPosition(touchPosition.x, touchPosition.y);
}

@Override
public void touchDragged(InputEvent event, float x, float y, int pointer)
{
touchPosition.set(x, y, 0);
if (unproject)
{
camera.unproject(touchPosition);
}
setPosition(touchPosition.x, touchPosition.y);
}
});
}

@Override
public void draw(Batch batch, float alpha){
batch.draw(texture, getX(), getY(), getWidth(), getHeight());
}

@Override
public void act(float delta){
//setBounds(getX(), getY(),getWidth(), getHeight());
}
}



package com.xxxx.yyyy;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;

public class LetterLoader {

public static Texture[] loadLetters()
{
Texture[] letters = new Texture[26];

for (int i = 0; i < 26; i++)
{
char letter = (char) (i + 65);
letters[i] = new Texture(Gdx.files.internal("bigletters/" + letter + ".png"));
}

return letters;
}
}





package com.xxxx.yyyy;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.Touchable;

public class WordPuzzle extends ApplicationAdapter
{
private final static float SCALE = 4f;
private final static float INV_SCALE = 1.f / SCALE;

private final static float VP_WIDTH = 1280 * INV_SCALE;
private final static float VP_HEIGHT = 720 * INV_SCALE;

private OrthographicCamera camera;
private ExtendViewport viewport;
private Stage stage;

private LetterActor letterActor;

@Override
public void create()
{
camera = new OrthographicCamera();
viewport = new ExtendViewport(VP_WIDTH, VP_HEIGHT, camera);
stage = new Stage();
stage.setViewport(viewport);
Gdx.input.setInputProcessor(stage);
Texture[] textures = LetterLoader.loadLetters();
for (int i = 0; i < textures.length; i++)
{
letterActor = new LetterActor(textures[i], camera);
letterActor.setTouchable(Touchable.enabled);
stage.addActor(letterActor);
}
}

@Override
public void render()
{
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}


@Override public void resize(int width, int height)
{
viewport.update(width, height, true);
}

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

最佳答案

在您最初的问题中,绘制位置的翻转可能是您反复调用 setBounds 造成的。我不确定。

回答您的新问题:如果您没有使用 Scene2D,则需要通过取消投影将屏幕坐标转换为世界坐标。这是由相机完成的。相机保存定义世界坐标系如何投影到屏幕坐标系的数据,因此它可以进行计算以来回投影和取消投影。但是你错了,你需要在对新坐标使用react后重新投影回屏幕坐标。这是在将场景投影到屏幕时在着色器中处理的,除非使用自定义着色器,否则您无需担心。您只需要使用世界坐标,并且只需要在获得屏幕坐标时取消投影。

但是,我之前错过的是您正在使用 Stage 及其关联的 InputListener。 Stage 在将屏幕坐标提供给 InputListener 之前取消投影屏幕坐标,因此您根本不需要担心取消投影或投影!


我原回答的相关信息:

从类中删除你的 actorXactorY 因为它们是多余的。使用 getX(), getY(), setX(), setY(), setPosition( ) 等。

您也不需要一遍又一遍地调用 setBounds。只需在实例化类时调用一次 setSize() 即可。每当您调用 setPosition() 时,边界就会自动更新。 (我注意到您将边界设置为纹理大小的两倍。也许您是在补偿未正确投影的情况?)

对于您的draw 方法,您应该使用getX()getY(),我假设您也想绘制它与其边界大小相同,因此也使用 getWidth()getHeight():

@Override
public void draw(Batch batch, float alpha){
batch.draw(texture, getX(), getY(), getWidth(), getHeight());
}

关于android - libgdx - 触摸和拖动不起作用(滞后并显示在 2 个地方),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32959032/

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