libgdx学习ActorGestureListener点击滑动等动作监听

介绍

最近学习用aide手机写游戏,在此记录一下libgdx学习过程。
ActorGestureListener用于游戏中的手势识别一般。
演示中的所有函数主函数都为MyGdxGame函数。

libgdx文档翻译

Detects tap, long press, fling, pan, zoom, and pinch gestures on an actor. If there is only a need to detect tap, use ClickListener.

ActorGestureListener可以检测点击,长按,滑动,平移,缩放,和收缩等的演员手势。如果只需要检测点击,使用ClickListener即可。

点击

libgdx文档翻译

在这里插入图片描述
touchDown检测监听按下动作,touchUp抬起,tap则是监听点击动作,不分按下抬起。
x和y分别是发生位置的横坐标和纵坐标。

演示代码

主函数:
public static float x=0,y=0; 把x和y定义为全局变量,在哪个类都可以改变。

package com.mycompany.mygame;

import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.scenes.scene2d.*;

public class MyGdxGame implements ApplicationListener
{
	Texture texture;
	SpriteBatch batch;
	Stage stage;
	public static float x=0,y=0;

	@Override
	public void create()
	{
		texture = new Texture(Gdx.files.internal("android.jpg"));
		batch = new SpriteBatch();
		stage=new Stage();
		stage.addListener(new MyGestureListener());
		Gdx.input.setInputProcessor(stage);
	}

	@Override
	public void render()
	{        
	    Gdx.gl.glClearColor(1, 1, 1, 1);
	    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		batch.begin();
		batch.draw(texture, x, y, 
				   Gdx.graphics.getWidth() / 2, Gdx.graphics.getWidth() / 2);
		batch.end();
		stage.act();
		stage.draw();
	}

	@Override
	public void dispose()
	{
	}

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

	@Override
	public void pause()
	{
	}

	@Override
	public void resume()
	{
	}
}

touchDown

MyGestureListener函数重写ActorGestureListener类

package com.mycompany.mygame;
import com.badlogic.gdx.scenes.scene2d.utils.*;
import com.badlogic.gdx.scenes.scene2d.*;

public class MyGestureListener extends ActorGestureListener
{
	@Override
	public void touchDown(InputEvent event, float x, float y, int pointer, int button)
	{
		// TODO: Implement this method
		super.touchDown(event, x, y, pointer, button);
		MyGdxGame.x=x;
		MyGdxGame.y=y;
	}
}

效果:
在这里插入图片描述

touchUp

package com.mycompany.mygame;
import com.badlogic.gdx.scenes.scene2d.utils.*;
import com.badlogic.gdx.scenes.scene2d.*;

public class MyGestureListener extends ActorGestureListener
{
	@Override
	public void touchUp(InputEvent event, float x, float y, int pointer, int button)
	{
		// TODO: Implement this method
		super.touchUp(event, x, y, pointer, button);
		MyGdxGame.x=x;
		MyGdxGame.y=y;
	}
}

在这里插入图片描述

tap

package com.mycompany.mygame;
import com.badlogic.gdx.scenes.scene2d.utils.*;
import com.badlogic.gdx.scenes.scene2d.*;

public class MyGestureListener extends ActorGestureListener
{
	@Override
	public void tap(InputEvent event, float x, float y, int count, int button)
	{
		// TODO: Implement this method
		super.tap(event, x, y, count, button);
		MyGdxGame.x=x;
		MyGdxGame.y=y;
	}
}

在这里插入图片描述

长按

在这里插入图片描述
如果返回是true,其他的手势不会被触发,因为该事件是随着时间流逝触发的。
演示代码:

package com.mycompany.mygame;
import com.badlogic.gdx.scenes.scene2d.utils.*;
import com.badlogic.gdx.scenes.scene2d.*;

public class MyGestureListener extends ActorGestureListener
{
	@Override
	public boolean longPress(Actor actor, float x, float y)
	{
		// TODO: Implement this method
		MyGdxGame.x=x;
		MyGdxGame.y=y;
		return super.longPress(actor, x, y);
	}	
}

在这里插入图片描述

滑动

在这里插入图片描述
velocityX为x方向的速度,velocityY为Y方向的速度。

package com.mycompany.mygame;
import com.badlogic.gdx.scenes.scene2d.utils.*;
import com.badlogic.gdx.scenes.scene2d.*;

public class MyGestureListener extends ActorGestureListener
{
	@Override
	public void fling(InputEvent event, float velocityX, float velocityY, int button)
	{
		// TODO: Implement this method
		super.fling(event, velocityX, velocityY, button);
		if(velocityX>300){
			MyGdxGame.x=MyGdxGame.x+100;
		}else if(velocityX<-300){
			MyGdxGame.x=MyGdxGame.x-100;
		}
		if(velocityY>300){
			MyGdxGame.y=MyGdxGame.y+100;
		}else if(velocityY<-300){
			MyGdxGame.y=MyGdxGame.y-100;
		}
	}
}

在这里插入图片描述

移动

在这里插入图片描述
移动时响应,只按下不移动不会触发。x和y为移动的位置,deltaX和deltaY为角度。

package com.mycompany.mygame;
import com.badlogic.gdx.scenes.scene2d.utils.*;
import com.badlogic.gdx.scenes.scene2d.*;

public class MyGestureListener extends ActorGestureListener
{
	@Override
	public void pan(InputEvent event, float x, float y, float deltaX, float deltaY)
	{
		// TODO: Implement this method
		super.pan(event, x, y, deltaX, deltaY);
		MyGdxGame.x=x;
		MyGdxGame.y=y;
	}
}

在这里插入图片描述

缩放

在这里插入图片描述
它会测量两个接触手指中间的距离,distance为目前距离,initialDistance为初始距离。

package com.mycompany.mygame;
import com.badlogic.gdx.scenes.scene2d.utils.*;
import com.badlogic.gdx.scenes.scene2d.*;

public class MyGestureListener extends ActorGestureListener
{
	@Override
	public void zoom(InputEvent event, float initialDistance, float distance)
	{
		// TODO: Implement this method
		super.zoom(event, initialDistance, distance);
		if(distance>initialDistance){
			MyGdxGame.x+=10;
			MyGdxGame.y+=10;
			}
		else{
			MyGdxGame.x-=10;
			MyGdxGame.y-=10;
			}
	}
}

在这里插入图片描述
由于手机录屏只能记录到一个触碰点,所以会出现上面的效果。
当图片向上走时,手势为放大(两触点距离变大)。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值