The document in download post have better format.
Creating the Scene class TiledMap
First
you need a map to show the scene, I recommend that you create the scene
by extended scene from class TiledScene that GDX has made available
for you
for example: public class extends AdvantureSceneTiledScene
We
have a constructor of tiled scene so, you need to have a some string
parameter that contains the path to the folder containing the file and
texture tmx file
tmx.,for example:
public AdventureScene (IGameService gameService, mapfile String, String textureDir)
public AdventureScene (IGameService gameService, mapfile String, String textureDir)
{super(gameService, mapfile, textureDir); / / ...
You
have used the protected variables inherited from parent class
TiledScene map to load objects that you have designed in tiledmap
editor. For example, I load the player object as designed in the tmx
file:
player = new Player (services, textureRegion)
for (TiledObjectGroup objectGroup: tiledMap.objectGroups)
{for(int i = 0; i <objectGroup.objects.size ( ); i + +)
if("player".equalsIgnoreCase (objectGroup.objects.get (i). name)) {
player.setX (objectGroup.objects.get (i). x);
player.setY (getMapHeight () - objectGroup.objects.get (i). y-20);
addItem
(player);}
You
set the xy coordinates of the player in the position of the object
"player" in the tmx file. Then you add to the collection of scenes
player in method addItem (...)
You just need to override the method renderForeground () to draw the player's score on the screen:
@ Override
public void renderForeground (float gametime)
{bigFont.setColor(Color. RED);
bigFont.draw(getBatch(), String.valueOf (player.score), 100,
100);}
Note:
you get the spriteBatch from getBatch method () of TiledScene class.
You may not use the method getSpriteBatch() of GameService.
Creating Player object
Player class should be inherited from class DefaultSprite
method
isPassPlayer () to check that it is stepping up tile can "pass" is not.
If this method returns false then player have come to obstacle tile so
player was not able to walk any more.
private isPass boolean ()
{
float x = this.getX ();
float y = this.getY (),
/ / Do not move if exceeded map bound
if (x <0) return false;
else if (y <0) return false;
if (x> tiledMap.getMapWidthUnits ()) return false;
else if (y> tiledMap.getMapHeightUnits ()) return false;
ispass boolean = true;
final int w = map.tileWidth;
Vector2 [] v = new Vector2 [2];
/ / We put 2 points on a test player to pass through the tile:
v [0] = new Vector2 (x, y )
v [1] = new Vector2 (v [0]. x + getRegionWidth () / 2, v [0]. y + getRegionHeight () / 2 -5)
for (int i = 0; i <v. length, i + +)
{int
row = (int) (tiledMap.getMapHeightUnits () - v [i]. y) / w,
int col = (int) v [i]. x / w;
map.getTileProperty String str = ( noPassLayer.tiles [row] [col], "NoPass")
/ / check if tile attribute "NoPass" or not
if ("1". equals (str))
{
ispass = false;
break;}
str = map.getTileProperty (foodLayer.tiles [row] [col], "Food")
if ("1". equals (str))
{
score + +;
break;
}
}
return
ispass;
}
Player need ramaining to base.update() method used to move and animation updates:
@ Override
public void update (float gametime) {
animation.isLooping= true;
oldPos.set (getX (), GetY ())
//Check if user press some direction key to controll the player.
if (Utils.isKeyDown (Keys.UP))
{direction
= UP;
setY (GetY () + 3);
checkPosition
();}
else if (Utils.isKeyDown (Keys.DOWN))
{direction
= DOWN;
setY (GetY () - 3);
checkPosition
();}
else if (Utils. isKeyDown (Keys.LEFT))
{direction
= LEFT;
setX (getX () - 3);
checkPosition
();}
else if (Utils.isKeyDown (Keys.RIGHT))
{direction
= RIGHT;
setX (getX () + 3 );
checkPosition
();}
else
{animation.isLooping=
false};
super.update(gametime);
}
In checkPosition method(), if the player steps into an obstacle tile, we reset its position :
private void checkPosition ()
{
if(! isPass ())
setPosition(oldPos);
}
Complete Game by class AdventureGame
AdvantureGame
Really not much different from the game class you wrote. However,
because this project is to use tilemap, so we'll have to check the
current scene is being used could be the render tiledScene or not to
make the change in rendering
@ Override
public void render ()
{if(scene == activeScene )
{
// clear the scenegame
Gdx.gl.glClearColor(0f, 0, 0.3f, 1);
Gdx.gl.glClear (GL20.GL_COLOR_BUFFER_BIT);
// Get tileRender from tiledScene
TileMapRenderer scene.getTileMapRenderer tileRender = ();
// render the background by the background layers' s index
int [] backgroundLayer = {0,1,4};
tileRender.render (gameService.getCamera (), backgroundLayer);
gametime = Gdx.graphics.getDeltaTime ();
// Update active active scene
activeScene.render(gametime);
batch.end ();
// render the foreground by the foreground layer 's index
int [] = {2} foregroundLayers;
tileRender.render (gameService.getCamera (), foregroundLayers)
// foreground of a scene render tiled
scene.renderforeground(gametime);
}
else
{
super.render();
}
Note:
You should pass the false value into setYdown() method because
currently GDX engine still can not use the coordinate system y "down" in
the tiled game. This means you will have to use coordinates with the
y-axis "up" explicitly.
@ Override
public void create (){
// set the oxygen system have axis-y axis down
setYdown (false);
No comments:
Post a Comment