EzDevInfo.com

libgdx interview questions

Top libgdx frequently asked interview questions

How to deal with different aspect ratios in libGDX?

I have implemented some screens using libGDX that would obviously use the Screen class provided by the libGDX framework. However, the implementation for these screens works only with pre-defined screen sizes. For example, if the sprite was meant for a 640 x 480 size screen (4:3 Aspect ratio), it won't work as intended on other screen sizes because the sprites go par the screen boundaries and are not scaled to the screen size at all. Moreover, if simple scaling would have been provided by the libGDX, the issue I am facing would have still been there because that would cause the aspect ratio of the game screen to change.

After researching on internet, I came across a blog/forum that had discussed the same issue. I have implemented it and so far it is working fine. But I want to confirm whether this is the best option to achieve this or whether there are better alternatives. Below is the code to show how I am dealing with this legitimate problem.

FORUM LINK: http://www.java-gaming.org/index.php?topic=25685.new

public class SplashScreen implements Screen {

    // Aspect Ratio maintenance
    private static final int VIRTUAL_WIDTH = 640;
    private static final int VIRTUAL_HEIGHT = 480;
    private static final float ASPECT_RATIO = (float) VIRTUAL_WIDTH / (float) VIRTUAL_HEIGHT;

    private Camera camera;
    private Rectangle viewport;
    // ------end------

    MainGame TempMainGame;

    public Texture splashScreen;
    public TextureRegion splashScreenRegion;
    public SpriteBatch splashScreenSprite;

    public SplashScreen(MainGame maingame) {
        TempMainGame = maingame;
    }

    @Override
    public void dispose() {
        splashScreenSprite.dispose();
        splashScreen.dispose();
    }

    @Override
    public void render(float arg0) {
        //----Aspect Ratio maintenance

        // update camera
        camera.update();
        camera.apply(Gdx.gl10);

        // set viewport
        Gdx.gl.glViewport((int) viewport.x, (int) viewport.y,
        (int) viewport.width, (int) viewport.height);

        // clear previous frame
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        // DRAW EVERYTHING
        //--maintenance end--

        splashScreenSprite.begin();
        splashScreenSprite.disableBlending();
        splashScreenSprite.draw(splashScreenRegion, 0, 0);
        splashScreenSprite.end();
    }

    @Override
    public void resize(int width, int height) {
        //--Aspect Ratio Maintenance--
        // calculate new viewport
        float aspectRatio = (float)width/(float)height;
        float scale = 1f;
        Vector2 crop = new Vector2(0f, 0f);

        if(aspectRatio > ASPECT_RATIO) {
            scale = (float) height / (float) VIRTUAL_HEIGHT;
            crop.x = (width - VIRTUAL_WIDTH * scale) / 2f;
        } else if(aspectRatio < ASPECT_RATIO) {
            scale = (float) width / (float) VIRTUAL_WIDTH;
            crop.y = (height - VIRTUAL_HEIGHT * scale) / 2f;
        } else {
            scale = (float) width / (float) VIRTUAL_WIDTH;
        }

        float w = (float) VIRTUAL_WIDTH * scale;
        float h = (float) VIRTUAL_HEIGHT * scale;
        viewport = new Rectangle(crop.x, crop.y, w, h);
        //Maintenance ends here--
    }

    @Override
    public void show() {
        camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT); //Aspect Ratio Maintenance

        splashScreen = new Texture(Gdx.files.internal("images/splashScreen.png"));
        splashScreenRegion = new TextureRegion(splashScreen, 0, 0, 640, 480);
        splashScreenSprite = new SpriteBatch();

        if(Assets.load()) {
            this.dispose();
            TempMainGame.setScreen(TempMainGame.mainmenu);
        }
    }
}

UPDATE: I recently came to know that libGDX has some of its own functionality to maintain aspect ratios which I would like to discuss here. While searching the aspect ratio issue across the internet, I came across several forums/developers who had this problem of "How to maintain the aspect ratio on different screen sizes?" One of the solutions that really worked for me was posted above.

Later on when I proceeded with implementing the touchDown() methods for the screen, I found that due to scaling on resize, the co-ordinates on which I had implemented touchDown() would change by a great amount. After working with some code to translate the co-ordinates in accordance with the screen resize, I reduced this amount to a great extent but I wasn't successful to maintain them with pin point accuracy. For example, if I had implemented touchDown() on a texture, resizing the screen would shift the touchListener on the texture region some pixels to the right or left, depending on the resize and this was obviously undesired.

Later on I came to know that the stage class has its own native functionality to maintain the aspect ratio (boolean stretch = false). Now that I have implemented my screen by using the stage class, the aspect ratio is maintained well by it. However on resize or different screen sizes, the black area that is generated always appears on the right side of the screen; that is the screen is not centered which makes it quite ugly if the black area is substantially large.

Can any community member help me out to resolve this problem?


Source: (StackOverflow)

Changing the Coordinate System in LibGDX (Java)

LibGDX has a coordinate system where (0,0) is at the bottom-left. (like this image: http://i.stack.imgur.com/jVrJ0.png)

This has me beating my head against a wall, mainly because I'm porting a game I had already made with the usual coordinate system (where 0,0 is in the Top Left Corner).

My question: Is there any simple way of changing this coordinate system?


Source: (StackOverflow)

Advertisements

TrueType Fonts in libGDX

Does anyone know how I can use a TTF font in libGDX? I have looked around and have seen things about StbTrueTypeFont but it doesn't seem to be in the latest release.

EDIT: I found the StbTrueType font stuff, the jar file is located in the extensions directory. I've added it to my project. Now I just need to figure out how to use it. Any examples?


Source: (StackOverflow)

Actions of Actors in libgdx

I have made my Actor, but I am unclear on how to take advantage of the action and act methods. Outside of the basic Javadoc, I have not found a good tutorials on these methods.

Can anyone provide an example with comments for actions on actors?


Source: (StackOverflow)

Error at building model of new Gradle project for libgdx

I installed Gradle in eclipse and want to import a libgdx Gradle project. But when i click on "Build Model" button, i have an error at about 50% of the loading bar. Here is the problem :

> Plug-in: org.springsource.ide.eclipse.gradle.core Severity : error
> Message : org.eclipse.osgi.internal.framework.EquinoxConfiguration$1
> Exception Stack trace : java.lang.reflect.InvocationTargetException
>   at
> org.springsource.ide.eclipse.gradle.core.util.GradleRunnable.run(GradleRunnable.java:92)
>   at
> org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:122)
> Caused by: org.eclipse.core.runtime.CoreException:
> org.eclipse.osgi.internal.framework.EquinoxConfiguration$1    at
> org.springsource.ide.eclipse.gradle.core.GradleModelProvider$GroupedModelProvider.ensureModels(GradleModelProvider.java:284)
>   at
> org.springsource.ide.eclipse.gradle.core.GradleProject.getGradleModel(GradleProject.java:633)
>   at
> org.springsource.ide.eclipse.gradle.core.GradleProject.getSkeletalGradleModel(GradleProject.java:654)
>   at
> org.springsource.ide.eclipse.gradle.ui.wizards.GradleImportWizardPageOne$11.doit(GradleImportWizardPageOne.java:516)
>   at
> org.springsource.ide.eclipse.gradle.core.util.GradleRunnable.run(GradleRunnable.java:84)
>   ... 1 more Caused by: org.gradle.tooling.GradleConnectionException:
> Could not fetch model of type 'HierarchicalEclipseProject' using
> Gradle distribution
> 'http://services.gradle.org/distributions/gradle-1.11-all.zip'.   at
> org.gradle.tooling.internal.consumer.ResultHandlerAdapter.onFailure(ResultHandlerAdapter.java:55)
>   at
> org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor$1$1.run(DefaultAsyncConsumerActionExecutor.java:57)
>   at
> org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
>   at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
>   at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
>   at java.lang.Thread.run(Unknown Source)     at
> org.gradle.tooling.internal.consumer.BlockingResultHandler.getResult(BlockingResultHandler.java:46)
>   at
> org.gradle.tooling.internal.consumer.DefaultModelBuilder.get(DefaultModelBuilder.java:48)
>   at
> org.springsource.ide.eclipse.gradle.core.GradleModelProvider.buildModel(GradleModelProvider.java:385)
>   at
> org.springsource.ide.eclipse.gradle.core.GradleModelProvider$GroupedModelProvider.ensureModels(GradleModelProvider.java:290)
>   ... 5 more Caused by:
> org.gradle.launcher.daemon.client.DaemonConnectionException: Could not
> dispatch a message to the daemon.     at
> org.gradle.launcher.daemon.client.DaemonClientConnection.dispatch(DaemonClientConnection.java:57)
>   at
> org.gradle.launcher.daemon.client.DaemonClient.executeBuild(DaemonClient.java:168)
>   at
> org.gradle.launcher.daemon.client.DaemonClient.execute(DaemonClient.java:151)
>   at
> org.gradle.launcher.daemon.client.DaemonClient.execute(DaemonClient.java:74)
>   at
> org.gradle.tooling.internal.provider.DaemonBuildActionExecuter.execute(DaemonBuildActionExecuter.java:42)
>   at
> org.gradle.tooling.internal.provider.DaemonBuildActionExecuter.execute(DaemonBuildActionExecuter.java:29)
>   at
> org.gradle.tooling.internal.provider.LoggingBridgingBuildActionExecuter.execute(LoggingBridgingBuildActionExecuter.java:53)
>   at
> org.gradle.tooling.internal.provider.LoggingBridgingBuildActionExecuter.execute(LoggingBridgingBuildActionExecuter.java:30)
>   at
> org.gradle.tooling.internal.provider.ProviderConnection.run(ProviderConnection.java:106)
>   at
> org.gradle.tooling.internal.provider.ProviderConnection.run(ProviderConnection.java:93)
>   at
> org.gradle.tooling.internal.provider.DefaultConnection.getModel(DefaultConnection.java:133)
>   at
> org.gradle.tooling.internal.consumer.connection.ModelBuilderBackedModelProducer.produceModel(ModelBuilderBackedModelProducer.java:49)
>   at
> org.gradle.tooling.internal.consumer.connection.GradleBuildAdapterProducer.produceModel(GradleBuildAdapterProducer.java:42)
>   at
> org.gradle.tooling.internal.consumer.connection.BuildInvocationsAdapterProducer.produceModel(BuildInvocationsAdapterProducer.java:47)
>   at
> org.gradle.tooling.internal.consumer.connection.ModelBuilderBackedConsumerConnection.run(ModelBuilderBackedConsumerConnection.java:55)
>   at
> org.gradle.tooling.internal.consumer.DefaultModelBuilder$1.run(DefaultModelBuilder.java:59)
>   at
> org.gradle.tooling.internal.consumer.connection.LazyConsumerActionExecutor.run(LazyConsumerActionExecutor.java:82)
>   at
> org.gradle.tooling.internal.consumer.connection.ProgressLoggingConsumerActionExecutor.run(ProgressLoggingConsumerActionExecutor.java:58)
>   at
> org.gradle.tooling.internal.consumer.connection.LoggingInitializerConsumerActionExecutor.run(LoggingInitializerConsumerActionExecutor.java:44)
>   at
> org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor$1$1.run(DefaultAsyncConsumerActionExecutor.java:55)
>   at
> org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
>   at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
>   at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
>   at java.lang.Thread.run(Unknown Source) Caused by:
> org.gradle.messaging.remote.internal.MessageIOException: Could not
> write message Build{id=67f4f73f-8d68-4e23-87c2-648a4fec30c8.1,
> currentDir=C:\applications\Eclipse} to '/127.0.0.1:1598'.     at
> org.gradle.messaging.remote.internal.inet.SocketConnection.dispatch(SocketConnection.java:115)
>   at
> org.gradle.launcher.daemon.client.DaemonClientConnection.dispatch(DaemonClientConnection.java:51)
>   ... 23 more Caused by: java.io.NotSerializableException:
> org.eclipse.osgi.internal.framework.EquinoxConfiguration$1    at
> java.io.ObjectOutputStream.writeObject0(Unknown Source)   at
> java.io.ObjectOutputStream.writeObject(Unknown Source)    at
> java.util.HashMap.internalWriteEntries(Unknown Source)    at
> java.util.HashMap.writeObject(Unknown Source)     at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
> sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)   at
> java.lang.reflect.Method.invoke(Unknown Source)   at
> java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)   at
> java.io.ObjectOutputStream.writeSerialData(Unknown Source)    at
> java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)    at
> java.io.ObjectOutputStream.writeObject0(Unknown Source)   at
> java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)     at
> java.io.ObjectOutputStream.writeSerialData(Unknown Source)    at
> java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)    at
> java.io.ObjectOutputStream.writeObject0(Unknown Source)   at
> java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)     at
> java.io.ObjectOutputStream.writeSerialData(Unknown Source)    at
> java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)    at
> java.io.ObjectOutputStream.writeObject0(Unknown Source)   at
> java.io.ObjectOutputStream.writeObject(Unknown Source)    at
> org.gradle.messaging.remote.internal.Message.send(Message.java:40)    at
> org.gradle.messaging.remote.internal.DefaultMessageSerializer$MessageWriter.write(DefaultMessageSerializer.java:62)
>   at
> org.gradle.messaging.remote.internal.inet.SocketConnection.dispatch(SocketConnection.java:112)
>   ... 24 more Root exception: org.eclipse.core.runtime.CoreException:
> org.eclipse.osgi.internal.framework.EquinoxConfiguration$1    at
> org.springsource.ide.eclipse.gradle.core.GradleModelProvider$GroupedModelProvider.ensureModels(GradleModelProvider.java:284)
>   at
> org.springsource.ide.eclipse.gradle.core.GradleProject.getGradleModel(GradleProject.java:633)
>   at
> org.springsource.ide.eclipse.gradle.core.GradleProject.getSkeletalGradleModel(GradleProject.java:654)
>   at
> org.springsource.ide.eclipse.gradle.ui.wizards.GradleImportWizardPageOne$11.doit(GradleImportWizardPageOne.java:516)
>   at
> org.springsource.ide.eclipse.gradle.core.util.GradleRunnable.run(GradleRunnable.java:84)
>   at
> org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:122)
> Caused by: org.gradle.tooling.GradleConnectionException: Could not
> fetch model of type 'HierarchicalEclipseProject' using Gradle
> distribution
> 'http://services.gradle.org/distributions/gradle-1.11-all.zip'.   at
> org.gradle.tooling.internal.consumer.ResultHandlerAdapter.onFailure(ResultHandlerAdapter.java:55)
>   at
> org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor$1$1.run(DefaultAsyncConsumerActionExecutor.java:57)
>   at
> org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
>   at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
>   at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
>   at java.lang.Thread.run(Unknown Source)     at
> org.gradle.tooling.internal.consumer.BlockingResultHandler.getResult(BlockingResultHandler.java:46)
>   at
> org.gradle.tooling.internal.consumer.DefaultModelBuilder.get(DefaultModelBuilder.java:48)
>   at
> org.springsource.ide.eclipse.gradle.core.GradleModelProvider.buildModel(GradleModelProvider.java:385)
>   at
> org.springsource.ide.eclipse.gradle.core.GradleModelProvider$GroupedModelProvider.ensureModels(GradleModelProvider.java:290)
>   ... 5 more Caused by:
> org.gradle.launcher.daemon.client.DaemonConnectionException: Could not
> dispatch a message to the daemon.     at
> org.gradle.launcher.daemon.client.DaemonClientConnection.dispatch(DaemonClientConnection.java:57)
>   at
> org.gradle.launcher.daemon.client.DaemonClient.executeBuild(DaemonClient.java:168)
>   at
> org.gradle.launcher.daemon.client.DaemonClient.execute(DaemonClient.java:151)
>   at
> org.gradle.launcher.daemon.client.DaemonClient.execute(DaemonClient.java:74)
>   at
> org.gradle.tooling.internal.provider.DaemonBuildActionExecuter.execute(DaemonBuildActionExecuter.java:42)
>   at
> org.gradle.tooling.internal.provider.DaemonBuildActionExecuter.execute(DaemonBuildActionExecuter.java:29)
>   at
> org.gradle.tooling.internal.provider.LoggingBridgingBuildActionExecuter.execute(LoggingBridgingBuildActionExecuter.java:53)
>   at
> org.gradle.tooling.internal.provider.LoggingBridgingBuildActionExecuter.execute(LoggingBridgingBuildActionExecuter.java:30)
>   at
> org.gradle.tooling.internal.provider.ProviderConnection.run(ProviderConnection.java:106)
>   at
> org.gradle.tooling.internal.provider.ProviderConnection.run(ProviderConnection.java:93)
>   at
> org.gradle.tooling.internal.provider.DefaultConnection.getModel(DefaultConnection.java:133)
>   at
> org.gradle.tooling.internal.consumer.connection.ModelBuilderBackedModelProducer.produceModel(ModelBuilderBackedModelProducer.java:49)
>   at
> org.gradle.tooling.internal.consumer.connection.GradleBuildAdapterProducer.produceModel(GradleBuildAdapterProducer.java:42)
>   at
> org.gradle.tooling.internal.consumer.connection.BuildInvocationsAdapterProducer.produceModel(BuildInvocationsAdapterProducer.java:47)
>   at
> org.gradle.tooling.internal.consumer.connection.ModelBuilderBackedConsumerConnection.run(ModelBuilderBackedConsumerConnection.java:55)
>   at
> org.gradle.tooling.internal.consumer.DefaultModelBuilder$1.run(DefaultModelBuilder.java:59)
>   at
> org.gradle.tooling.internal.consumer.connection.LazyConsumerActionExecutor.run(LazyConsumerActionExecutor.java:82)
>   at
> org.gradle.tooling.internal.consumer.connection.ProgressLoggingConsumerActionExecutor.run(ProgressLoggingConsumerActionExecutor.java:58)
>   at
> org.gradle.tooling.internal.consumer.connection.LoggingInitializerConsumerActionExecutor.run(LoggingInitializerConsumerActionExecutor.java:44)
>   at
> org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor$1$1.run(DefaultAsyncConsumerActionExecutor.java:55)
>   at
> org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
>   at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
>   at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
>   at java.lang.Thread.run(Unknown Source) Caused by:
> org.gradle.messaging.remote.internal.MessageIOException: Could not
> write message Build{id=67f4f73f-8d68-4e23-87c2-648a4fec30c8.1,
> currentDir=C:\applications\Eclipse} to '/127.0.0.1:1598'.     at
> org.gradle.messaging.remote.internal.inet.SocketConnection.dispatch(SocketConnection.java:115)
>   at
> org.gradle.launcher.daemon.client.DaemonClientConnection.dispatch(DaemonClientConnection.java:51)
>   ... 23 more Caused by: java.io.NotSerializableException:
> org.eclipse.osgi.internal.framework.EquinoxConfiguration$1    at
> java.io.ObjectOutputStream.writeObject0(Unknown Source)   at
> java.io.ObjectOutputStream.writeObject(Unknown Source)    at
> java.util.HashMap.internalWriteEntries(Unknown Source)    at
> java.util.HashMap.writeObject(Unknown Source)     at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
> sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)   at
> java.lang.reflect.Method.invoke(Unknown Source)   at
> java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)   at
> java.io.ObjectOutputStream.writeSerialData(Unknown Source)    at
> java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)    at
> java.io.ObjectOutputStream.writeObject0(Unknown Source)   at
> java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)     at
> java.io.ObjectOutputStream.writeSerialData(Unknown Source)    at
> java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)    at
> java.io.ObjectOutputStream.writeObject0(Unknown Source)   at
> java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)     at
> java.io.ObjectOutputStream.writeSerialData(Unknown Source)    at
> java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)    at
> java.io.ObjectOutputStream.writeObject0(Unknown Source)   at
> java.io.ObjectOutputStream.writeObject(Unknown Source)    at
> org.gradle.messaging.remote.internal.Message.send(Message.java:40)    at
> org.gradle.messaging.remote.internal.DefaultMessageSerializer$MessageWriter.write(DefaultMessageSerializer.java:62)
>   at
> org.gradle.messaging.remote.internal.inet.SocketConnection.dispatch(SocketConnection.java:112)
>   ... 24 more

Session data :

eclipse.buildId=4.4.1.M20140925-0400
java.version=1.8.0_20
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
Framework arguments:  -product org.eclipse.epp.package.java.product
Command-line arguments:  -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.java.product

Any help will be appreciated


Source: (StackOverflow)

Creating an iOS library or framework using libgdx (roboVM)

Is it possible to create an iOS library or framework using libgdx (RoboVM) that can be imported into Xcode?

Background: One of my colleagues has created a 3D visualisation app as a libgdx project for android and windows desktop. It can be compiled to run on iOS using RoboVM. However, I would like to wrap extra native user interface elements around it using Xcode. I know its possible to build the user interface programmatically via RoboVM but I would be keen to investigate if its possible to bring the existing work into Xcode. I don't need to edit the 3D visualisation component but add extra GUI elements around the 3D Vis window. I thought compiling the libgdx (RoboVM) code to a framework or library might be a solution that could be imported?!


Source: (StackOverflow)

Andengine vs libgdx

Has any one got recommendations (technical, functional, or otherwise) for one of these game engines over the other?

I've worked through a couple of tutorials for each of them, and they both seem pretty functional, but at this point I don't know enough about either of them to make a decision either way. I think either of them will require a fairly large number of hours invested in learning the engine so I'm keen to try and pick one I'll be wanting to stick with.

So far it seems that AndEngine is slightly more popular, but libgdx has the ability to run games on the desktop which seems a pretty big advantage...

I guess also, I'm fairly new to android development, and am finding it a bit of a struggle - is one of these significantly easier than the other?


Source: (StackOverflow)

Default Skin LibGDX?

I've been following this: https://code.google.com/p/table-layout/#Quickstart to get a little introduction to tables in LibGDX. I already experimented around a little with buttons.

Now I have this code:

    Label introLabel = new Label("Skip Intro", skin);
    TextField introText = new TextField("", skin);

    table.add(introLabel);
    table.add(introText).width(100);
    table.row();

But it throws me a NullPointerException because: No com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle registered with name: default

I only added my buttons (from somewhere else in the screen) into the skin:

    atlas = new TextureAtlas("assets/gui/buttons/alpha_generic_buttons.pack");

    skin = new Skin();
    skin.addRegions(atlas);

My question would now be what kind of textures a table needs and most of all, how I use them with the table.


Source: (StackOverflow)

java and libGDX / LWJGL game fullscreen wrong size for multiple monitors on Ubuntu

I'm working on a libGDX (library on top of LWJGL) game project, and use the Intellij IDEA IDE from several different workstations:

  • Windows 7 x64 laptop with two displays (1920x1080 and 1600x1200), nVidia GT540M.
  • Ubuntu 12.04 LTS on a laptop with a single display (1366x768), Intel integrated graphics.
  • Ubuntu 12.04 LTS on a desktop with two displays (1920x1080 and 1280x1024), nVidia GTS 450.

I'm using the OpenJDK for Java 6 on the Ubuntu boxes, and Sun/Oracle Java 6 on the Windows box (I heard Java 6 was the one to use for Android compatibility).

When running on full-screen:

  • Windows 7 laptop: works fine.
  • Ubuntu laptop: works fine.
  • Ubuntu desktop: background image is shown enlarged, and only part of it fits on the screen. +

Looking into this further, I see that the calls to Gdx.graphics.getHeight() and Gdx.graphics.getWidth() return the size of the rectangle needed to cover both displays, in my case 3200x1080, but when I tell my game to run full-screen, it only uses one of the displays, so the cameras get set to 1920x1080, but my camera movement and Tiled map panning think they've got 3200x1080 to work with, making things distorted and unusable (since character can walk off of the screen to the right).

I'm guessing my problem actually comes from the sizes returned by the awt.Toolkit's getScreenSize() call, but I don't know how to interrogate it more deeply to get the size of the screen it will actually use when I go fullscreen.

My DesktopStarter gets the screen size and sets fullscreen as follows:

    LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
    Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
    cfg.width = screenDimension.width;
    cfg.height = screenDimension.height;
    cfg.fullscreen = true;
    new LwjglApplication(new Game1(), cfg);

Is there a work-around to get the height/width of just the display that "full screen" will actually launch into?

So the trouble I'm seeing, is that executing the game.jar file, exiting the game, then executing again, repeatedly, results in different display modes showing up in the list of modes returned by Gdx.graphics.getDisplayModes() -- as P.T. pointed out below, this is a thin wrapper around LWJGL's Display.getAvailableDisplayModes(). Why is this happening? Why would it be a different set of modes presented on subsequent runs on Ubuntu?

edit: per P.T.'s suggestion, put LWJGL references in question, since it seems to be LWJGL that's providing the list of display modes.

Thanks!


Source: (StackOverflow)

Getting a directory file and the ClassLoader for a libGDX Android game

I have a libGDX game project for Android, and I want to execute a Groovy script in it.

To do so, I am examining this example code: https://github.com/melix/grooidshell-example

They managed to execute Groovy embed in Java on Android. Particularly GrooidShell.java (https://github.com/melix/grooidshell-example/blob/master/GroovyDroid/src/main/java/me/champeau/groovydroid/GrooidShell.java)

I have managed to implement most of the code in the Android launcher of the libGDX project. However, I cannot run it because I am missing two arguments:

public GrooidShell(File tmpDir, ClassLoader parent) {

The first one can be any directory. And the second one, I don't even know what is it for.

My question is, what is the ClassLoader and File arguments supposed to be? I need to get them and use them in the AndroidLauncher class of libGDX, which is like this:

public class AndroidLauncher extends AndroidApplication {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MyGdxGame(), config);
    }
}

Source: (StackOverflow)

LibGDX - how to create a button?

I want to create a button that changes when the user hovers it, or clicking it.
I created the following variable -

Button buttonPlay = new Button();

I don't know what to do now, how to load the images? how to write text into the button?
how to implement the events / effects (hover, click)?

It will be very helpful if someone will just write me an example button.


Source: (StackOverflow)

When to use actors in libgdx? What are cons and pros?

I'm writing simple solar system simulator. This is my first libgdx project. I'm using a Stage and Actors for the main menu and is pretty handy especially touch events handling. But ... looking at the examples i see nobody uses actors in actual game logic. I wander if i should use actor as a parent of planet class or just write my own class tor that. The planets won't be touchable and they will be moved only between the frames so the third parameter of action MoveBy will have to be time between frames. That are the cons. What are the pros for using Actors?


Source: (StackOverflow)

How to display text with two-color background?

I need to create an app for android, where the 2-color text will be displayed on the 2-color background. See picture on the left. Then, the line should be moved with animation and result image should be like on the picture on the right.

I have the following questions:

  1. Should I use some 2d engine to do this? Or, will it be OK to use standard views? How to do it?
  2. How to draw the text like on the pictures?

pic1 --------- pic2


Source: (StackOverflow)

Could not find class XXX referenced from method XXX.

I'm working on a libGDX project and I have a class called CheerVArachnids that has another inline class which is an event listener. When I run this project on the desktop it works fine. BUT when I run on my Android device, it can't find that inline class and I get the following error:

Could not find class 'com.bbj.cva.CheerVArachnids$PlaceUnitListener', referenced from method com.bbj.cva.CheerVArachnids.<init>

Here are the important parts of my class:

package com.bbj.cva;

public class CheerVArachnids implements ApplicationListener {

    class PlaceUnitListener implements EventSubscriber<PlaceUnitEvent> {

        @Override
        public void onEvent(PlaceUnitEvent event) 
        {   
            //
        }
    }

    public CheerVArachnids() {

        EventBus.subscribe(PlaceUnitEvent.class, new PlaceUnitListener());
        EventBus.subscribe(RemoveScreenObjectEvent.class,
                new RemoveScreenObjectListener());
    }
}

Any ideas why on Android, at runtime it can't find that inline class?


Source: (StackOverflow)

Libgdx setup UI gives 2 unexpected errors in new GWT project

I have run the setup ui, however, I get two different errors in the -html project than what is described in the tutorial: libgdx tutorial

The tutorial error stated is as follows (which is an error I do not see):

To fix the error of the HTML5/GWT project, go to the "Problems" view, right click the error message "The GWT SDK JAR gwt-servlet.jar is missing in the WEB-INF/lib directory" and select "Quick Fix". Click "Finish".

The errors I have are:

The project was not built since its build path is incomplete. Cannot find the class file for com.google.gwt.core.client.EntryPoint. Fix the build path then try building this project ...
The type com.google.gwt.core.client.EntryPoint cannot be resolved. It is indirectly referenced from required .class files

Is there anyway i can fix this? I just updated my eclipse, I also downloaded Version 18 ADT, revision 19 for Android SDK Tools, and revision 11 for Android SDK Platform-tools.


Source: (StackOverflow)