Book Image

Learning Libgdx Game Development

By : Andreas Oehlke, Andreas Oehlke
Book Image

Learning Libgdx Game Development

By: Andreas Oehlke, Andreas Oehlke

Overview of this book

Game development is a field of interdisciplinary skills, which also makes it a very complex topic in many respects. One decision that usually needs to be made at the beginning of a game development processis to define the kind of computer system or platform the game will be developed for. This does not pose any problems in general but as soon as the game should also be able to run on multiple platforms it will become a developer's nightmare to maintain several distinct copies of the same game. This is where the libGDX multi-platform game development framework comes to the rescue! "Learning Libgdx Game Development" is a practical, hands-on guide that provides you with all the information you need to know about the libGDX framework as well as game development in general so you can start developing your own games for multiple platforms. You will gradually acquire deeper knowledge of both, libGDX and game development while you work through twelve easy-to-follow chapters. "Learning Libgdx Game Development" will walk you through a complete game development cycle by creating an example game that is extended with new features over several chapters. These chapters handle specific topics such as organizing resources, managing game scenes and transitions, actors, a menu system, using an advanced physics engine and many more. The chapters are filled with screenshots and/or diagrams to facilitate comprehension. "Learning Libgdx Game Development" is the book for you if you want to learn how to write your game code once and run it on a multitude of platforms using libGDX.
Table of Contents (19 chapters)
Learning Libgdx Game Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding parallax scrolling to the mountains in the background


Parallax scrolling is a special scrolling technique that creates the illusion of depth in a 2D scene. Therefore, the objects in the background move slower than the objects in the foreground when the camera moves by.

We will now implement a parallax scrolling effect for the mountains in the background of the game screen.

Add the following import line to the Mountains class:

import com.badlogic.gdx.math.Vector2;

After that, add the following lines of code to the same class:

public void updateScrollPosition (Vector2 camPosition) {
  position.set(camPosition.x, position.y);
}

Next, make the following changes to the same class:

private void drawMountain (SpriteBatch batch, float offsetX, floatoffsetY, float tintColor, float parallaxSpeedX) {
  TextureRegion reg = null;
  batch.setColor(tintColor, tintColor, tintColor, 1);
  float xRel = dimension.x * offsetX;
  float yRel = dimension.y * offsetY;

  // mountains span the whole level  
  int...