Android使用libgdx实现模拟方向键控制角色移动的方法

所属分类: 软件编程 / Android 阅读数: 1618
收藏 0 赞 0 分享

本文实例讲述了Android使用libgdx实现模拟方向键控制角色移动的方法。分享给大家供大家参考,具体如下:

package com.demo;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
//Libgdx的Texture与Sprite使用
public class LibgdxActivity extends AndroidApplication {
  public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    // 初始化游戏屏幕,并设置是否支持GLES 2.0,如果您对向下兼容没什么需要选择true即可(2.1以上),否则选择false。
//   initialize(new FirstGame(), true);
    initialize(new Box2DDemo(), true);
  }
}

package com.demo;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
public class FirstActor extends Actor{
  private Texture texture;
  @Override
  public void draw(SpriteBatch batch, float arg1) {
    batch.draw(texture, this.x, this.y);
  }
  @Override
  public Actor hit(float arg0, float arg1) {
    if (x > 0 && y > 0 && this.height > y && this.width > x) {
      return this;
    } else {
      return null;
    }
  }
  @Override
  public boolean touchDown(float arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub
    return false;
  }
  @Override
  public void touchDragged(float arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub
  }
  @Override
  public void touchUp(float arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub
  }
  public FirstActor(String name) {
    super(name);
    texture = new Texture(Gdx.files.internal("bt.png"));
    this.height = texture.getHeight();
    this.width = texture.getWidth();
  }
}

package com.demo;
import android.util.Log;
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.graphics.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
class FirstGame implements ApplicationListener,ClickListener {
  private static String UP = "up";
  private static String DOWN = "down";
  private static String LEFT = "left";
  private static String RIGHT = "right";
  //舞台
  private Stage stage;
  //演员
  private Actor firstActor;
  private Texture texture;
  private Button buttonUp,buttonDown,buttonLeft,buttonRight;
  private NinePatch patch1, patch2;
  @Override
  public void create() {
    stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
    firstActor = new FirstActor("renwu");
    buttonUp = initButton(UP,40,80);
    buttonDown = initButton(DOWN,40,0);
    buttonLeft = initButton(LEFT,0,40);
    buttonRight = initButton(RIGHT,80,40);
    buttonUp.setClickListener(this);
    buttonDown.setClickListener(this);
    buttonLeft.setClickListener(this);
    buttonRight.setClickListener(this);
    stage.addActor(firstActor);
    stage.addActor(buttonUp);
    stage.addActor(buttonDown);
    stage.addActor(buttonLeft);
    stage.addActor(buttonRight);
    Gdx.input.setInputProcessor(stage);
  }
  @Override
  public void render() {
    // 清屏
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
  }
  @Override
  public void dispose() {
    // 释放占用的资源
    stage.dispose();
  }
  @Override
  public void resume() {
  }
  @Override
  public void pause() {
  }
  @Override
  public void resize(int width, int height) {
  }
  public Button initButton(String name,int x,int y){
    if(name.equals(UP)){
      texture = new Texture(Gdx.files.internal("up_alt.png"));
    }else if(name.equals(DOWN)){
      texture = new Texture(Gdx.files.internal("down_alt.png"));
    }else if(name.equals(LEFT)){
      texture = new Texture(Gdx.files.internal("back_alt.png"));
    }else if(name.equals(RIGHT)){
      texture = new Texture(Gdx.files.internal("forward_alt.png"));
    }
    patch1 = new NinePatch(texture, 0, 0, 0, 0);
    Button button = new Button(new ButtonStyle(patch1, patch1, patch1, 0f, 0f, 0f, 0f, null, null), name);
    button.x = x;
    button.y = y;
    button.width = 32;
    button.height = 32;
    return button;
  }
  @Override
  public void click(Actor button) {
    if(button.equals(buttonUp)){
      Actor actor = button.parent.findActor("renwu");;
      actor.y += 10;
      Log.i("touch", "up");
    }else if(button.equals(buttonDown)){
      Actor actor = button.parent.findActor("renwu");;
      actor.y -= 10;
      Log.i("touch", "down");
    }else if(button.equals(buttonLeft)){
      Actor actor = button.parent.findActor("renwu");;
      actor.x -= 10;
      Log.i("touch", "left");
    }else if(button.equals(buttonRight)){
      Actor actor = button.parent.findActor("renwu");;
      actor.x += 10;
      Log.i("touch", "right");
    }
  }
}

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多