介绍
最近学习用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;
}
}
}
由于手机录屏只能记录到一个触碰点,所以会出现上面的效果。
当图片向上走时,手势为放大(两触点距离变大)。