Simple examples of Android game development tutorials

Source: Internet
Author: User
Tags stub

Map we created the next is the protagonist's appearance. In fact, the above describes how to tiledmap and stage combination, the role of the 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 and add an object layer.

Add a shape to where the protagonist is going to appear

Named Play1

Our main characters are:

The idea is that we go through all of the object in the map, and if the name is consistent with the play1 we set, then instantiate an image, position and object, and add it to the stage.

Key code:

The code is as follows Copy Code

for (Tiledobjectgroup group:map.objectGroups) {


for (Tiledobject object:group.objects) {


if ("Play1". Equals (Object.name)) {


Player = new Image (new textureregion 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, stage is the lower left corner.


Stage.addactor (player);


}


}


}

The effect is as follows:

And now let's try to get the protagonist to move.

The first is how we control, the Android device is preferred to use touch control. If we hold the front, the protagonist forwards. Hold the top down, lead up.

So how do we determine which direction we're holding down?

As shown in the picture, the yellow is stage, the pink border is the entire map, and part of the display is not shown. The green point in the lower right corner is the protagonist's position, and we assume that the red point is our touch point.

Identify the red touch point for the forward, I am providing a solution, but the method is not unique ha, I am so sure direction is not necessarily the most consistent with the user experience.

A coordinate system is established with the position of the protagonist as the origin, and the new coordinate x,y of the touch point is obtained.

The quadrant of the touch point in the new coordinate system is determined, and the direction can be known by judging the size of the x,y.

The code is as follows:

The code is as follows Copy Code
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);


}


}

The direct movement camera position may move the map, but our protagonist has disappeared from the map ... The solution is to move the coordinates of the actor that you want to still show on the map with the camera.

The code is as follows:

The code is as follows Copy Code

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:

The code is as follows Copy 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 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 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, 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:"
&nbs p;               + pointer + "button:" + button);
        return false;
   }
}

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.