Exception in thread "main" java.lang.Error: Unresolved compilation problem [message #1845074] |
Wed, 06 October 2021 01:17 |
Jeremy West Messages: 1 Registered: October 2021 |
Junior Member |
|
|
When trying to run my game, which is supposed to be a simple one where I control one ship, the other ship moves on its own, changing directions when it hits the boundary of the screen, and the npc ship stopping and going depending on if the two ships are colliding, I'm getting this error:
When trying to run my game, I'm getting this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor GDXGame() is undefined
at LaunchMyGame.main(LaunchMyGame.java:6)
This is my GDXGame.java code:
import com.badlogic.gdx.Game;
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.math.Rectangle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.Input.Keys;
public class GDXGame extends Game
{
private Sprite shipsprite;
private Texture backtexture;
private Sprite npcsprite;
private SpriteBatch batch;
private float posx, posy;
private float npcx, npcy;
private boolean overlapflag;
private int speed;
private int npcspeed;
private float delta;
private int scrwidth, scrheight;
public GDXGame(int w, int h)
{
super();
scrwidth = w;
scrheight = h;
npcspeed = 30;
}
public void create()
{
shipsprite = new Sprite( new Texture(Gdx.files.internal("ship.png")));
backtexture = new Texture(Gdx.files.internal("spacebk.jpg"));
npcsprite = new Sprite( new Texture(Gdx.files.internal("spaceshipx.png")));
batch = new SpriteBatch();
posx = 100;
posy = 200;
npcx = 800;
npcy = 800;
speed = 100;
shipsprite.setX(posx);
shipsprite.setY(posy);
npcsprite.setX(npcx);
npcsprite.setY(npcy);
overlapflag = false;
}
public void movenpcShip (Sprite n, float d, float spx)
{
if (npcx > 0)
npcx -= npcspeed;
if (npcx < 0)
npcx += -npcspeed;
}
public void moveShip(Sprite s, float d, float spx)
{
int dirx = 0;
int diry = 0;
if( Gdx.input.isKeyPressed(Keys.LEFT))
dirx = -1;
if ( Gdx.input.isKeyPressed(Keys.RIGHT))
dirx = 1;
if ( Gdx.input.isKeyPressed(Keys.UP))
diry = 1;
if (Gdx.input.isKeyPressed(Keys.DOWN))
diry = -1;
s.setX( s.getX() + (dirx* spx * d));
s.setY( s.getY() + (diry* spx * d));
}
public boolean checkCollision(Rectangle a, Rectangle b)
{
if (a.overlaps(b))
return true;
else
return false;
}
public void moveShip()
{
shipsprite.setX(posx);
shipsprite.setY(posy);
}
public void render()
{
delta = Gdx.graphics.getDeltaTime(); //get delta
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
moveShip(shipsprite, delta, speed);
overlapflag = checkCollision( shipsprite.getBoundingRectangle(), npcsprite.getBoundingRectangle());
batch.begin();
batch.draw(backtexture, 0, 0);
shipsprite.draw(batch);
if ( !overlapflag)
npcsprite.draw(batch);
batch.end();
}
}
And this is my LaunchMyGame.java code:
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
public class LaunchMyGame
{
public static void main (String[] args)
{
GDXGame myProgram = new GDXGame();
LwjglApplication launcher = new LwjglApplication(myProgram,"Window Title",1600,1200);
}
}
I have no idea how to fix this issue
|
|
|
Re: Exception in thread "main" java.lang.Error: Unresolved compilation problem [message #1845077 is a reply to message #1845074] |
Wed, 06 October 2021 05:09 |
David M. Karr Messages: 802 Registered: July 2009 |
Senior Member |
|
|
In your "main" method, you are calling "new GDXGame()". That is attempting to call a constructor for the "GDXGame" class that doesn't take any parameters, what is often called a "no-args" constructor. In your class definition, you are defining one constructor for GDXGame, but one which takes two parameters (w and h). You haven't defined a "no-args" constructor. That is what the compile error refers to.
If you hadn't defined ANY constructors, your class would get a "default" constructor, which by definition is the "no-args" constructor, which would do effectively nothing. As you did define a constructor that is NOT the "no-args" constructor, it will not define a "default" constructor. If you need a "no-args" constructor, you need to define it.
|
|
|
Powered by
FUDForum. Page generated in 0.02761 seconds