Struggling to structure libgdx project - java

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.

Related

making a smooth jump in libGDX

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.

Player doesn't move when attempting to draw from a Player class

So I'm currently in the process of refactoring my code in my game for the player and started a player class:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Player
{
protected String name;
protected float health, maxHealth;
protected Texture texture;
protected int xPos, yPos;
public Player(String name, float maxHealth, Texture texture) {
this(name, maxHealth, texture, 0, 0);
}
public Player(String name, float maxHealth, Texture texture, int xPos, int yPos) {
this.name = name;
this.health = this.maxHealth = maxHealth;
this.texture = texture;
this.xPos = xPos;
this.yPos = yPos;
}
public Texture getTexture() {
return this.texture;
}
public int getX() {
return xPos;
}
public int getY() {
return yPos;
}
public String getName() {
return name;
}
public void draw(SpriteBatch spriteBatch) {
spriteBatch.begin();
spriteBatch.draw(texture, xPos, yPos, 0.5f, 0.5f, 0, 0,
texture.getWidth(), texture.getHeight(), false, false);
spriteBatch.end();
}
public void update(float delta) {
processMovement(delta);
}
public void processMovement(float delta) {
if(Gdx.input.isKeyPressed(Input.Keys.A)) {
xPos -= 50 * delta;
}
if(Gdx.input.isKeyPressed(Input.Keys.D)) {
xPos += 50 * delta;
}
}
}
I'm using an orthographic camera, I haven't added any terrain yet as I'm going to do that next, however I want the player to always stay in the center but have the player move around the terrain when I draw it.
The code I have for creating the camera and drawing my player is as follows:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class GameState implements Screen
{
private PixelGame parent;
private OrthographicCamera camera;
private Player player;
private Texture playerTexture;
private SpriteBatch spriteBatch;
public GameState(PixelGame parent) {
this.parent = parent;
this.playerTexture = new Texture(Gdx.files.internal("player/sprite.png"));
this.player = new Player("Me", 20, playerTexture);
this.spriteBatch = new SpriteBatch();
}
#Override
public void render(float delta) {
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
player.draw(spriteBatch);
player.update(delta);
}
#Override
public void show() {
}
#Override
public void dispose() {
}
#Override
public void resize(int width, int height) {
float ratio = (float)width / (float)height;
camera = new OrthographicCamera(2f * ratio, 2f);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
}
The player sprite doesn't seem to move, but when i change the values to something >75 the player sprite sprints across the screen like no-ones business.
xPos and yPos in your class are ints. I would say that's the problem. Your processMovement() method is called like 100 times per second and 50 * delta is most likely smaller than 1 so it's rounded to 0 because value has to be stored in int variable. Try changing xPos and yPos to floats.
And if that doesn't help (can't be sure without trying out the code) do some debugging. Put break points. See if processMovement() is called at all and if it is then what value variable delta has. How calculation goes.

what reason is there for non-smooth 2d sprite movement in this example

Just in process of making a space invaders style game for android.
I want the player to be able to touch the screen anywhere to right or left of character (at bottom of screen) to move him that direction.
The code compiles without error and the playe does move, but
A) he's moving much slower than I expected
B) The movement is 'jittery' even though I have multiplied the movement speed by deltatime in a few different ways.
Please could someone be kind enough to take a look at my code to say where I have gone wrong? :-
package com.moneylife.stashinvaders;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class StashInvaders extends ApplicationAdapter {
GameManager gameManager;
#Override
public void create () {
gameManager = new GameManager();
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameManager.update();
gameManager.draw();
}
#Override
public void dispose () {
gameManager.spriteBatch.dispose();
}
}
GameManager class:
package com.moneylife.stashinvaders;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
/**
* Created by Dave on 12/08/2016.
*/
public class GameManager {
SpriteBatch spriteBatch;
Player player1;
public GameManager(){
spriteBatch = new SpriteBatch();
player1 = new Player();
}
public void update(){
player1.update();
}
public void draw(){
spriteBatch.begin();
player1.draw(spriteBatch);
spriteBatch.end();
}
}
Player class:
package com.moneylife.stashinvaders;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.math.Vector2;
/**
* Created by Dave on 12/08/2016.
*/
public class Player {
Vector2 position;
Texture texture;
int speed = 50;
float deltaTime;
public Player(){
Gdx.input.setInputProcessor(new GestureDetector(new MyGestureDetector()));
texture = new Texture("bazookaman.png");
position = new Vector2(Gdx.graphics.getBackBufferWidth() / 2 - texture.getWidth() / 2, 0);
}
public void update(){
deltaTime = Gdx.graphics.getDeltaTime();
}
public void draw(SpriteBatch spriteBatch){
spriteBatch.draw(texture, position.x, position.y);
}
public class MyGestureDetector implements GestureDetector.GestureListener {
#Override
public boolean touchDown(float x, float y, int pointer, int button) {
return false;
}
#Override
public boolean tap(float x, float y, int count, int button) {
return false;
}
#Override
public boolean longPress(float x, float y) {
return false;
}
#Override
public boolean fling(float velocityX, float velocityY, int button) {
return false;
}
#Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
if (x > position.x){
position.x += speed * deltaTime;
}
if (x < position.x){
position.x -= speed * deltaTime;
}
return false;
}
#Override
public boolean panStop(float x, float y, int pointer, int button) {
return false;
}
#Override
public boolean zoom(float initialDistance, float distance) {
return false;
}
#Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
return false;
}
#Override
public void pinchStop() {
}
}
}
Look closely at your logic.
if (x > position.x){
position.x += speed * deltaTime; //if close to touch point, position.x is now bigger than x
}
if (x < position.x){ //if we just moved right past the touch point, undo it
position.x -= speed * deltaTime;
}
Furthermore, this pan method will only be called on frames where the finger position was polled and was found to have moved. The finger position is not polled 60 times per second like your game is probably running, and movement will not always have occurred.
Instead, you should use the panning to modify a target position for your character. In the update() method you can always be moving towards that target position with some speed. It's up to you whether you should be using x or deltaX in the pan method to change your target X. Different types of gameplay.

Make a sprite jump

I want to make a sprite\bitmap jump using only android (no game engines). I wasn't able to find tutorials on how to do so in android using only canvas and views, but I did find a tutorial for xna(http://www.xnadevelopment.com/tutorials/thewizardjumping/thewizardjumping.shtml), and I tried to recreate it with the tools android offers. I was able to make the character move left and right using the tutorial code but making it jump just won't work.
this is my sprite class:
package com.example.spiceup;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.KeyEvent;
import android.widget.Toast;
public class Sprite {
enum State
{
Walking, Standing, Jumping
}
State mCurrentState = State.Standing;
Point mDirection;
int mSpeed = 0;
Context cc;
int mPreviousKeyboardState;
private String spriteName;
Bitmap sprite;
private int rows;
private int rows2;
int START_POSITION_X = 125;
int START_POSITION_Y = 245;
int SPRITE_SPEED = 6;
int MOVE_UP = -1;
int MOVE_DOWN = 1;
int MOVE_LEFT = -1;
int MOVE_RIGHT = 1;
Point mStartingPosition;
int aCurrentKeyboardState;
private float mScale = 1.0f;
Point Position;
public Sprite(String name,Bitmap sprite) {
this.sprite=sprite;
this.spriteName=name;
Position=new Point(150,150);
mStartingPosition=new Point(150,150);
mDirection=new Point(0,0);
}
public void Update()
{
UpdateMovement(aCurrentKeyboardState);
UpdateJump(aCurrentKeyboardState);
}
public void setkeyboard(int keyboard){
aCurrentKeyboardState = keyboard;
}
public void setLastKeyboard(int keyboard){
mPreviousKeyboardState = keyboard;
}
private void UpdateMovement(int aCurrentKeyboardState)
{
if (mCurrentState == State.Walking)
{
mSpeed = 0;
mDirection.x = 0;
if (aCurrentKeyboardState==KeyEvent.KEYCODE_A)
{
mSpeed = SPRITE_SPEED;
mDirection.x = MOVE_LEFT;
}
else if(aCurrentKeyboardState==KeyEvent.KEYCODE_D)
{
mSpeed = SPRITE_SPEED;
mDirection.x= MOVE_RIGHT;
}
Position.x += mDirection.x * mSpeed;
}
}
private void UpdateJump(int aCurrentKeyboardState)
{
if (mCurrentState == State.Walking)
{
if (aCurrentKeyboardState==KeyEvent.KEYCODE_SPACE && mPreviousKeyboardState!=KeyEvent.KEYCODE_SPACE)
{
Jump();
}
}
if (mCurrentState == State.Jumping)
{
if (mStartingPosition.y - Position.y> 150)
{
Position.y += mDirection.y * mSpeed;
mDirection.y = MOVE_DOWN;
}
if (Position.y > mStartingPosition.y)
{
Position.y = mStartingPosition.y;
mCurrentState = State.Walking;
}
}
}
private void Jump()
{
if (mCurrentState != State.Jumping)
{
mCurrentState = State.Jumping;
mStartingPosition = Position;
mDirection.y = MOVE_UP;
mSpeed = 6;
Position.y += mDirection.y * mSpeed;
}
}
public void Draw(Canvas c)
{
c.drawBitmap(sprite, Position.x,Position.y, null);
}
public void setmCurrentState(State mCurrentState) {
this.mCurrentState = mCurrentState;
}
}
this is the surfaceview:
import com.example.spiceup.Sprite.State;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView {
Context cc;
Bitmap Sprite;
Sprite sprite2;
Handler handlerAnimation100;
private GameLoopThread gameLoopThread;
private SurfaceHolder holder;
public GameView(Context c) {
// TODO Auto-generated constructor stub
super(c);
gameLoopThread = new GameLoopThread(this);
this.cc=c;
this.Sprite=BitmapFactory.decodeResource(getResources(), R.drawable.walk1);
this.Sprite=Bitmap.createScaledBitmap(Sprite, Sprite.getWidth()*2, Sprite.getHeight()*2, false);
sprite2=new Sprite("Spicy",Sprite);
this.requestFocus();
this.setFocusableInTouchMode(true);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
sprite2.Update();
sprite2.Draw(canvas);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
sprite2.setkeyboard(keyCode);
sprite2.setmCurrentState(State.Walking);
return false;
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
sprite2.setmCurrentState(State.Standing);
sprite2.setLastKeyboard(keyCode);
return false;
}
}
if anyone knows where is my error or has a better code to show me I'll be happy, all I'm trying to do is to create a bitmap that moves around and can jump (but also jump while walking)
So I think what is happening in your code is that it's hitting the max height in the game loop and adding back to the y of the sprite. Second game loop run it is no longer above or near the max height distance therefore you stop going down and your start position becomes that position in the air. On a third loop through you hit spacebar again and your sprite starts the whole jumping processes over and the same thing happens on the next to loops through or however many it takes to trigger that if statement to get the sprite to start falling.
Good place to start is have a persistent boolean that determines whether or not the sprite is actually done climbing and jumping state should stay true while climbing and falling. See below.
boolean maxJumpAchieved = false;
if (mCurrentState == State.Jumping)
{
if (mStartingPosition.y - Position.y> 150)
{
maxJumpAchieved = true;
}
if (maxJumpAchieved) {
mDirection.y = MOVE_DOWN;
Position.y += mDirection.y * mSpeed;
}
if (Position.y > mStartingPosition.y)
{
maxJumpAchieved = false;
Position.y = mStartingPosition.y;
mCurrentState = State.Walking;
}
}
I think this should get you in the right direction but if you have issues let me know and I can edit my answer.
Another thing to note is don't set the mCurrentState to State.Walking until you know for sure you're on the ground otherwise you could double jump for days.

Sprite flicker while moving libgdx

This code makes image Border flicker(Flash) while moving left,right,down or up. Why image border flash wile moving even if I use Screen class render() method delta value.
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
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.math.Vector3;
public class MoveSpriteExample extends GdxTest implements InputProcessor {
Texture texture;
SpriteBatch batch;
OrthographicCamera camera;
Vector3 spritePosition = new Vector3();
Sprite sprite;
public void resize (int width, int height) {
}
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
batch = new SpriteBatch();
camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(false, w, h);
texture = new Texture(Gdx.files.internal("data/grasswall.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
sprite = new Sprite(texture);
sprite.setSize(32, 32);
spritePosition.y=100;
sprite.setPosition(spritePosition.x,spritePosition.x);
lastUpdateTime = System.nanoTime();
}
public void Update(float Delta)
{
if (Gdx.input.isKeyPressed(Keys.D)==true)
{
spritePosition.x += 150*(Delta / 1000000000.0);
}else if (Gdx.input.isKeyPressed( Keys.A)==true)
{
spritePosition.x -= 150*(Delta / 1000000000.0);
}
else if (Gdx.input.isKeyPressed( Keys.Z)==true)
{
spritePosition.y -= 150*(Delta / 1000000000.0);
}
else if (Gdx.input.isKeyPressed( Keys.W)==true)
{
spritePosition.y += 150*(Delta / 1000000000.0);
}
}
float lastUpdateTime;
float currentTime;
public void render() {
currentTime = System.nanoTime();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
Update(currentTime - lastUpdateTime);
sprite.setPosition(spritePosition.x,spritePosition.y);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
lastUpdateTime = currentTime;
}
}
pls provide code with sample
As i said in the last post of you, you should take a look at the Tutorial!
First of all you do not need to calculate the delta time yourself. I already postet how you get it in the last post for you.
Ill give you a super short example with a moving sprite without flickering.
At first here is the ApplicationListener. (not a GDXtest)
public class MainClass implements ApplicationListener {
private Screen currentScreen = null;
#Override
public void create() {
Texture.setEnforcePotImages(false);
this.currentScreen = new TestScreen();
}
#Override
public void dispose() {
this.currentScreen.dispose();
}
#Override
public void render() {
this.currentScreen.render(Gdx.graphics.getDeltaTime());
}
#Override
public void resize(int width, int height) {
this.currentScreen.resize(width, height);
}
#Override
public void pause() {
this.currentScreen.pause();
}
#Override
public void resume() {
this.currentScreen.resume();
;
}
}
It's preaty simple and as you can see it does call the render of the screen automatically with the delta time! this.currentScreen.render(Gdx.graphics.getDeltaTime()).
The next thing you need is a simple Screen. So something that does implement the Screeninterface. Id really recommand that you use a Scene2D setup with a stage. But here is an example without.
public class TestScreen implements Screen {
private Sprite mySprite;
private SpriteBatch batch = new SpriteBatch();
public TestScreen() {
this.mySprite = new Sprite(new Texture(
Gdx.files.internal("data/appicon.png")));
this.mySprite.setPosition(50f, 50f);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
this.update(delta);
this.batch.begin();
this.mySprite.draw(batch);
this.batch.end();
}
public void update(float delta) {
if (Gdx.input.isKeyPressed(Keys.D) == true) {
mySprite.setX(mySprite.getX() + 150 * delta);
} else if (Gdx.input.isKeyPressed(Keys.A) == true) {
mySprite.setX(mySprite.getX() - 150 * delta);
} else if (Gdx.input.isKeyPressed(Keys.Z) == true) {
mySprite.setY(mySprite.getY() - 150 * delta);
} else if (Gdx.input.isKeyPressed(Keys.W) == true) {
mySprite.setY(mySprite.getY() + 150 * delta);
}
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
Regardas and do take a look at the tutorial. (Here is again the beginning of it)

Categories

Resources