When lunching the application with DesktopLuncher i experience UnsatisfiedLinkError on root class (Main) in core module inside the project.
public class Main extends Game {
#Override
public void create() {
this.cameraBuilder = ...
setScreen(new LandingMenu(this));
}
#Override
public void render() {
super.render();
}
#Override
public void dispose() {
batch.dispose();
}
public World getWorld() {
return world;
}
public SpriteBatch getBatch() {
return batch;
}
public CameraBuilder<OrthographicCamera> getCameraBuilder() {
return cameraBuilder;
}
private final SpriteBatch batch = new SpriteBatch();;
private CameraBuilder<OrthographicCamera> cameraBuilder;
private final World world = new World(new Vector2(0, -9.8f), true);
}
any answer will be appreciated.
The exception is a cause and issue lives on create method on ApplicationListener Called when the Application is first created, on the other hand SpriteBatch is a Batch which is used for drawing Sprites, more specifically, it deals with drawing a bunch of textures on Quads in OpenGL thus is must be initialized inside of create method and not in the root class level.
#Override
public void create() {
batch = new SpriteBatch();
setScreen(new LandingMenu(this));
}
...
private SpriteBatch batch;
Find more in here.
Related
I have a world that is the GameScreen (20ish objects) which lays all objects as intended. However, when I get GameOver I want to be a blank canvas with just the background and some new objects(a couple objects), but all the existing objects from GameScreen carry over and I cant figure out how to stop it or delete them on the GameOver screen
public class GameScreen extends World
{
public GameScreen()
{
super(600, 400, 1);
prepare();
}
private void prepare()
{
addObjects.......
}
}
public class GameLost extends GameScreen
{
public GameLost()
{
removeObjects(GameScreen);
prepare();
}
private void prepare()
{
addObjects...
}
I'm pretty sure you don't want GameLost extends GameScreen.
Do
class GameLost {
private Background bg;
public void paint() {
paint(bg);
}
}
class GameScreen {
private Background bg;
private List<GameObjects>...
public void paint() {
paint(bg);
gameObjects.forEach(go -> paint(go));
}
}
which makes it easy to move over the background from game screen to game over screen, without the game objects.
I need help calling different classes.
I want to create a class for, a menu, the game, an option menu, credits etc.
I just need to know how to call a different class
//Ok i have edited my post, hope i provided enough information.
game class
//Add Screens
private MenuScreen menuScreen;
public final static int MENU=0;
public void changeScreens(int screen){
switch(screen){
case MENU:
if(menuScreen==null){
menuScreen=new MenuScreen(this);
}
}
}
//Render (When you click on the button)
if(drawplay) {
menuScreen=new MenuScreen(this);
setScreen( menuScreen);
//MenuScreen Class
ublic class MenuScreen implements Screen {
private rugby parent;
public MenuScreen(rugby rugby){
parent = rugby;
}
#Override
public void render(float delta) {
Gdx.app.log( "Play","Play" );
}
I am trying to get my main class to setscreen to my gamescreen class.when I try to run the desktop application the only thing that happens is that a window with a black background opens and stays up for half a second and then closes down and I have no idea why it doesn't work. I'm using Netbeans if that has anything to do with it.
Here is my main class:
public class MyGdxGame extends Game {
public SpriteBatch batch;
#Override
public void create () {
batch = new SpriteBatch();
this.setScreen(new GameScreen(this));
}
#Override
public void render () {
super.render();
}
}
and here is my screen class:
public class GameScreen implements Screen{
Texture texture;
private MyGdxGame game;
public GameScreen(MyGdxGame game) {
this.game = game;
texture = new Texture("badlogic.jpg");
}
#Override
public void show() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
#Override
public void render(float f) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.begin();
game.batch.draw(texture, 0, 0);
game.batch.end();
}
//I havn't touched the other methods so I didn't paste them.
I haven't touched the desktop class.
I get this error message:
Exception in thread "LWJGL Application" java.lang.UnsupportedOperationException: Not supported yet.
at com.mygdx.game.GameScreen.show(GameScreen.java:27)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:14)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
You are basically throwing an exception in method show(), which is called right after setting the screen to MyGdxGame class.
#Override
public void show() {
throw new UnsupportedOperationException("Not supported yet.");
}
Never used NetBeans, but it looks like it overrides show() with an exception. As the comment in the code suggest, change the body of generated methods in Tools | Templates to avoid this kind of errors.
I want to develop a simple interactive game (like arcanoid). I already have implemented a menu and different views, and now I need to develop the actually game (draw flying ball, some movable platform) and I don't know how to do this. I need something like canvas where I can draw my graphic each frame.
I have tryed to implement this with Canvas and Timer. But it doesn't want update graphic itself, but only when user clicks on screen or similar. Also I saw com.google.gwt.canvas.client.Canvas, but I cannot understand how to use it in Vaadin application.
So my question is next: is it possible to draw some graphic each frame with high framerate in any way? If possible, how can I do this?
P.S. I use the Vaadin 7.3.3.
ADDED LATER:
Here is a link to my educational project with implementation below.
I'll be glad if it helps someone.
ORIGINAL ANSWER:
Well... I found the solution myself. First of all, I have created my own widget - "client side" component (according to this article).
Client side part:
public class GWTMyCanvasWidget extends Composite {
public static final String CLASSNAME = "mycomponent";
private static final int FRAMERATE = 30;
public GWTMyCanvasWidget() {
canvas = Canvas.createIfSupported();
initWidget(canvas);
setStyleName(CLASSNAME);
}
Connector:
#Connect(MyCanvas.class)
public class MyCanvasConnector extends AbstractComponentConnector {
#Override
public Widget getWidget() {
return (GWTMyCanvasWidget) super.getWidget();
}
#Override
protected Widget createWidget() {
return GWT.create(GWTMyCanvasWidget.class);
}
}
Server side part:
public class MyCanvas extends AbstractComponent {
#Override
public MyCanvasState getState() {
return (MyCanvasState) super.getState();
}
}
Then I just add MyCanvas component on my View:
private void createCanvas() {
MyCanvas canvas = new MyCanvas();
addComponent(canvas);
canvas.setSizeFull();
}
And now I can draw anything on Canvas (on client side in GWTMyCanvasWidget) with great performance =). Example:
public class GWTMyCanvasWidget extends Composite {
public static final String CLASSNAME = "mycomponent";
private static final int FRAMERATE = 30;
private Canvas canvas;
private Platform platform;
private int textX;
public GWTMyCanvasWidget() {
canvas = Canvas.createIfSupported();
canvas.addMouseMoveHandler(new MouseMoveHandler() {
#Override
public void onMouseMove(MouseMoveEvent event) {
if (platform != null) {
platform.setCenterX(event.getX());
}
}
});
initWidget(canvas);
Window.addResizeHandler(new ResizeHandler() {
#Override
public void onResize(ResizeEvent resizeEvent) {
resizeCanvas(resizeEvent.getWidth(), resizeEvent.getHeight());
}
});
initGameTimer();
resizeCanvas(Window.getClientWidth(), Window.getClientHeight());
setStyleName(CLASSNAME);
platform = createPlatform();
}
private void resizeCanvas(int width, int height) {
canvas.setWidth(width + "px");
canvas.setCoordinateSpaceWidth(width);
canvas.setHeight(height + "px");
canvas.setCoordinateSpaceHeight(height);
}
private void initGameTimer() {
Timer timer = new Timer() {
#Override
public void run() {
drawCanvas();
}
};
timer.scheduleRepeating(1000 / FRAMERATE);
}
private void drawCanvas() {
canvas.getContext2d().clearRect(0, 0, canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight());
drawPlatform();
}
private Platform createPlatform() {
Platform platform = new Platform();
platform.setY(Window.getClientHeight());
return platform;
}
private void drawPlatform() {
canvas.getContext2d().fillRect(platform.getCenterX() - platform.getWidth() / 2, platform.getY() - 100, platform.getWidth(), platform.getHeight());
}
}
I'm experiencing a problem using LibGDX.
I have a Screen and in its render method I have:
public class LoadingScreen implements Screen{
private OrthographicCamera guiCam;
private TankChallenge tankChallenge;
private SpriteBatch batcher;
private GL10 OpenGL;
public LoadingScreen(TankChallenge tankChallenge) {
this.tankChallenge=tankChallenge;
Gdx.graphics.setTitle("RobotChallange Beta - Loading");
float AR = Gdx.graphics.getHeight()/Gdx.graphics.getWidth();
guiCam = new OrthographicCamera(10,10 * AR);
guiCam.position.set(10f/2,AR*10f/2,0);
batcher = new SpriteBatch();
OpenGL = Gdx.graphics.getGL10();
//loadAssets();
}
#Override
public void render(float delta) {
OpenGL.glClearColor(1, 0.5f, 1, 1);
OpenGL.glClear(GL10.GL_COLOR_BUFFER_BIT);
System.out.print("e");
guiCam.update();
Sprite sp = new Sprite();
sp.setColor(1f,1f,0, 1);
sp.setSize(100,100);
sp.setOrigin(20, 0);
batcher.setProjectionMatrix(guiCam.combined);
batcher.begin();
batcher.disableBlending();
sp.draw(batcher);
batcher.end();
}
I call this by setting it in the implementes ApplicationListener class. I know it is arriving the LoadingScreen because Title is actually set to "RobotChallenge Beta - Loading".
I found a solution for both problems. I added to the Game extending class this:
public void render() {
super.render();
}
Thanks to this the problem is solved and I don't need to override the SetScreen method. Thanks!
Immediately after setting this as your screen, you need to call super.render() in your Game extending class. For instance, I use the following method in my main Game extending class for setting the screen:
#Override
public void setScreen(Screen screen)
{
super.setScreen(screen);
super.render();
}
This seems to kick off render() being called.
Just remove #Override render() in your Game extending class, that fixes the problem. Overriding setScreen method causes flickering you mentioned.