Libgdx 矩形检测触摸
Libgdx Rectangle detect Touch
我想检测用户是否点击了骆驼并增加了钱,但它不起作用(如果我点击(移动的)骆驼则没有任何反应)。我没有在互联网上找到解决方案。
我在 render() 中添加了这个:
if(Gdx.input.justTouched()){
for (Lama lama : lamas) {
if(lama.lamarect.contains(Gdx.input.getX(), Gdx.input.getX())){
money+=100;
}else{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.app.debug("POSITION", "X touched: " + Gdx.input.getX() + " Y touched: " + Gdx.input.getY());
}
}
}
编辑:
@Override
public void create() {
spawnLama();
textureAtlas = new TextureAtlas(Gdx.files.internal("data/lamination.pack"));
animation = new Animation(1/15f, textureAtlas.getRegions());
camera = new OrthographicCamera(1280, 720);
...
private void spawnLama() {
Lama lama = new Lama();
lama.x = MathUtils.random(-1000, -200);
lama.y = MathUtils.random(-350, 100);
lama.speedx = MathUtils.random(-5, 5);
if(lama.speedx >= -1 && lama.speedx <=1){
lama.speedx = 2;
}
lama.speedy = MathUtils.random(-5, 5);
if(lama.speedy >= -1 && lama.speedy <=1){
lama.speedy = 2;
}
Rectangle livinglama = new Rectangle();
livinglama.x = lama.x;
livinglama.y = lama.y;
livinglama.width = 64;
livinglama.height = 64;
lama.lamarect = livinglama;
lamas.add(lama);
lastLamaTime = TimeUtils.nanoTime();
}
@Override
public void render() {
Gdx.gl.glClearColor(110 / 255F, 211 / 255F, 43 / 255F, 1 / 255F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
elapsedTime += Gdx.graphics.getDeltaTime();
if(spawnlama == true){
spawnLama();
spawnlama = false;
}
for (Lama lama : lamas) {
if(lama.x <= -1000){
lama.speedx = -lama.speedx;
}
else if(lama.x >= -200){
lama.speedx = -lama.speedx;
}
if(lama.y <= -350){
lama.speedy = -lama.speedy;
} else if(lama.y >=100){
lama.speedy = -lama.speedy;
}
if(lama.x == -500){
if(lama.speedx < 0){
lama.speedx --;
}
if(lama.speedx >= 0){
lama.speedx++;
}
}
lama.move();
}
if(Gdx.input.justTouched()){
for (Lama lama : lamas) {
Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touch);
if(lama.lamarect.contains(touch.x, touch.y)){
money+=100;
}else{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.app.debug("POSITION", "X touched: " + touch.x + " Y touched: " + touch.y);
}
}
}
batch.begin();
for (Lama lama : lamas) {
TextureRegion currentFrame;
currentFrame = animation.getKeyFrame(elapsedTime, true);
if(!currentFrame.isFlipX()) {
if (lama.speedx >= 0) {
currentFrame.flip(true, false);
}
}else{
if (lama.speedx < 0) {
currentFrame.flip(true, false);
}
}
batch.draw(currentFrame, lama.x, lama.y, currentFrame
.getRegionWidth(), currentFrame.getRegionHeight());
}
elapsedTime += Gdx.graphics.getDeltaTime();
喇嘛class:
public class Lama {
public int x, y, speedx, speedy;
public Rectangle lamarect;
// float delay = 1; // seconds
void move() {
x += speedx;
y += speedy;
lamarect.x = x;
lamarect.y = y;
}
}
Gdx.input.getX
和 Gdx.input.getY
给出屏幕坐标,这些需要转换回世界坐标。如果您正在使用相机,您可以使用 camera.unproject(touchVector)
如果您正在使用没有指定相机的舞台,您可以通过 stage.getCamera()
获取相机。
如果两者都没有,那么您必须自己将其转换回来,我相信您必须先翻转 y 坐标,然后将它们添加到您在游戏世界中看到的左下角点。但是你可以通过实现相机让你的生活更轻松。
if(Gdx.input.justTouched()){
Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touch);
for (Lama lama : lamas) {
if(lama.lamarect.contains(touch.x, touch.y)){
money+=100;
}else{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.app.debug("POSITION", "X touched: " + touch.x + " Y touched: " + touch.y);
}
}
}
除此之外,您在 contains
方法中传递了 Gdx.input.getX()
两次。
我想检测用户是否点击了骆驼并增加了钱,但它不起作用(如果我点击(移动的)骆驼则没有任何反应)。我没有在互联网上找到解决方案。
我在 render() 中添加了这个:
if(Gdx.input.justTouched()){
for (Lama lama : lamas) {
if(lama.lamarect.contains(Gdx.input.getX(), Gdx.input.getX())){
money+=100;
}else{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.app.debug("POSITION", "X touched: " + Gdx.input.getX() + " Y touched: " + Gdx.input.getY());
}
}
}
编辑:
@Override
public void create() {
spawnLama();
textureAtlas = new TextureAtlas(Gdx.files.internal("data/lamination.pack"));
animation = new Animation(1/15f, textureAtlas.getRegions());
camera = new OrthographicCamera(1280, 720);
...
private void spawnLama() {
Lama lama = new Lama();
lama.x = MathUtils.random(-1000, -200);
lama.y = MathUtils.random(-350, 100);
lama.speedx = MathUtils.random(-5, 5);
if(lama.speedx >= -1 && lama.speedx <=1){
lama.speedx = 2;
}
lama.speedy = MathUtils.random(-5, 5);
if(lama.speedy >= -1 && lama.speedy <=1){
lama.speedy = 2;
}
Rectangle livinglama = new Rectangle();
livinglama.x = lama.x;
livinglama.y = lama.y;
livinglama.width = 64;
livinglama.height = 64;
lama.lamarect = livinglama;
lamas.add(lama);
lastLamaTime = TimeUtils.nanoTime();
}
@Override
public void render() {
Gdx.gl.glClearColor(110 / 255F, 211 / 255F, 43 / 255F, 1 / 255F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
elapsedTime += Gdx.graphics.getDeltaTime();
if(spawnlama == true){
spawnLama();
spawnlama = false;
}
for (Lama lama : lamas) {
if(lama.x <= -1000){
lama.speedx = -lama.speedx;
}
else if(lama.x >= -200){
lama.speedx = -lama.speedx;
}
if(lama.y <= -350){
lama.speedy = -lama.speedy;
} else if(lama.y >=100){
lama.speedy = -lama.speedy;
}
if(lama.x == -500){
if(lama.speedx < 0){
lama.speedx --;
}
if(lama.speedx >= 0){
lama.speedx++;
}
}
lama.move();
}
if(Gdx.input.justTouched()){
for (Lama lama : lamas) {
Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touch);
if(lama.lamarect.contains(touch.x, touch.y)){
money+=100;
}else{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.app.debug("POSITION", "X touched: " + touch.x + " Y touched: " + touch.y);
}
}
}
batch.begin();
for (Lama lama : lamas) {
TextureRegion currentFrame;
currentFrame = animation.getKeyFrame(elapsedTime, true);
if(!currentFrame.isFlipX()) {
if (lama.speedx >= 0) {
currentFrame.flip(true, false);
}
}else{
if (lama.speedx < 0) {
currentFrame.flip(true, false);
}
}
batch.draw(currentFrame, lama.x, lama.y, currentFrame
.getRegionWidth(), currentFrame.getRegionHeight());
}
elapsedTime += Gdx.graphics.getDeltaTime();
喇嘛class:
public class Lama {
public int x, y, speedx, speedy;
public Rectangle lamarect;
// float delay = 1; // seconds
void move() {
x += speedx;
y += speedy;
lamarect.x = x;
lamarect.y = y;
}
}
Gdx.input.getX
和 Gdx.input.getY
给出屏幕坐标,这些需要转换回世界坐标。如果您正在使用相机,您可以使用 camera.unproject(touchVector)
如果您正在使用没有指定相机的舞台,您可以通过 stage.getCamera()
获取相机。
如果两者都没有,那么您必须自己将其转换回来,我相信您必须先翻转 y 坐标,然后将它们添加到您在游戏世界中看到的左下角点。但是你可以通过实现相机让你的生活更轻松。
if(Gdx.input.justTouched()){
Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touch);
for (Lama lama : lamas) {
if(lama.lamarect.contains(touch.x, touch.y)){
money+=100;
}else{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.app.debug("POSITION", "X touched: " + touch.x + " Y touched: " + touch.y);
}
}
}
除此之外,您在 contains
方法中传递了 Gdx.input.getX()
两次。