FAQ

Here is a list of questions or issues people typically encounter when making an app and their respective answers or solutions.

Will this blog give me all the answers to making an app?
Unfortunately, no. There's no way we can anticipate all of the questions or issues you may encounter. However, this blog is a great resource to learn about the process and it may help you tackle a few specific problems.

How do I get started?
Just dive right in! There's some great getting started tutorials all over the internet. Try following one of those until you start to get the hang of things and then make it your own. If you run into issues, try reading through this blog for help.

What should I do if I don't know how to code?
You'll probably want to learn how to do that before you start working on an app or game. Check out our external resources page for some great tutorials.

Can I use a different language other than Java?
Yes. We chose Java because one of us already had a lot of experience with the language. It's also extremely powerful and the main language used for Android and libGDX.

But Java is hard....
If you're having trouble picking up Java, there's a bunch of other options out there. There are some game engines (like libGDX) that have their own language. Many of them also have a drag and drop environment and simpler coding than you'll see with libGDX. The drawback is you miss out on some of the power and control you'll have with Java. If you're especially interested in making games, this might be a great route to take. Try checking out https://coronalabs.com/



How do I attach a picture to a box2D body?
First create a texture and then a sprite. See the following link to learn how:
https://github.com/libgdx/libgdx/wiki/Spritebatch%2C-Textureregions%2C-and-Sprites

Then you will need to update the sprite's properties to match the body with every frame. Here's how we did it:

We first created a class called DynamicSprite that extends the Sprite class and allows the attachment of a Body object. Then, with every frame, the DynamicSprite object's update() method is called that updates its properties. Here's what that method looks like:

public void update() {
    Vector2 position = getBody().getWorldCenter();
    float radius = getShape().getRadius();
    setSize(radius * 2, radius * 2);
    setPositionCenter(position.x, position.y);
    setOrigin(getWidth() / 2, getHeight() / 2);
    setRotation((float)Math.toDegrees(physicsBody.getAngle()));
}

And here's where it's called inside the render() loop:

batch.begin();
batch.setProjectionMatrix(camera.combined); //This syncs the screen coordinates with the camera
for(DynamicSprite sprite : sprites) {
    sprite.update();
    sprite.draw(batch);
}
batch.end();



How do I create a menu or HUD?
You should look into using scene2d. This will let you set the menu elements up on the screen exactly the way you want. It will also allow lots of functionality such as making tables and scrolling menus. Read the documentation here before continuing:
https://github.com/libgdx/libgdx/wiki/Scene2d.ui

Here's an example of how to make a button:
http://stackoverflow.com/questions/21488311/libgdx-how-to-create-a-button

Finally, a good way to organize buttons and other menu elements on the screen instead of setting pixel positions manually is to create a table. This will also help scale things to different screen sizes. See this link:
https://github.com/libgdx/libgdx/wiki/Table

How do I add pictures to my elements (e.g. buttons) in Scene2d?
You first need to create a .pack file using a scene2d texture packer tool. This is what you will use to create a Texture Atlas. A Texture Atlas is basically just a big picture filled with all of the images that will be used in the app. It is more efficient in memory to store them all as one. The texture packer tool is just used to arrange them in the most efficient way possible. It also gives you a .json file (inside the .pack file) with coordinates and dimensions for every image so that you can access them easily in the app. Here's a link to download the texture packer tool:
https://code.google.com/p/libgdx-texturepacker-gui/downloads/list

And here's a link to some documentation explaining how to use it:
https://github.com/libgdx/libgdx/wiki/Texture-packer

The next thing you will need to do is use the .pack file in the app to create a texture atlas. You can then You can use the following code as an example:

        font = new BitmapFont();
        skin = new Skin();
        buttonAtlas = new TextureAtlas(Gdx.files.internal("buttons/buttons.pack"));
        skin.addRegions(buttonAtlas);
        textButtonStyle = new TextButtonStyle();
        textButtonStyle.font = font;
        textButtonStyle.up = skin.getDrawable("up-button");
        textButtonStyle.down = skin.getDrawable("down-button");
        textButtonStyle.checked = skin.getDrawable("checked-button");
        button = new TextButton("Button1", textButtonStyle);
This was taken from the following link:
http://stackoverflow.com/questions/21488311/libgdx-how-to-create-a-button

You might also find the following links helpful:
https://github.com/libgdx/libgdx/wiki/Skin
https://github.com/libgdx/libgdx/wiki/Scene2d.ui#skin
http://stackoverflow.com/questions/16182844/default-skin-libgdx


How do I pause my game?
You can pause just by setting the timestep = 0. The timestep is what is used to advance all of the physics by a certain amount of time. Every frame that elapses (iteration of the render loop) the following code should be called:

world.step(timestep, 6, 2);

The variable timestep is the amount of time to elapse the game by. This is commonly set at 1/60 for 60 frames per second. If it is set at 0, no time will elapse and none of the physics will advance.

Here are the methods we used to control the pause in our app:
/** * This method pauses the game by setting timestep = 0 */public void pauseGame() {
    running = false;
    timeStep = 0;
}

/** * This method unpauses the game by changing the timestep back to TIMESTEP */public void unpauseGame() {
    running = true;
    timeStep = TIMESTEP;
}

How do I export my app?
It will be different depending on the platform. This link provides all the information you need.
https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-the-project

more to come...

No comments:

Post a Comment