Android Game Development Framework LIBGDX use (13) role and character movement in-tiledmap

Source: Internet
Author: User
Tags libgdx

Http://www.cnblogs.com/htynkn/archive/2012/01/13/libgdx_13.html

This article follows, address: Android game development Framework LIBGDX use (12)-tiledmap map use

We've created the map, and then there's the protagonist. In fact, the above describes how tiledmap and stage of the combination, the role of processing is simple.

Can inherit the actor class to create the protagonist class, I will steal a lazy, with image instead.

Edit our TMX file to add an object layer.

Add a shape to where the protagonist is going to appear

Name is Play1

Our main characters are:

The idea is that we iterate through all the objects in the map, and if the name matches the play1 we set, we instantiate an image, and the position and object are consistent and added to the stage.

Key code:











}



The effect is as follows:

And now try to get the protagonist to move.

First of all is how we control, the Android device Priority selection touch. If we hold on to the front, the protagonist moves forward. Hold the top down, and the protagonist is up.

So how do we determine which direction we are holding?

, the yellow is the stage, the pink border is the entire map, part of the display, some are not shown. The green dot in the lower right corner is where the protagonist is, and we assume that the red dot is our touch point.

Identify the red touch point as forward, I am providing a solution, but the method is not unique ha, I am sure this direction is not necessarily the most in line with the user experience.

A coordinate system is reproduced with the position of the protagonist as the origin, and the new coordinates of the touch point x, Y.

The quadrant of the contact point in the new coordinate system is determined, and the direction is known in determining the size of x, Y.

The code is as follows:

 Vector3 tmp = new Vector3 (x, y, 0); 
Stage.getcamera (). Unproject (TMP);
Float newx = tmp.x-player.x;
Float newy = tmp.y-player.y;
if (newx > 0 && newy > 0) {
if (Newx > Newy) {
Changedirect (4);
} else {
Changedirect (1);
}
} else if (newx > 0 && newy < 0) {
if (Newx >-newy) {
Cha Ngedirect (4);
} else {
Changedirect (2);
}
} else if (newx < 0 && newy > 0) {
if (-newx > Newy) {
Cha Ngedirect (3);
} else {
Changedirect (1);
}
} else {
if (-newx >-newy) {
Changedirect (3);
} else {
Changedirect (2);
}
}

Move the camera position directly to move the map, but our protagonist disappears from the map ... The way to do this is to move the coordinates of the actor you want to still show on the map along with the camera.

The code is as follows:

private void Cameramove (Vector3 Vector3) {
Stage.getcamera (). Position.add (Vector3);
For (Actor actor:stage.getActors ()) {
Actor.x + = vector3.x;
Actor.y + = Vector3.y;
}
}

Full code:

Package com.cnblogs.htynkn.game;

Import Com.badlogic.gdx.ApplicationListener;
Import COM.BADLOGIC.GDX.GDX;
Import Com.badlogic.gdx.InputMultiplexer;
Import Com.badlogic.gdx.InputProcessor;
Import Com.badlogic.gdx.files.FileHandle;
Import Com.badlogic.gdx.graphics.Color;
Import com.badlogic.gdx.graphics.GL10;
Import Com.badlogic.gdx.graphics.OrthographicCamera;
Import Com.badlogic.gdx.graphics.Texture;
Import Com.badlogic.gdx.graphics.g2d.BitmapFont;
Import com.badlogic.gdx.graphics.g2d.TextureRegion;
Import Com.badlogic.gdx.graphics.g2d.tiled.TileAtlas;
Import Com.badlogic.gdx.graphics.g2d.tiled.TileMapRenderer;
Import Com.badlogic.gdx.graphics.g2d.tiled.TiledLoader;
Import Com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
Import Com.badlogic.gdx.graphics.g2d.tiled.TiledObject;
Import Com.badlogic.gdx.graphics.g2d.tiled.TiledObjectGroup;
Import Com.badlogic.gdx.math.Vector2;
Import Com.badlogic.gdx.math.Vector3;
Import Com.badlogic.gdx.scenes.scene2d.Actor;
Import Com.badlogic.gdx.scenes.scene2d.Stage;
Import Com.badlogic.gdx.scenes.scene2d.ui.Image;
Import Com.badlogic.gdx.scenes.scene2d.ui.Label;
Import Com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;

public class Firstgame implements Applicationlistener, Inputprocessor {

Stage stage;
float width;
float height;
Private Tiledmap map;
Private Tileatlas Atlas;
Private Tilemaprenderer Tilemaprenderer;
Image player;
Vector3 camdirection = new Vector3 (1, 1, 0);
Vector2 maxcamposition = new Vector2 (0, 0);
Vector3 movevector = new Vector3 (0, 0, 0);
Boolean ispress;

Image Image;

@Override
public void Create () {
Final String Path = "map/";
Final String mapname = "Tilemap";
FileHandle maphandle = Gdx.files.internal (path + mapname + ". tmx");
Map = Tiledloader.createmap (maphandle);
Atlas = new Tileatlas (map, Gdx.files.internal ("map/"));
Tilemaprenderer = new Tilemaprenderer (map, Atlas, 10, 10);
Maxcamposition.set (Tilemaprenderer.getmapwidthunits (), Tilemaprenderer
. Getmapheightunits ());

width = Gdx.graphics.getWidth ();
Height = Gdx.graphics.getHeight ();
stage = new stage (width, height, true);
Label label = new Label ("FPS:", New LabelStyle (New Bitmapfont (gdx.files
. Internal ("Font/blue.fnt"),
Gdx.files.internal ("Font/blue.png"), false), Color.White),
"Fpslabel");
Label.y = Height-label.getprefheight ();
label.x = 0;
Stage.addactor (label);

for (Tiledobjectgroup group:map.objectGroups) {
for (Tiledobject object:group.objects) {
if ("Play1". Equals (Object.name)) {
Player = new Image (new Textureregion (New Texture (gdx.files
. Internal ("Map/player.png")), 0, 0, 27, 40));
player.x = Object.x;
Player.y = Tilemaprenderer.getmapheightunits ()-object.y; Map is the upper left corner and stage is the lower left corner
Stage.addactor (player);
}
}
}

Inputmultiplexer inputmultiplexer = new Inputmultiplexer ();
Inputmultiplexer.addprocessor (this);
Inputmultiplexer.addprocessor (stage);
Gdx.input.setInputProcessor (Inputmultiplexer);
}

@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);
Orthographiccamera C = (Orthographiccamera) Stage.getcamera ();
if (ispress) {
Cameramove (Movevector);
}
(Label) Stage.findactor ("Fpslabel"). SetText ("FPS:"
+ Gdx.graphics.getFramesPerSecond ());
Stage.act (Gdx.graphics.getDeltaTime ());
Tilemaprenderer.render (c);
Stage.draw ();
}

private void Cameramove (Vector3 Vector3) {
Stage.getcamera (). Position.add (Vector3);
For (Actor actor:stage.getActors ()) {
Actor.x + = vector3.x;
Actor.y + = Vector3.y;
}
}

@Override
public void Resize (int width, int height) {
TODO auto-generated Method Stub

}

@Override
public void Resume () {
TODO auto-generated Method Stub

}

@Override
public boolean keyDown (int keycode) {
TODO auto-generated Method Stub
return false;
}

@Override
public boolean keytyped (char character) {
TODO auto-generated Method Stub
return false;
}

@Override
public boolean keyUp (int keycode) {
TODO auto-generated Method Stub
return false;
}

@Override
public boolean scrolled (int amount) {
TODO auto-generated Method Stub
return false;
}

private void Changedirect (int typeId) {
Switch (typeId) {
Case 1:
Movevector.set (0, 1, 0);
Gdx.app.log ("Direction change", "upward");
Break
Case 2:
Movevector.set (0,-1, 0);
Gdx.app.log ("Direction change", "downward");
Break
Case 3:
Movevector.set (-1, 0, 0);
Gdx.app.log ("Direction change", "left");
Break
Case 4:
Movevector.set (1, 0, 0);
Gdx.app.log ("Direction change", "right");
Break
}
}

@Override
public boolean touchDown (int x, int y, int pointer, int button) {
Vector3 tmp = new Vector3 (x, y, 0);
Stage.getcamera (). Unproject (TMP);
float newx = tmp.x-player.x;
float newy = tmp.y-player.y;
if (newx > 0 && newy > 0) {
if (Newx > Newy) {
Changedirect (4);
} else {
Changedirect (1);
}
} else if (newx > 0 && newy < 0) {
if (Newx >-newy) {
Changedirect (4);
} else {
Changedirect (2);
}
} else if (newx < 0 && newy > 0) {
if (-newx > Newy) {
Changedirect (3);
} else {
Changedirect (1);
}
} else {
if (-newx >-newy) {
Changedirect (3);
} else {
Changedirect (2);
}
}
Ispress = true;
return false;
}

@Override
public boolean touchdragged (int x, int y, int pointer) {
TODO auto-generated Method Stub
return false;
}

@Override
public boolean touchmoved (int x, int y) {
TODO auto-generated Method Stub
return false;
}

@Override
public boolean touchUp (int x, int y, int pointer, int button) {
Ispress = false;
Gdx.app.log ("Info", "touchup:x:" + x + "y:" + y + "pointer:"
+ pointer + "button:" + button);
return false;
}
}



Final effect: (Image loading may be a bit slow)

(... No words ...

I do not know how to record the phone screen, so only with the simulator demo, but the real machine (ZTE V880) speed is very smooth, absolutely no problem.

If you have more than one role, the method is the same, build several more objects on the line. It can be seen that our ninja is very high-level ... There is no obstacle to walking the map, and if you keep walking you will find that the map will disappear, and the articles will be solved slowly.

Android Game Development Framework LIBGDX use (13) role and character movement in-tiledmap

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.