making a smooth jump in libGDX - java

i want to create a jump of my player that is smooth it goes up then down
its a 2D side scroller like Mario
iv'e tried wait and make jumps slow by using a lot of steps and i cant figure it out
player control class:
package com.mygdx.game;
import java.lang.Thread.State;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
#SuppressWarnings("unused")
public class PlayerController {
static int speed = 1;
public static int jump = 0;
static void keyInput() {
jump++;
if (Gdx.input.isKeyPressed(Keys.A)) {
main.playerX += speed;
main.backgroundSpriteX += speed;
}
if (Gdx.input.isKeyPressed(Keys.D)) {
main.playerX -= speed;
main.backgroundSpriteX -= speed;
}
if (Gdx.input.isKeyPressed(Keys.W)) {
//this is where i want to be able to jump
}
}
static void Controller() {
main.player = new Sprite(main.texture);
main.playerX = (main.canvisWidth * 0);
main.playerY = (main.canvisHeight * 0); //can be 0
}
}
main class:
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.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class main implements ApplicationListener {
public static final int backgroundSpriteY = 0;
public static final int backgroundSprite2Y = 0;
public static int canvisWidth = 800;
public static int canvisHeight = 480;
public static int backgroundSpriteX = 0;
public static Texture texture;
public static int backgroundSprite2X = -canvisWidth;
public static Sprite player;
public static int playerX;
public static int playerY;
static SpriteBatch spriteBatch;
static int Jumpframes = 0;
private double playerSize = .4;
public void create() {
WorldObjects.shapeRender.setAutoShapeType(true);
spriteBatch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("imageedit_3_3813241913.png"));
PlayerController.Controller();
WorldSetup.start();
player.setSize((float) (player.getWidth() * playerSize), (float) (player.getHeight() * playerSize));
}
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
PlayerController.keyInput();
WorldController.Scroll();
spriteBatch.begin();
spriteBatch.draw(WorldSetup.backgroundTexture, backgroundSpriteX, backgroundSpriteY);
spriteBatch.draw(WorldSetup.backgroundTexture2, backgroundSprite2X, backgroundSprite2Y);
spriteBatch.draw(texture, playerX, playerY, player.getWidth(), player.getHeight());
spriteBatch.end();
WorldSetup.WorldRender();
//Jumpframes++;
}
public void resize(int width, int height) {
}
public void pause() {
}
public void resume() {
}
public void dispose() {
}
}
i want to have a slow jump like a Mario sort of jump and i cant seem to create a slow/smooth jump
I’d like 20 frame jump going up 30 pixels starting fast and slowing down

I think what you need is a 2D Physics Engine for Games, The the popular one is Box2D
lots of tutorials on this, like Brent Aureli's works, to jump your character you just apply force on it like
player.b2body.applyForceToCenter(0, 80f, true);
hope this helps

You can maybe get away from recording the start jump position like this... instead of using the buttons to directly alter their position, use the buttons to alter their velocity then use their current position plus xvelocity and yvelocity to calculate next position. when they press jump they get a increased y and every frame you reduce that until they land on a platform then set it back to 0.

Related

How to keep UI elements from resizing?

I have a UI bar and a game board rectangle bellow it.
I want the UI elements to maintain their original size, regardless of screen resolution. This can be achieved via a ScreenWievport (as per suggested here), but that completely messes up my game board.
What I have right now works, but the UI elements resize with the window.
My code:
MyGame.java (entry point)
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.viewport.FitViewport;
public final class MyGame extends Game {
private int screenWidth;
private int screenHeight;
private ShapeRenderer renderer;
public MyGame(int screenWidth, int screenHeight) {
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
}
#Override
public void create() {
renderer = new ShapeRenderer();
this.setScreen(
new MyGameScreen(
// new FitViewport(screenWidth, screenHeight)
new FitViewport(640, 480),
renderer
)
);
}
}
MyGameScreen.java (main application Screen)
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Event;
import com.badlogic.gdx.scenes.scene2d.EventListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup;
import com.badlogic.gdx.utils.viewport.Viewport;
public final class MyGameScreen extends ScreenAdapter {
/** Starting sprouts slider minimum value. */
private static final int SLIDER_MIN = 2;
/** Starting sprouts slider maximum value. */
private static final int SLIDER_MAX = 7;
/** Starting sprouts slider step value. */
private static final int SLIDER_STEP = 1;
/** The spacing between the elements in a toolbar. */
private static final int TOOLBAR_CELL_SPACING = 10;
/** The padding of the toolbar row. */
private static final int TOOLBAR_PADDING = 10;
/** The spacing between the rows of the root layout. */
private static final float ROOT_ROW_SPACING = 5;
/** The padding of the root layout. */
private static final float ROOT_PADDING = 10;
private final Viewport viewport;
private final ShapeRenderer renderer;
private Stage stage;
public MyGameScreen(Viewport viewport, ShapeRenderer renderer) {
this.viewport = viewport;
this.renderer = renderer;
}
#Override
public void show() {
stage = new Stage(viewport);
Skin skin =
new Skin(
Gdx.files.internal("default/skin/uiskin.json")
);
final Label sliderLabel = new Label(""+SLIDER_MIN, skin);
sliderLabel.setColor(Color.BLACK);
final Slider slider =
new Slider(
SLIDER_MIN,
SLIDER_MAX,
SLIDER_STEP,
false,
skin
);
slider.addListener(new EventListener() {
#Override
public boolean handle(final Event event) {
sliderLabel.setText("" + (int) slider.getValue());
return true;
}
});
slider.setValue(SLIDER_MIN);
TextButton resetButton =
new TextButton(
"New game",
skin
);
Table toolbar = new Table().pad(TOOLBAR_PADDING);
toolbar.left().add(resetButton).space(TOOLBAR_CELL_SPACING);
toolbar.add(sliderLabel).space(TOOLBAR_CELL_SPACING);
toolbar.add(slider);
toolbar.setHeight(resetButton.getPrefHeight() + 2 * TOOLBAR_PADDING);
Rectangle gameBounds =
new Rectangle(
ROOT_PADDING,
ROOT_PADDING,
stage.getViewport().getWorldWidth() - 2 * ROOT_PADDING,
stage.getViewport().getWorldHeight()
- 2 * ROOT_PADDING
- toolbar.getHeight()
- ROOT_ROW_SPACING
);
Actor gameBoard = new MyGameBoard(new MyGameState(gameBounds), renderer);
gameBoard.setBounds(
gameBounds.getX(),
gameBounds.getY(),
gameBounds.getWidth(),
gameBounds.getHeight()
);
VerticalGroup rootLayout =
new VerticalGroup()
.center()
.space(ROOT_ROW_SPACING)
.pad(ROOT_PADDING);
rootLayout.setFillParent(true);
rootLayout.grow().addActor(toolbar);
rootLayout.addActor(gameBoard);
stage.addActor(rootLayout);
Gdx.input.setInputProcessor(stage);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.getViewport().apply();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
}
MyGameBoard.java
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.scenes.scene2d.Actor;
public final class MyGameBoard extends Actor {
private static final float BORDER_THICKNESS = 5f;
private static final Color DEFAULT_BACKGROUND_COLOR =
new Color(204 / 255f, 229 / 255f, 1, 1);
private final MyGameState gameState;
private final ShapeRenderer renderer;
public MyGameBoard(
final MyGameState gameState,
final ShapeRenderer renderer
) {
this.gameState = gameState;
this.renderer = renderer;
}
#Override
public void draw(final Batch batch, final float parentAlpha) {
batch.end();
Gdx.gl.glLineWidth(BORDER_THICKNESS);
renderer.setProjectionMatrix(batch.getProjectionMatrix());
renderer.begin(ShapeType.Line);
renderer.setColor(DEFAULT_BACKGROUND_COLOR);
renderer.rect(getX(), getY(), getWidth(), getHeight());
// draw my game board state (a single point in the middle of the board)
renderer.circle(
gameState.gameBounds.width / 2,
gameState.gameBounds.height / 2,
BORDER_THICKNESS
);
renderer.end();
batch.begin();
}
}
MyGameState.java (sample game board state)
import com.badlogic.gdx.math.Rectangle;
public class MyGameState {
public Rectangle gameBounds;
public MyGameState(Rectangle gameBounds) {
this.gameBounds = gameBounds;
}
}
I've looked at the following similar questions:
libgdx resolution/density scaling assets
Libgdx have two different views drawn with own coordinate systems?
Scale libgdx UI for mobile?
but I simply wasn't able to piece the information provided in each of those questions together into a working solution for my problem.

Struggling to structure libgdx project

I know this question might get taken down but I'm going to ask it anyway.
I've been trying to make a draughts game in Java using Libgdx; I've been using Stage2D as well.
I don't know how to structure the project as I plan on having separate classes for the board, game, players, pieces and GUI. Also, I have written some code to draw a board, however, it doesn't seem to work with Stage2D.
I still want to figure this out myself, as I want to learn from this project. So, please do not give too much away.
However, I don't know how to draw the board or start this project.
All the resources and tutorials I have found online have been very unclear. I don't really know what to do.
package com.mygdx.game;
public enum Color {
BLACK, WHITE
}
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;
public class GUI {
float r_brown = (float) 163 / 255;
float g_brown = (float) 102 / 255;
float b_brown = (float) 46 / 255;
float r_cream = (float) 240 / 255;
float g_cream = (float) 187 / 255;
float b_cream = (float) 108 / 255;
private ShapeRenderer shapeRenderer;
private OrthographicCamera camera;
public GUI(ShapeRenderer shapeRenderer, OrthographicCamera camera) {
this.shapeRenderer = shapeRenderer;
this.camera = camera;
}
public void drawGUI() {
}
}
package com.mygdx.game;
public enum Type {
SOLDIER, KING
}
package com.mygdx.game;
public class Player {
private Color color;
public Player() {
}
}
package com.mygdx.game;
public class King extends Piece {
private Location location;
private Type type = Type.KING;
private Player player;
public King(Player player, Location location) {
super(player, location);
}
#Override
public void draw() {
// TODO Auto-generated method stub
}
#Override
public void move() {
// TODO Auto-generated method stub
}
#Override
public void capture() {
// TODO Auto-generated method stub
}
}
package com.mygdx.game;
public class Location {
private int x;
private int y;
public Location(int x, int y) {
this.x = x;
this.y = y;
}
}
package com.mygdx.game;
import com.badlogic.gdx.scenes.scene2d.Actor;
public abstract class Piece extends Actor {
private Location location;
private Type type;
private Player player;
public Piece(Player player, Location location) {
this.player = player;
this.location = location;
}
public abstract void draw();
public abstract void move();
public abstract void capture();
public void setLocation(Location location) {
this.location = location;
}
public Location getLocation() {
return this.location;
}
public void setType(Type type) {
this.type = type;
}
public Type getType() {
return this.type;
}
public void setPlayer(Player player) {
this.player = player;
}
public Player getPlayer() {
return this.player;
}
}
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.actions.MoveByAction;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
public class Soldier extends Actor {
private Location location;
private Type type = Type.SOLDIER;
private Player player;
Sprite sprite = new Sprite(new Texture(Gdx.files.internal("black_solider.png")));
public Soldier(Player player, Location location) {
//super(player, location);
setBounds(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
setTouchable(Touchable.enabled);
addListener(new InputListener() {
#Override
public boolean keyDown(InputEvent event, int keycode) {
if (keycode == Input.Keys.RIGHT) {
MoveByAction mba = new MoveByAction();
mba.setAmount(100f, 0f);
mba.setDuration(5f);
Soldier.this.addAction(mba);
}
return true;
}
});
}
#Override
public void draw(Batch batch, float parentAlpha) {
sprite.draw(batch, parentAlpha);
}
#Override
public void act(float delta) {
super.act(delta);
}
//#Override
public void move() {
// TODO Auto-generated method stub
}
//#Override
public void capture() {
// TODO Auto-generated method stub
}
public void setLocation(Location location) {
this.location = location;
}
public Location getLocation() {
return this.location;
}
public void setType(Type type) {
this.type = type;
}
public Type getType() {
return this.type;
}
public void setPlayer(Player player) {
this.player = player;
}
public Player getPlayer() {
return this.player;
}
}
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
public class MyGdxGame extends ApplicationAdapter {
Stage stage;
private GUI gui;
private ShapeRenderer shapeRenderer;
private OrthographicCamera camera;
float r_brown = (float) 163 / 255;
float g_brown = (float) 102 / 255;
float b_brown = (float) 46 / 255;
float r_cream = (float) 240 / 255;
float g_cream = (float) 187 / 255;
float b_cream = (float) 108 / 255;
#Override
public void create() {
ScreenViewport viewport = new ScreenViewport();
stage = new Stage(viewport);
Gdx.input.setInputProcessor(stage);
shapeRenderer = new ShapeRenderer();
camera = new OrthographicCamera();
gui = new GUI(shapeRenderer, camera);
Soldier piece1 = new Soldier(new Player(), new Location(400, 400));
stage.addActor(piece1);
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if ((i % 2) == 0) {
if ((j % 2) == 0) {
shapeRenderer.setColor(r_brown, g_brown, b_brown, 1);
shapeRenderer.rect(100 * i, 100 * j, 100, 100);
} else {
shapeRenderer.setColor(r_cream, g_cream, b_cream, 1);
shapeRenderer.rect(100 * i, 100 * j, 100, 100);
}
} else {
if ((j % 2) == 0) {
shapeRenderer.setColor(r_cream, g_cream, b_cream, 1);
shapeRenderer.rect(100 * i, 100 * j, 100, 100);
} else {
shapeRenderer.setColor(r_brown, g_brown, b_brown, 1);
shapeRenderer.rect(100 * i, 100 * j, 100, 100);
}
}
}
}
shapeRenderer.end();
camera.update();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
/*
Piece[][] pieces = new Piece[8][8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Piece currentPiece = pieces[i][j];
currentPiece.draw();
}
}
*/
}
#Override
public void dispose() {
shapeRenderer.dispose();
}
}
A few quick and superficial pointers:
You seem to have multiple challenges and problems. Focus on them one at a time if you can, dividing and conquering them one by one.
If you are doing this for the sake of learning and you are very new to programming, be very careful about not having a large scope for the project. Set a very modest goal for what you want to be able to do with the project, and once you have accomplished that, you can add an additional goal. Iteratively continuing. This is very risk-resilient, because you start from a relatively easy point and go from there. The drawback with this is that you may have to do a fair amount of work, because one design early one that was suitable then may not be suitable for later work, and this may require a rewrite and redesign to a smaller or larger degree. Once you get more experienced, planning and seeking to design early on can be very, very helpful, especially if you know that some important aspects are going to be important later and the design you choose early on will impact these - but again, this is once you are more experienced.
Consider what things you would like the game to be able to have, like AI (state-space-search), graphics, saving/loading, the player using a GUI to move around, etc. And then pick the few things that you consider both easy and most nice to have. If you are new, don't require it to be a cohesive whole, you can always take that on later as a more ambitious goal once you have more experience, have learned more, thought more about things, etc. Again, be very, very modest with your goals and scope, and get some experience and just learn.

How to use a timer to repaint some swing components

INTRODUCTION
Hi guys, I was learning how to make a little game where you are a racquet (rectangle) and all the asteroids (enemies) are falling down the screen and you have to avoid them
PROBLEM
When the game starts the enemies (asteroids) are paint on the screen in a very high speed, and they occupy all the size of the screen so there is no way to avoid them (try the game), I just want that between painting an enemy and the next one there is a mininum time to delay (for example 0,5 seconds).
But I just don't know how to do that, I tried to use Thread.sleep() and TimeUnit but they just make the game slower.
Surfing on stackoverflow I've find out that I may try to use Swing timers, I've read some stuff on the web but I want to know how can I use swing timers in my code (if they can solve my problem).
Here it is the code:
The main class:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
#SuppressWarnings("serial")
public class Game extends JPanel {
Racquet racquet = new Racquet(this);
Enemy Enemy = new Enemy(this);
static ArrayList<Enemy> enemyList = new ArrayList<Enemy>();
public Game() {
addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
racquet.keyReleased(e);
}
#Override
public void keyPressed(KeyEvent e) {
racquet.keyPressed(e);
}
});
setFocusable(true);
}
/** TO SET THE RANDOM POSITION ON WHERE THE ENEMIES HAVE TO APPEAR ON THE SCREEN **/
public int random(int x, int y, ArrayList<Enemy> pa){
int r = 0;
for(int i = 0; i<pa.size(); i++){
Random rand = new Random();
r = rand.nextInt(x+y)-1;
return r;
}
return r;
}
/** letting the enemies move on the screen **/
private void move() {
for(int i = 0; i < enemyList.size(); i++){
enemyList.get(i).move();
}
racquet.move();
}
/** Painting on the screen enemies and the racquet **/
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for(int i = 0; i < enemyList.size(); i++){
enemyList.get(i).paint(g2d);
}
racquet.paint(g2d);
}
public void gameOver() {
JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION);
System.exit(ABORT);
}
public void createEnemy(){
enemyList.add(new Enemy(this));
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Asteroids");
Game game = new Game();
frame.add(game);
frame.setSize(300, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.random(200, 300, enemyList);
while (true) {
game.createEnemy();
game.move();
game.repaint();
Thread.sleep(5);
}
}
}
The racquet class:
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
public class Racquet {
private static final int Y = 330;
private static final int WIDTH = 30;
private static final int HEIGHT = 6 ;
int x = 0;
int xa = 0;
private Game game;
public Racquet(Game game) {
this.game = game;
}
/** letting the racquet moves on the screen **/
public void move() {
if (x + xa > 0 && x + xa < game.getWidth() - WIDTH)
x = x + xa;
}
/** Creating the rectangle racquet **/
public void paint(Graphics2D g) {
g.fillRect(x, Y, WIDTH, HEIGHT);
}
/** // Setting xa everytime to 0, if we don't do this it just takes a single pression to go to a direction until we press the other key **/
public void keyReleased(KeyEvent e) {
xa = 0;
}
/** Choosing the direction **/
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)
xa = -1;
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
xa = 1;
}
public Rectangle getBounds() {
return new Rectangle(x, Y, WIDTH, HEIGHT);
}
public int getTopY() {
return Y;
}
}
And the enemy class:
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.*;
public class Enemy {
private Game game;
int x = 0;
int y = 0;
int xa = 1;
int ya = 1;
/** Generating a random position where the enemies have to appear **/
public Enemy(Game game){
this.game = game;
x = game.random(0, 320, game.enemyList);
}
/** Paint the enemies **/
public void paint(Graphics2D g) {
g.fillRect(x, y, 20, 20);
}
/** move the enemies and detect collisions **/
public void move(){
y += ya;
if(collision()){
game.gameOver();
}
}
/** returns true if the enemy rectangle touch the racquet **/
public boolean collision(){
return game.racquet.getBounds().intersects(getBounds());
}
public Rectangle getBounds() {
return new Rectangle(x, y, 20, 20);
}
}
Suggestions:
Your game "tick" time is 5 mSecs, a not quite reasonable time. I suggest that
You get rid of the "magic" number, using a constant instead,
and make the tic a little bigger, say 12 to 15 mSec.
Also better to use a Swing Timer or to at least make sure that you do your while loop in a defined background thread.
Most importantly, you're creating a new enemy with each tick of your game loop, and that's too fast. Instead:
Don't create a new Enemy with each tick,
Instead save the time that the last enemy was created in a field,
Check the delta-time, the current system time - lastEnemyCreationTime, inside of your game-loop,
Only create a new enemy if the delta-time is greater than a reasonable value, either a constant value or a field (not a magic number).
On creation of the new enemy, reset the lastEnemyCreationTime to the current system time.
Unrelated recs:
Override the JPanel's paintComponent, not the paint method as this will give you double buffering by default which can lead to smoother perceived graphics.
Favor using Key Bindings over a KeyListener as this will help to easily eliminate focus issues inherent with use of KeyListeners.

Java libGdx: FreeTypeFontGenerator is not working (Black screen)

I added in a custom font Aller_light.ttf and when I try to run the program with the font drawn with no errors the screen is black, so I'm not sure if its rendering somewhere else off screen or its not rendering at all.
If you could give your 2 cents that would be of great value to me.
EDIT
Now it's saying i have a nullPointerExeption
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.mygdx.finis.states.MenuState.draw(MenuState.java:43)
at com.mygdx.finis.states.GameStateManager.draw(GameStateManager.java:31)
at com.mygdx.finis.Main.render(Main.java:36)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
This is what should be happening:
MenuState Class
package com.mygdx.finis.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.mygdx.finis.Assets;
import com.mygdx.finis.Main;
public class MenuState extends GameState{
private SpriteBatch sb;
private BitmapFont font;
private int currentItem;
private String[] menuItems;
protected MenuState(GameStateManager gsm) {
super(gsm);
}
public void init() {
sb = new SpriteBatch();
FreeTypeFontGenerator gen = new FreeTypeFontGenerator(
Gdx.files.internal(null));
font = gen.generateFont(20);
menuItems = new String[]{
"Play"
};
}
public void update(float dt) {
handleInput();
}
public void draw() {
sb.setProjectionMatrix(Main.cam.combined);//*** This is where the null pointer starts *********
sb.begin();
font.draw(sb, "Quack", Main.WIDTH / 2, Main.HEIGHT / 2);//*** This does not work *******************
sb.end();
}
public void handleInput() {
}
public void dispose() {
}
}
Main Class
package com.mygdx.finis;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.mygdx.finis.states.GameStateManager;
public class Main implements ApplicationListener{
public static int WIDTH;
public static int HEIGHT;
public static OrthographicCamera cam;
private GameStateManager gsm;
public void create(){
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
cam = new OrthographicCamera(WIDTH, HEIGHT);
cam.translate(WIDTH / 2, HEIGHT / 2);
cam.update();
gsm = new GameStateManager();
}
public void render(){
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
gsm.draw();//****** THIS IS WHERE THE ISSUE ENDS ******
}
public void resize(int width, int height){}
public void pause(){}
public void resume(){}
public void dispose(){}
}
It seems most likely that your SpriteBatch sb is null. Are you absolutely sure init() is being called?
The only other alternative is that Main.cam is null.
It has to be one of those.
Incidentally, this looks odd to me...
Gdx.files.internal(null));
Why are you doing that?

LibGdx-Blank Screen that can't be closed

I want to make a simple flappy bird clone while I'm in vacation(The firt time I try game programming since high school) so after looking at ligdx wiki and sort of adapting the code to fit my needs I had something like this:
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
public class FlappyBall implements ApplicationListener {
Texture ballImage;
Texture chaoImage;
Texture canoImage;
OrthographicCamera camera;
SpriteBatch batch;
Rectangle ball;
int score = 0;
final float gravity = 9.0f;
#Override
public void create() {
ballImage = new Texture(Gdx.files.internal("crystalball_small.png"));
//chaoImage = new Texture(Gdx.files.internal("Flappy-Ground.png"));
camera = new OrthographicCamera(800,480);
camera.setToOrtho(false, 800, 480);
batch = new SpriteBatch();
ball = new Rectangle();
ball.x = 128;
ball.y = 20;
ball.width = 64;
ball.height = 64;
}
#Override
public void dispose() {
ballImage.dispose();
//chaoImage.dispose();
}
#Override
public void render() {
// cleans the screen
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// rendenring ball
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(ballImage, ball.x, ball.y);
batch.end();
// jump(not tested)
if (Gdx.input.isTouched()) {
ball.y += 10;
}
// apply gravity(not tested)
while (ball.y < 480)
ball.y -= gravity * Gdx.graphics.getDeltaTime();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
however this code ends up showing a blank screen that can't even be closed when I press the X button(in order to close it I need to terminate its process in eclipse or use the xkill command :/)
Ps.:That's my Main.java file:
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class Main {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "Flappy Ball";
cfg.useGL20 = false;
cfg.width = 800;
cfg.height = 480;
new LwjglApplication(new FlappyBall(), cfg);
}
}
Ps².:The compiler shows no errors when this code is being executed
Thanks :D
Your render method will probably never finish it's first frame. The while loop will keep going until ball.y becomes greater than or equal to 480. This will never happen because you are subtracting a positive number.
while (ball.y < 480)
ball.y -= gravity * Gdx.graphics.getDeltaTime();
I would rather change it in something like this:
float change = gravity * Gdx.graphics.getDeltaTime();
if(ball.y - change >= 0)
ball.y -= change;

Categories

Resources