I hope you can help me and I hope I did not overlook any relevant answers.
So I get this Exception when I try and run my DesktopLauncher.java class.
Executing: gradle run
Configuration on demand is an incubating feature.
warning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
:core:compileJava
:core:processResources UP-TO-DATE
:core:classes
:core:jar
warning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
:desktop:compileJava
:desktop:processResources UP-TO-DATE
:desktop:classes
Exception in thread "LWJGL Application" java.lang.ClassCastException: com.bluemoon.game.MainGame cannot be cast to com.badlogic.gdx.InputProcessor
at com.bluemoon.game.MainGame.create(MainGame.java:30)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
:desktop:run
BUILD SUCCESSFUL
Total time: 4.267 secs
I just copied the my code from this tutorial.
This is my MainGame.java from the core files
package com.bluemoon.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
public class MainGame extends ApplicationAdapter {
Texture img;
TiledMap tiledMap;
OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;
#Override
public void create () {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false,w,h);
camera.update();
tiledMap = new TmxMapLoader().load("basic_map.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
Gdx.input.setInputProcessor((InputProcessor) this);
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
tiledMapRenderer.setView(camera);
tiledMapRenderer.render();
}
}
And this is my DesktopLauncher.java
package com.bluemoon.game.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.bluemoon.game.MainGame;
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.fullscreen = true;
cfg.resizable = false;
new LwjglApplication(new MainGame(), cfg);
}
}
When I run the DesktopLauncher.java, the program is built, then the screen turns black (probably because it's going into full screen mode). After about 1-2 seconds the program crashes.
I'm using gradle a packet management system, if that's important.
It is because MainGame class doesn't implement InputProcessor class. It should be:
public class MainGame extends ApplicationAdapter implements InputProcessor {
Of course, you will have to implement all methods from InputProcessor.
Related
I have an issue using LibGDX with assets directory. I actually build my project folder this way. I follow this tutorial to learn. (I work on Eclipse)
The code I use is :
package com.mygdx.game;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;
public class MyGdxGame implements ApplicationListener {
private SpriteBatch batch;
private TextureAtlas textureAtlas;
private Sprite sprite;
private int currentFrame = 1;
private String currentAtlasKey = new String("0001");
#Override
public void create() {
batch = new SpriteBatch();
// THE PROBLEM IS UNDER THIS LINE
textureAtlas = new TextureAtlas(Gdx.files.internal("spritesheet.atlas"));
AtlasRegion region = textureAtlas.findRegion("0001");
sprite = new Sprite(region);
sprite.setPosition(120, 100);
sprite.scale(2.5f);
Timer.schedule(new Task(){
#Override
public void run() {
currentFrame++;
if(currentFrame > 20)
currentFrame = 1;
// ATTENTION! String.format() doesnt work under GWT for god knows why...
currentAtlasKey = String.format("%04d", currentFrame);
sprite.setRegion(textureAtlas.findRegion(currentAtlasKey));
}
}
,0,1/30.0f);
}
#Override
public void dispose() {
batch.dispose();
textureAtlas.dispose();
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
(if the code is hard to read I use exactly the same code that is in the tutorial linked below)
My package explorer looks like this :
imgur link
And it returns me :
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: File not found: spritesheet.atlas (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.(TextureAtlas.java:103)
at com.badlogic.gdx.graphics.g2d.TextureAtlas.(TextureAtlas.java:231)
at com.badlogic.gdx.graphics.g2d.TextureAtlas.(TextureAtlas.java:226)
at com.badlogic.gdx.graphics.g2d.TextureAtlas.(TextureAtlas.java:216)
at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:23)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
I tried some trick like Project>Clean, refreshing, close and re-open Eclipse and even recreated a project. Any idea ?
Okay, I found the trick. For those which encounter the same issue (working on Eclipse but it's pretty the same whatever the IDE is) there is one thread on stack overflow already existing which give additional solutions.
For me I had to set-up the "working directory" (for the desktop main for exemple). To do this go on Run>Run configuration>Arguments and at working directory's section there is Default and Other. Tick Other's box and me I had to write ${workspace_loc:my-gdx-game-core/assets} but I think it works like ${workspace_loc:[name-of-your-core-directory]/assets}. Hope it helped.
maybe you haven't linked the asset folder, if so,
try right click on my-gdx-game-desktop, in properties dialog select Java Build Path, click link source button, then navigate to the assets folder.
if you're already set it, try readd with this step, restart eclipse if needed
I'm getting this error while working on Android Studio with libGDX.
Here's my code:
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.Animation;
public class MyGdxGame extends ApplicationAdapter {
private SpriteBatch batch;
private TextureAtlas shooterAtlas;
private Animation animation;
private float timePassed = 0 ;
#Override
public void create () {
batch = new SpriteBatch();
shooterAtlas = new TextureAtlas(Gdx.files.internal("shooter.atlas"));
animation = new Animation(1/30f,shooterAtlas.getRegions());
}
#Override
public void dispose() {
batch.dispose();
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
timePassed += Gdx.graphics.getDeltaTime();
batch.draw(animation.getKeyFrame(timePassed,true),300,500);
batch.end();
}
}
first i had this code:
import javafx.animation.animation
instead of:
import com.badlogic.gdx.graphics.g2d.Animation;
but it was giving me error on this:
animation = new Animation(1/30f,shooterAtlas.getRegions());
that:
animation is an abstract cannot be instantiated
But then I import this:
import com.badlogic.gdx.graphics.g2d.Animation;
and delete:
import javafx.animation.animation
because it was giving me error that you cant have both and resolving with alt+enter didn't worked also.
But on compilation it gives the error:
Warning:[options] bootstrap class path not set in conjunction with -source 1.6
C:\Users\COMPUTER\Documents\new libgdx\core\src\com\mygdx\game\MyGdxGame.java
1 warning
Error:Execution failed for task ':core:compileJava'.
Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 1.526 secs
Information:2 errors
Information:1 warning
Information:See complete output in console
The last error is because you are compiling your code with the previous jdk version and you have a newer one try to set source and target both to the jdk version you currently have.
So I am making a Android Application Using LibGDX and Eclipse. I made a Core-/Desktop and Android-Project.
When I run my game in the desktop project, nothing is stretched out to the ground, but when I try to run it on my Galaxy S4, the screen gets all stretched out to the bottom of the screen.
This is the First class of my little App. (in Core)
package com.mygdx.ghost;
import states.GSM;
import states.PlayState;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.mygdx.ghost.handler.Content;
public class Ghost extends ApplicationAdapter {
public static final String TITLE= "Ghost Hunter";
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
public static Content res;
private SpriteBatch sb;
private GSM gsm;
public void create () {
Gdx.gl.glClearColor(0.2f,0.2f,0.3f,1);
res = new Content();
res.loadAtlas("pack.pack", "pack");
sb = new SpriteBatch();
gsm = new GSM();
gsm.push(new PlayState(gsm));
}
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
gsm.render(sb);
}
}
I am using 480x800 because I was promised that would be the game dimensions and not the screen dimensions.
I used the dimensions in the desktop project like this:
package com.mygdx.ghost.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.ghost.Ghost;
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.width = Ghost.WIDTH;
config.height = Ghost.HEIGHT;
config.title = Ghost.TITLE;
new LwjglApplication(new Ghost(), config);
}
}
Do I use them the same in my andriod project?
I'm new to Libgdx and am having some problems filling an image to the entire screen. If I import a 1900x1200 image, it fills the screen, but if instead the image is 1024x512, the image is not stretched to the screen. The following is the code I've used. I thought that ''background.setSize(stageWidth, stageHeight)'' would scale the image to the screen but this doesn't happen. Could you please inform me of what I'm doing wrong? I've tried toggling with the ''setFillParent'' (as mentioned in another post) but it still doesn't work. Here's also included a screenshot of what currently appears (both on the Desktop implementation and Android): http://tinypic.com/r/b7j20z/8
Thank You for your help!
package com.mygdx.game;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class MyGdxGame implements ApplicationListener{
private Stage stage;
#Override
public void create() {
stage = new Stage();
float stageHeight = Gdx.graphics.getHeight();
float stageWidth = Gdx.graphics.getWidth();
// Background
Texture board = new Texture(Gdx.files.internal("data/frame.png"));
Image background = new Image(board);
background.setOrigin(0, 0);
background.setSize(stageWidth, stageHeight);
background.rotateBy(0);
background.setPosition(0, 0);
stage.addActor(background);
}
#Override
public void dispose() {
stage.dispose();
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
After thinking about it for a bit, the most likely cause of your problem is that you're adding your image directly to the root of the stage. The root of the stage is a Group that doesn't handle any kind of layout managing, which is probably why your image isn't sized correctly.
I'd recommend instead that you create a Table, and then use setFillParent() so it takes up the entire screen. You can then simply use the Table's built in function, table.background(drawable), to set a background. However, you have to keep in mind that a Texture doesn't count as drawable for the table, so you have to use the class TextureRegionDrawable to get a valid drawable for the background. Here's an example:
table.background(new TextureRegionDrawable(new TextureRegion(board)));
I'm developing my first android game with libgdx, using as a base this amazing tutorial (http://www.kilobolt.com/day-11-supporting-iosandroid--splashscreen-menus-and-tweening.html). Everything works properly except for the tweening that I’m using in the splash screen to show the logo of my “company”. The weird part is that the logo works just fine in the desktop version, but in the android version is not being showed.
I’m using the same class that they used in the app developed in the tutorial. By the way, this app’s splash screen works fine in both versions.
I have compared my app and the tutorial’s app, but I found no differences except for this on the package explorer. I don't know if it means something:
http://i1223.photobucket.com/albums/dd507/victormmenendez/tut.jpg
package com.victor.Screens;
import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenEquations;
import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.victor.TweenAccessors.SpriteAccessor;
import com.victor.FZHelpers.AssetLoader;
import com.victor.FZombies.FZGame;
public class SplashScreen implements Screen
{
private TweenManager manager;
private SpriteBatch batcher;
private Sprite sprite;
private FZGame game;
public SplashScreen(FZGame game)
{
this.game = game;
}
#Override
public void show()
{
sprite = new Sprite(AssetLoader.logo);
sprite.setColor(1, 1, 1, 0);
float width = Gdx.graphics.getWidth();
float height = Gdx.graphics.getHeight();
float desiredWidth = width * .4f;
float scale = desiredWidth / sprite.getWidth();
sprite.setSize(sprite.getWidth() * scale, sprite.getHeight() * scale);
sprite.setPosition((width / 2) - (sprite.getWidth() / 2), (height / 2) - (sprite.getHeight() / 2));
setupTween();
batcher = new SpriteBatch();
}
private void setupTween()
{
Tween.registerAccessor(Sprite.class, new SpriteAccessor());
manager = new TweenManager();
TweenCallback cb = new TweenCallback()
{
#Override
public void onEvent(int type, BaseTween<?> source)
{
game.setScreen(new GameScreen());
}
};
Tween.to(sprite, SpriteAccessor.ALPHA, .8f).target(1).ease(TweenEquations.easeInOutQuad).repeatYoyo(1, .4f).setCallback(cb).setCallbackTriggers(TweenCallback.COMPLETE).start(manager);
}
#Override
public void render(float delta)
{
manager.update(delta);
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batcher.begin();
sprite.draw(batcher);
batcher.end();
}
}
Thank you very much for your help. I'm sorry if in any moment I couldn’t explain myself, but English is not my primary language. If you need any extra piece of code, I can add it.
You need to make sure you export the tween jar files in your Build Path. For Eclipse, you can Right-click on the project, choose "Build Path" > "Configure Build Path", go to the "Order and Export" tab on the right side, and make sure that the tween jar files are checked in the list there.