(libgdx小结)演员类的使用

本文介绍了一个使用LibGDX框架开发的简单游戏示例,包括游戏角色Mario的移动控制及动画展示。通过两个按钮控制Mario左右移动,并根据不同状态显示不同动画。
















二、应用举例

1、Mario

package com.example.groupactiontest;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

/**
 * mario具有以下属性:坐标、状态、动作
 * mario具有以下行为:人物移动、状态改变
 * @author pc
 *
 */
public class Mario extends Actor {

	//mario的坐标
	public static float x;
	public static float y;
	
	public float stateTime = 0;
	
	//mario的状态
	enum STATE{
		LEFT,RIGHT,IDLE
	}STATE state;
	
	//mario的动作
	Animation aniLeft;
	Animation aniRight;
	Animation aniIdle;
	
	//用于动画
	Texture texture;
	TextureRegion currentFrame;
	
	//按钮
	ImageButton buttonL;
	ImageButton buttonR;
	
	
	
	public Mario(float x, float y) {
		this.x = x;
		this.y = y;
		
		state = STATE.IDLE;//mario的默认状态为idle(空闲)
		
		this.show();
	}
	
	
	private void show() {
		texture = new Texture(Gdx.files.internal("Mario.png"));
		TextureRegion[][] split = TextureRegion.split(texture, 64, 64);
	    TextureRegion[][] mirror = TextureRegion.split(texture, 64, 64);
		
	    /**
	     * 获取对称的画面
	     */
		for(TextureRegion[] region1 : mirror){
			for(TextureRegion region2 : region1){
				region2.flip(true, false);
			}
		}
		
		/**
		 * 左中右动画的实现
		 */
		TextureRegion[] rightAniFrame = new TextureRegion[3];
		rightAniFrame[0] = split[0][1];
		rightAniFrame[1] = split[0][2];
		rightAniFrame[2] = split[0][0];
		aniRight = new Animation(0.1f,rightAniFrame);
		
		TextureRegion[] leftAniFrame = new TextureRegion[3];
		leftAniFrame[0] = mirror[0][1];
		leftAniFrame[1] = mirror[0][2];
		leftAniFrame[2] = mirror[0][0];
		aniLeft = new Animation(0.1f, leftAniFrame);
		
		TextureRegion[] idleAniFrame = new TextureRegion[1];
		idleAniFrame[0] = split[0][0];
		aniIdle = new Animation(0.1f, idleAniFrame);
		
		
		
		
		
		/**
		 * ImageButon的初始化
		 */
		buttonL = new ImageButton(new TextureRegionDrawable(split[1][0]),new TextureRegionDrawable(split[1][1]));
		buttonR = new ImageButton(new TextureRegionDrawable(mirror[1][0]),new TextureRegionDrawable(mirror[1][1]));
		buttonL.setPosition(20, 20);
		buttonR.setPosition(100, 20);
		
		
		
		
		
		
		/**
		 * ImageButton的监听事件
		 */
		buttonL.addListener(new InputListener(){
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				
				state = STATE.LEFT;
				return true;
			}
			
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				
				state = STATE.IDLE;
				
				super.touchUp(event, x, y, pointer, button);
			}
		});
		
		buttonR.addListener(new InputListener(){
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				
				state = STATE.RIGHT;
				return true;
			}
			
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				
				state = STATE.IDLE;
				
				super.touchUp(event, x, y, pointer, button);
			}
		});
		
	}

    /**
     * 人物移动
     */
	public void update(){
		if(state == STATE.LEFT){
			x -= 1.5f;
			
			if(x < 20){//空气墙。即mario最左不能超过这个坐标
				x = 20;
			}
		}else if(state == STATE.RIGHT){
			x += 1.5f;
			
			if(x > 400){//空气墙。即mario最右不能超过这个坐标
				x = 400;
			}
		}
	}
	
	/**
	 * 状态判断,更换画面
	 */
	public void aniCheck(){
		if(state == STATE.LEFT){
			currentFrame = aniLeft.getKeyFrame(stateTime, true);
		}else if(state == STATE.RIGHT){
			currentFrame = aniRight.getKeyFrame(stateTime, true);
		}else if(state == STATE.IDLE){
			currentFrame = aniIdle.getKeyFrame(stateTime,true);
		}
	}
	
	
	@Override
	public void act(float arg0) {//在一定的时间段内,更新演员的状态
		// TODO Auto-generated method stub
		super.act(arg0);
	}

	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {//演员本身可以在舞台中绘画
		
		stateTime += Gdx.graphics.getDeltaTime();//修改statetime
		update();//移动人物
		aniCheck();//更改人物状态
		
		
		batch.draw(currentFrame, x, y);//把当前帧滑到界面上
		
		super.draw(batch, parentAlpha);
	}

	
}


2、MyGame

package com.example.groupactiontest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

/**
 * 游戏画面展现出来的东西就只有
 * 背景---->image
 * mario---->Mario
 * 按钮--->mario的成员变量
 * 
 * 
 * 把它们都添加到舞台上
 * @author pc
 *
 */
public class MyGame implements ApplicationListener {

	Stage stage;
	Image image;
	Mario mario;
	
	@Override
	public void create() {
		stage = new Stage(480,320,false);
		
		image = new Image(new Texture(Gdx.files.internal("image.jpg")));
		image.setPosition(0, 170);
		
		mario = new Mario(0,190);
		
		stage.addActor(image);
		stage.addActor(mario);
		stage.addActor(mario.buttonL);
		stage.addActor(mario.buttonR);
		
		Gdx.input.setInputProcessor(stage);
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		stage.act();
		stage.draw();
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

}


三、运行效果



四、源码下载

http://download.youkuaiyun.com/detail/caihongshijie6/7007011

libGDX 框架中实现游戏摇杆功能,可以通过自定义控件或使用现有的 UI 组件来完成。libGDX 提供了灵活的 `Actor` 和 `Stage` 系统,可以用于构建交互式控件,例如虚拟摇杆。 以下是一个基本的实现思路和示例代码: ### 1. 创建摇杆的 Actor 可以创建一个继承自 `Actor` 的,用于表示摇杆。该将处理触摸事件,并根据触摸位置更新摇杆的偏移量。 ```java import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.utils.DragListener; public class JoystickActor extends Actor { private Vector2 center; private float radius; private Vector2 direction = new Vector2(); private boolean isDragging = false; public JoystickActor(float x, float y, float radius) { this.center = new Vector2(x, y); this.radius = radius; setBounds(x - radius, y - radius, 2 * radius, 2 * radius); addListener(new DragListener() { @Override public void dragStart(InputEvent event, float x, float y, int pointer) { isDragging = true; updateDirection(x, y); } @Override public void drag(InputEvent event, float x, float y, int pointer) { updateDirection(x, y); } @Override public void dragStop(InputEvent event, float x, float y, int pointer) { isDragging = false; direction.setZero(); } }); } private void updateDirection(float x, float y) { Vector2 touch = new Vector2(x, y); Vector2 offset = touch.sub(center); if (offset.len() > radius) { offset.nor().scl(radius); } direction.set(offset); } public Vector2 getDirection() { return direction; } public boolean isDragging() { return isDragging; } } ``` ### 2. 在 Stage 中添加摇杆 在游戏的主舞台中添加这个摇杆,并在每一帧中读取其方向信息,用于控制角色或摄像机。 ```java public class MyGameStage extends Stage { private JoystickActor joystick; public MyGameStage(Viewport viewport) { super(viewport); // 初始化摇杆,设置中心位置和半径 joystick = new JoystickActor(150, 150, 100); addActor(joystick); } public void act(float delta) { super.act(delta); // 获取摇杆方向 Vector2 direction = joystick.getDirection(); if (joystick.isDragging()) { // 根据方向进行角色移动或其他操作 // 例如:player.move(direction); } } } ``` ### 3. 渲染摇杆 可以通过 `Sprite` 或者 `ShapeRenderer` 来绘制摇杆的外观,例如一个圆形底座和一个可移动的摇杆头。 ```java public class JoystickActor extends Actor { private Sprite baseSprite; private Sprite knobSprite; public JoystickActor(float x, float y, float radius, TextureAtlas atlas) { this(x, y, radius); baseSprite = new Sprite(atlas.findRegion("joystick_base")); knobSprite = new Sprite(atlas.findRegion("joystick_knob")); baseSprite.setPosition(x - radius, y - radius); knobSprite.setPosition(x - radius, y - radius); } @Override public void draw(Batch batch, float parentAlpha) { baseSprite.draw(batch); knobSprite.setPosition( center.x + direction.x - radius, center.y + direction.y - radius ); knobSprite.draw(batch); } } ``` ### 4. 使用纹理资源 需要为摇杆准备两张纹理图片,一张是底座(`joystick_base`),另一张是摇杆头(`joystick_knob`)。这些资源可以通过 `TextureAtlas` 加载。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值