Android uses LIBGDX to implement the method of controlling role movement of analog key _android

Source: Internet
Author: User
Tags clear screen stub touch libgdx

This article is an example of how Android uses LIBGDX to implement a simulation key to control role movement. Share to everyone for your reference, specific as follows:

Package Com.demo;
Import Android.os.Bundle;
Import com.badlogic.gdx.backends.android.AndroidApplication;
LIBGDX's texture and sprite use public
class Libgdxactivity extends Androidapplication {public
  void OnCreate ( Bundle Bundle) {
    super.oncreate (Bundle);
    Initialize the game screen and set whether to support Gles 2.0, if you have no need to choose true for backward compatibility (more than 2.1), otherwise choose 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 Fi
   Rstactor (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 ';
  Stage private Stage Stage;
  Actor 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 () {//Clear screen Gdx.gl.glClear (gl10.gl_color_buffer_bit);
    Stage.act (Gdx.graphics.getDeltaTime ());
  Stage.draw ();
  @Override public void Dispose () {//release of occupied Resource Stage.dispose (); @Override public void Resume () {} @Override public void Pause () {} @Override public void resize (int wi DTH, 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.findA
      ctor ("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");

 }
  }
}

I hope this article will help you with your Android programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.