Android libgdx-触摸和拖动不起作用(落后,显示在2处)
所以我编辑了这篇文章,并用当前代码进行了更新。 当我在LetterActor中将unproject设置为false时,我可以拖动字母,但不能正确拖动,它们会落在我的手指后面,不会在我松开触摸的地方结束。 此外,当我触摸字母/演员时,它会在两个地方画出来。它在两个位置之间闪烁 当我将unproject设置为true时,屏幕上根本不显示任何内容 我错过了什么 如果我理解正确,取消投射会将你从屏幕坐标带到游戏坐标,其中游戏坐标是你对游戏的内部表示。但是你必须定义游戏坐标和屏幕坐标之间的关系,或者unproject如何知道该怎么做?这是怎么做到的? 当你再次画到屏幕上时,你不应该再次投影吗 我认为: 1.取消项目以获取事件的内部游戏坐标 2.使用新事件更新内部游戏表示 3.项目以再次获取屏幕坐标Android libgdx-触摸和拖动不起作用(落后,显示在2处),android,libgdx,touch,Android,Libgdx,Touch,所以我编辑了这篇文章,并用当前代码进行了更新。 当我在LetterActor中将unproject设置为false时,我可以拖动字母,但不能正确拖动,它们会落在我的手指后面,不会在我松开触摸的地方结束。 此外,当我触摸字母/演员时,它会在两个地方画出来。它在两个位置之间闪烁 当我将unproject设置为true时,屏幕上根本不显示任何内容 我错过了什么 如果我理解正确,取消投射会将你从屏幕坐标带到游戏坐标,其中游戏坐标是你对游戏的内部表示。但是你必须定义游戏坐标和屏幕坐标之间的关系,或者unp
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();
}
}
包com.xxxx.yyyy;
导入com.badlogic.gdx.graphics.Camera;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.g2d.Batch;
导入com.badlogic.gdx.math.Vector3;
导入com.badlogic.gdx.scenes.scene2d.Actor;
导入com.badlogic.gdx.scenes.scene2d.InputEvent;
导入com.badlogic.gdx.scenes.scene2d.InputListener;
公共类LetterActor扩展了Actor
{
私有纹理;
私有向量3 touchPosition=新向量3();
私人摄像机;
私有布尔unproject=true;
公共信笺演员(纹理、纹理、照相机)
{
纹理=字母纹理;
摄像机=摄像机;
触摸位置设置(0,0,0);
相机。取消投影(触摸位置);
设置大小(texture.getWidth(),texture.getHeight());
addListener(新的InputListener()
{
@凌驾
公共布尔接地(InputEvent事件、浮点x、浮点y、整数指针、整数按钮)
{
触摸位置设置(x,y,0);
如果(未投影)
{
相机。取消投影(触摸位置);
}
设置位置(touchPosition.x,touchPosition.y);
返回true;
}
@凌驾
公共无效修补(InputEvent事件、浮点x、浮点y、整数指针、整数按钮)
{
触摸位置设置(x,y,0);
如果(未投影)
{
相机。取消投影(触摸位置);
}
设置位置(touchPosition.x,touchPosition.y);
}
@凌驾
public void touchdrable(InputEvent事件、浮点x、浮点y、int指针)
{
触摸位置设置(x,y,0);
如果(未投影)
{
相机。取消投影(触摸位置);
}
设置位置(touchPosition.x,touchPosition.y);
}
});
}
@凌驾
公共作废提取(批量、浮动alpha){
绘制(纹理,getX(),getY(),getWidth(),getHeight());
}
@凌驾
公共无效法案(浮动三角洲){
//setBounds(getX(),getY(),getWidth(),getHeight());
}
}
包com.xxxx.yyyy;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.graphics.Texture;
公共类加载器{
公共静态纹理[]loadLetters()
{
纹理[]字母=新纹理[26];
对于(int i=0;i<26;i++)
{
字符字母=(字符)(i+65);
字母[i]=新纹理(Gdx.files.internal(“bigletters/”+letter+.png”);
}
回信;
}
}
包com.xxxx.yyyy;
导入com.badlogic.gdx.ApplicationAdapter;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.graphics.GL20;
导入com.badlogic.gdx.graphics.Orthographic Camera;
导入com.badlogic.gdx.utils.viewport.ExtendViewport;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.scenes.scene2d.Stage;
导入com.badlogic.gdx.scenes.scene2d.Touchable;
公共类WordPuzzle扩展了ApplicationAdapter
{
专用最终静态浮动刻度=4f;
专用最终静态浮动投资比例=1.f/比例;
专用最终静态浮动VP_宽度=1280*INV_刻度;
专用最终静态浮动VP_高度=720*INV_刻度;
私人正交摄影机;
私有ExtendViewport视口;
私人舞台;
私人投稿人;
@凌驾
公共void create()
{
摄影机=新的正交摄影机();
视口=新的ExtendViewport(视口宽度、视口高度、摄影机);
阶段=新阶段();
stage.setViewport(viewport);
Gdx.input.setInputProcessor(阶段);
纹理[]纹理=LetterLoader.loadLetters();
对于(int i=0;i
在您最初的问题中,绘制位置的翻转可能是由于您反复调用设置边界。我不确定
回答您的新问题:如果您没有使用Scene2D,则需要通过取消投影将屏幕坐标转换为世界坐标。这是由摄像机完成的。摄像机保存定义世界坐标系如何投影到屏幕坐标系的数据
@Override
public void draw(Batch batch, float alpha){
batch.draw(texture, getX(), getY(), getWidth(), getHeight());
}