欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-17 15:14:40
本文实例讲述了android使用libgdx实现模拟方向键控制角色移动的方法。分享给大家供大家参考,具体如下: package com.demo; import...

本文实例讲述了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程序设计有所帮助。

上一篇:

下一篇: