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

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文档翻译

libgdx学习ActorGestureListener点击滑动等动作监听
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;
	}
}

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

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;
	}
}

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

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;
	}
}

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

长按

libgdx学习ActorGestureListener点击滑动等动作监听
如果返回是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);
	}	
}

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

上一篇:android-如何在Libgdx中更改AdMob方向


下一篇:android-Play商店和设备上多个DPI的资产处理