Use of the android game development framework libgdx (13)-Role and role movement in TiledMap

Source: Internet
Author: User
Tags libgdx

The libgdx used in this article is version 0.92, which may be different from the latest version. The full text is for reference only.

This article follows up with the preceding article. Address: Use of the android game development framework libgdx (12)-Use of the TiledMap

After the map is created, the next step is the appearance of the main character. In fact, the above section describes how to combine TiledMap and Stage, and the role processing is simple.

I can inherit the Actor class to create the main role class, so I will be a lazy and use Image instead.

Edit our TMX file and add an Object layer.

Add a shape to the place where the main character appears

Named play1

Our main character is:

The idea is that we traverse all objects in the map. If the name is the same as play1 we set, an Image will be instantiated, And the location will be the same as the Object and added to the stage.

Key code:

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 indicates the upper left corner, and Stage indicates the lower left corner.
Stage. addActor (player );
}
}
}

The effect is as follows:

Now let's try to make the main character move.

The first is how we control the touch. for android devices, touch is preferred. If we hold down the front, the main character moves forward. Hold down the top and keep the main character up.

So how can we determine which direction we hold down?

The color is yellow, and the pink border is the whole Map. Some of them are displayed, but some are not. The Green Point in the lower right corner is the main character's position. We assume that the red point is our touch point.

I think the red touch point is forward. I am providing a solution, but the method is not unique. In this way, it is not necessarily consistent with the user experience.

Create a coordinate system with the starting point of the main character to obtain the new coordinates x, y of the touch point.

Determine the quadrant of the touch point in the new coordinate system, and then determine the size of x and y to know the direction.

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) {
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);
}
}

You can directly move the Camera position to move the map, but our protagonist disappears from the map... The solution is to move the coordinates of the Actor you want to remain on the map along with 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;
}
}

Complete 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 indicates the upper left corner, and Stage indicates 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 ("change direction", "up ");
Break;
Case 2:
MoveVector. set (0,-1, 0 );
Gdx. app. log ("change direction", "down ");
Break;
Case 3:
MoveVector. set (-1, 0, 0 );
Gdx. app. log ("change direction", "Left direction ");
Break;
Case 4:
MoveVector. set (1, 0, 0 );
Gdx. app. log ("change direction", "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 slow)

(... Cannot be uploaded) in speechless...

 

I don't know how to record the mobile phone screen, so I only need to use a simulator to demonstrate it, but the real machine (ZTE V880) is very fast and can be completely OK.

If there are multiple roles, the method is the same. Just create several more objects. We can clearly see that our ninja level is very high... There is no obstacle in walking a map, and if you keep walking, you will find that a part of the map will disappear, and the next article will solve these problems slowly.

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.