How to fix this error on libgdx project? - java

Anyone know why does it become red or error in android studio and how to fix it?
Here is the code
package com.appalyse.deliopblaster;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MyGdxGame extends ApplicationAdapter implements InputProcessor {
private SpriteBatch batch;
private BitmapFont font;
private int screenWidth, screenHeight;
private String message = "Touch me";
#Override
public void create () {
batch = new SpriteBatch();
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
font = new BitmapFont();
font.setColor(Color.GREEN);
font.getData().scale(5);
Gdx.input.setInputProcessor(this);
}
#Override
public void dispose() {
batch.dispose();
font.dispose();
}
#Override
public void render () {strong text
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
BitmapFont.*TextBounds* = font.*getBounds*(message);
float x = screenWidth/2 - *textSize*.width/2;
float y = screenHeight/2 + *textSize*.height/2;
batch.begin();
font.draw(batch, message, x, y);
batch.end();
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
message = "Touch down at " + screenX + ", " + screenY;
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
message = "Touch up at " + screenX + ", " + screenY;
return true;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
//message = "Dragging at" + screenX + ", " + screenY;
return true;
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}
It become error at
BitmapFont.*TextBounds* = font.*getBounds*(message);
float x = screenWidth/2 - *textSize*.width/2;
float y = screenHeight/2 + *textSize*.height/2;
Error at TextBounds , getBounds and textSize
I want to know the method for fixing it.

1.Javascript and Java are different languages. So your tag should be removed.
2.Java does not have * as valid syntax like you do it. * is just for multiplication purposes.
3.With libGDX 1.6 there were huge changes for font handling. The "getBounds()" method no longer exists. To get width and height of an text try this:
GlyphLayout glyphLayout = new GlyphLayout();
glyphLayout.setText(font, message);
float x = screenWidth/2 - glyphLayout.width/2;
float y = screenHeight/2 + glyphLayout.height/2;

Related

Collision using Tiled and LibGDX not working

I have implemented a Player class which handles collision between the player and the objects in game. When I run it, he falls through the floor. I have followed a tutorial step by step but it is not working.
Below I have added the Player class and GameScreen class.
I have tried making rectangles around my Player to interact with, but it wasnt a fix.
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.MapObjects;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.skeleton.app.AGame;
import inf112.skeleton.app.entities.Player;
import inf112.skeleton.app.scenes.Hud;
public class GameScreen implements Screen {
public AGame game;
public TiledMap map;
private OrthogonalTiledMapRenderer mapRenderer;
private OrthographicCamera camera;
public Player player;
private BitmapFont font;
public Viewport gameport;
private Hud hud;
public GameScreen (AGame game) {
this.game = game;
hud = new Hud(game.batch);
}
#Override
public void show() {
font = new BitmapFont();
font.setColor(Color.RED);
font.setColor(Color.RED);
map = new TmxMapLoader().load("assets/data/GameBoard2.tmx");
camera = new OrthographicCamera();
camera.setToOrtho(false, 10, 10);
mapRenderer = new OrthogonalTiledMapRenderer(map, (float)0.015625);
mapRenderer.setView(camera);
player = new Player((TiledMapTileLayer) map.getLayers().get("Player"));
player.setPosition(11 * player.getCollisionLayer().getTileWidth(), (player.getCollisionLayer().getHeight() - 14) * player.getCollisionLayer().getTileHeight());
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
mapRenderer.setView(camera);
mapRenderer.render();
mapRenderer.getBatch().begin();
player.draw((SpriteBatch) mapRenderer.getBatch());
mapRenderer.getBatch().end();
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
#Override
public void resize(int width, int height) {
camera.viewportHeight = height;
camera.viewportWidth = width;
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
font.dispose();
mapRenderer.dispose();
map.dispose();
game.dispose();
}
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class Player extends Sprite implements InputProcessor {
private String blockedKey = "blocked";
private Vector2 velocity = new Vector2();
private float speed = 60 * 2, gravity = 60 * 1.8f, increment;
private boolean canJump;
private TiledMapTileLayer collisionLayer;
public Player(TiledMapTileLayer collisionLayer) {
this.collisionLayer = collisionLayer;
//setScale((float)0.1);
}
public void draw(SpriteBatch spriteBatch) {
update(Gdx.graphics.getDeltaTime());
super.draw(spriteBatch);
}
public void update(float delta) {
// apply gravity
velocity.y -= gravity * delta;
// clamp velocity
if(velocity.y > speed)
velocity.y = speed;
else if(velocity.y < -speed)
velocity.y = -speed;
// save old position
float oldX = getX(), oldY = getY();
boolean collisionX = false, collisionY = false;
// move on x
setX(getX() + velocity.x * delta);
// calculate the increment for step in #collidesLeft() and #collidesRight()
increment = collisionLayer.getTileWidth();
increment = getWidth() < increment ? getWidth() / 2 : increment / 2;
if(velocity.x < 0) // going left
collisionX = collidesLeft();
else if(velocity.x > 0) // going right
collisionX = collidesRight();
// react to x collision
if(collisionX) {
setX(oldX);
velocity.x = 0;
}
// move on y
setY(getY() + velocity.y * delta * 5f);
// calculate the increment for step in #collidesBottom() and #collidesTop()
increment = collisionLayer.getTileHeight();
increment = getHeight() < increment ? getHeight() / 2 : increment / 2;
if(velocity.y < 0) // going down
canJump = collisionY = collidesBottom();
else if(velocity.y > 0) // going up
collisionY = collidesTop();
// react to y collision
if(collisionY) {
setY(oldY);
velocity.y = 0;
}
// update animation
}
private boolean isCellBlocked(float x, float y) {
TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (x / collisionLayer.getTileWidth()), (int) (y / collisionLayer.getTileHeight()));
return cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey(blockedKey);
}
public boolean collidesRight() {
for(float step = 0; step <= getHeight(); step += increment)
if(isCellBlocked(getX() + getWidth(), getY() + step))
return true;
return false;
}
public boolean collidesLeft() {
for(float step = 0; step <= getHeight(); step += increment)
if(isCellBlocked(getX(), getY() + step))
return true;
return false;
}
public boolean collidesTop() {
for(float step = 0; step <= getWidth(); step += increment)
if(isCellBlocked(getX() + step, getY() + getHeight()))
return true;
return false;
}
public boolean collidesBottom() {
for(float step = 0; step <= getWidth(); step += increment)
if(isCellBlocked(getX() + step, getY()))
return true;
return false;
}
public Vector2 getVelocity() {
return velocity;
}
public void setVelocity(Vector2 velocity) {
this.velocity = velocity;
}
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
this.speed = speed;
}
public float getGravity() {
return gravity;
}
public void setGravity(float gravity) {
this.gravity = gravity;
}
public TiledMapTileLayer getCollisionLayer() {
return collisionLayer;
}
public void setCollisionLayer(TiledMapTileLayer collisionLayer) {
this.collisionLayer = collisionLayer;
}
#Override
public boolean keyDown(int keycode) {
switch(keycode) {
case Input.Keys.W:
if(canJump) {
velocity.y = speed / 1.8f;
canJump = false;
}
break;
case Input.Keys.A:
velocity.x = -speed;
break;
case Input.Keys.D:
velocity.x = speed;
}
return true;
}
#Override
public boolean keyUp(int keycode) {
switch(keycode) {
case Input.Keys.A:
case Input.Keys.D:
velocity.x = 0;
}
return true;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(float amountX, float amountY) {
return false;
}
The position of a libGDXSprite is bottom left so you need to make a distinction between position as far as your render is concerned, and as far as collision is concerned.
So if you add a half tile width to the x position when checking for the collision that would match what you see, being the base of the sprite.
Imagine a tilewidth of 4f and a position of 7f. As you look the sprite is rendered mainly on the third tile, but the collision is tested against the second one.

libgdx: wrong touch handling on phone

foreword: on desktop everything works just perfect.
Trouble appers when i'm testing my project on phone
I start the game. Here is some buttons on screen: button_1, button_2, button_3. For example i'm touching button_1: with very first touch after app start nothing happing at all. If i'm touching button_1 again, it works fine -> touchDown (button image goes down for 10px) then touchUp (button image goes up for 10px and button code run). But if i touch another button instead, for example button_2, only touchDown of button_1 occurs (button_1 image goes down for 10px and up) and nothing else. It's happens for every button, so i need to touch button twice to make it work.
Button class:
public class myButton {
private float x, y, width, height;
private Texture buttonUp;
public Rectangle bounds;
private boolean isPressed = false;
public myButton(float x, float y, float width, float height, Texture buttonUp) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.buttonUp = buttonUp;
bounds = new Rectangle(x, y, width, height);
}
public boolean isClicked(int screenX, int screenY) {
return bounds.contains(screenX, screenY);
}
public void draw(SpriteBatch batch) {
if (isPressed) {
batch.draw(buttonUp, x, y - 10, width, height);
} else {
batch.draw(buttonUp, x, y, width, height);
}
}
public boolean isTouchDown(int screenX, int screenY) {
if (bounds.contains(screenX, screenY)) {
isPressed = true;
return true;
}
return false;
}
public boolean isTouchUp(int screenX, int screenY) {
if (bounds.contains(screenX, screenY) && isPressed) {
isPressed = false;
return true;
}
isPressed = false;
return false;
}
}
InputHandlerer:
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
screenX = (int) touchPos.x;
screenY = (int) touchPos.y;
playButton.isTouchDown(screenX, screenY);
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
screenX = (int) touchPos.x;
screenY = (int) touchPos.y;
if (playButton.isTouchUp(screenX, screenY)) {
start();
return true;
}
}
in create() method i've got:
touchPos = new Vector3();
Gdx.input.setInputProcessor(new InputHandlerer());
camera = new OrthographicCamera();
viewport = new FitViewport(1080, 1920, camera);
in render() method i've got:
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
batch.setProjectionMatrix(camera.combined);
I repeat - on desktop every button works properly, but on phone not. What could be the problem?
Maybe on desktop is working because you can do only one click at a time, instead on phone you should manage the pointers, also if you don't want to use the multitouch.
On your InputHandlerer class you have to initilize:
private int button1pointer = -1;
private boolean button1isPressed = false;
Then you have to manage the pointers on your tochDown/Up methods:
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
for (int i = 0; i < 2; i++) { //here you can choose how many touches you can manage
if (Gdx.input.isTouched(i)) {
screenX = (int) touchPos.x;
screenY = (int) touchPos.y;
if (playButton.isTouchDown(screenX, screenY) && (button1pointer != i) && (!button1isPressed)) {
button1pointer = i;
button1isPressed = true;
}
return true;
}
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if (!(Gdx.input.isTouched(pointer)) && (button1pointer == pointer) && (button1isPressed)) {
button1pointer = -1;
button1isPressed = false;
start();
return true;
}
}

How to allow user to touch scroll using libgdx in a cross-plattform project?

I'm developing a cross-plattform application in java (focusing on android right now) and I'm mainly using libgdx to achieve this. Right now I have a screen that consists of rows of clickable buttons where the rows extend below the screen. I want to allow the user to touch/flick scroll up and down in order for them to view all the different buttons available.
Some relevant information: I'm using an Orthographic Camera and an InputProcessor.
Right now I'm thinking that the camera is supposed to move up and down based on the touchDragged method in the InputProcessor.
I'll attach the relevant code in the GameRenderer and InputHandler classes below. (with a lot of unrelevant code removed.) Any help or suggestions would be great, I've googled like a mad man and been stuck for a day now.
GameRenderer
public class GameRenderer {
private GameWorld myWorld;
private OrthographicCamera cam;
private SpriteBatch batcher;
private ArrayList<Element> elements = new ArrayList<Element>();
private int gameHeight;
public GameRenderer(GameWorld world, int gameHeight) {
myWorld = world;
elements = myWorld.getElement();
this.gameHeight = gameHeight;
cam = new OrthographicCamera();
cam.setToOrtho(true, 136, gameHeight);
batcher = new SpriteBatch();
batcher.setProjectionMatrix(cam.combined);
}
public void render() {
Gdx.gl.glClearColor(0, 191, 255, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batcher.begin();
for (int i = 0; i < elements.size(); i++) {
Element e = (Element) elements.get(i);
batcher.draw(AssetLoader.element, e.getLocationX(), e.getLocationY(), e.getElementWidth(), e.getElementHeight());
AssetLoader.font.draw(batcher, e.getFormula(), e.getLocationX() + 4, e.getLocationY() + 3);
}
batcher.end();
}
}
InputHandler
public class InputHandler implements InputProcessor
{
private Element myElement;
private float scaleX, scaleY;
private ArrayList<Element> elements;
GameWorld myWorld;
GameRenderer myRenderer;
public InputHandler(GameWorld game, GameRenderer renderer, ArrayList elementList, float x, float y) {
myWorld = game;
myRenderer = renderer;
elements = elementList;
scaleX = x;
scaleY = y;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return true;
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}

Do something ontouch in libgdx

I have written the following code. It is simple rectangle in middle of the screen. I want the rectangle to move -50 in y direction onClick (direction is just for illustration purposes only). Despite trying various methods. I cannot get the code to work. I am not sure if I am doing something fundamentally wrong. I would appreciate some direction from better learned minds.
package com.mygdx.gameobjects;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class Hero extends Rectangle implements InputProcessor{
private Rectangle hero;
private Vector2 position;
public Hero(){
position = new Vector2(x, y);
hero = new Rectangle(position.x+68-10, position.y+204-30-20, 20, 20);
}
public void update (float delta) {
}
public void onClick() {
position.y = -50;
}
public Rectangle getHero() {
return hero;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
onClick();
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}

trying to make my (very simple) libgdx game more object-oriented. added GameObject class - game doesn't launch anymore

what the title says.
first of all, here are my 3 classes:
MyGdxGame:
package com.mygdx.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.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Sprite sp;
Texture img;
int width;
int height;
Player p;
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("dildo.png");
sp = new Sprite(img);
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
p = new Player(img);
Gdx.input.setInputProcessor(p);
}
#Override
public void render () {
//sp.flip(true, false);
p.update();
Gdx.gl.glClearColor(255,255,255,255);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
//batch.draw(img, 0, height/6, -img.getWidth(), img.getHeight());
p.draw(batch);
//sp.draw(batch);
batch.end();
}
}
Player:
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.TimeUtils;
public class Player extends GameObject implements InputProcessor {
private Sprite sp;
private Texture te;
private float xPos;
private float yPos;
private float xSpeed;
private float ySpeed;
private float accelerationX;
private float g;
Vector2 position;
private float accelerationJump = 9f;
private boolean allowJump = true;
private boolean isDragged = false;
public Player(Texture sprite) {
xPos = 0;
yPos = (super.getHeight()/6);
xSpeed = 5.5f;
ySpeed = 0;
te = sprite;
sp.setX(xPos);
sp.setY(yPos);
g = 0.2f;
accelerationX=0.02f;
}
public void update() {
ySpeed -= g;
if(isDragged) {
xSpeed += accelerationX;
moveBy(xSpeed,0);
isDragged = false;
}
if (getxPos() > super.getWidth()) {
setxPos(0);
} else {
moveBy(xSpeed, ySpeed);
}
if (getyPos() < super.getHeight() / 6) {
moveTo(sp.getX(), getHeight() / 6);
}
if (onGround()) {
allowJump = true;
ySpeed = 0;
}
}
public void jump() {
ySpeed += accelerationJump;
}
public boolean onGround() {
return (sp.getY() == super.getHeight()/6);
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if (allowJump && isDragged == false) {
allowJump = false;
jump();
}
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
if(isDragged==false) {
isDragged = true;
}
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}
GameObject:
package com.mygdx.game;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public abstract class GameObject {
private Sprite sp;
private int width;
private int height;
private float xPos;
private float yPos;
private float xSpeed;
private float ySpeed;
public GameObject(int width, int height) {
this.width = width;
this.height = height;
}
public GameObject() {
}
public void moveTo(float xPos2, float yPos2) {
setxPos(xPos2);
setyPos(yPos2);
}
public void moveBy(float dx, float dy) {
sp.setY(sp.getY() + dy);
sp.setX(sp.getX() + dx);
}
public float getxPos() {
return xPos;
}
public float getyPos() {
return yPos;
}
public void setxPos(float xPos2) {
sp.setX(xPos2);
}
public void setyPos(float yPos) {
sp.setY(yPos);
}
public float getxSpeed() {
return xSpeed;
}
public float getySpeed() {
return ySpeed;
}
public void setxSpeed(float xSpeed2) {
xSpeed = xSpeed2;
}
public void setySpeed(float ySpeed2) {
ySpeed = ySpeed2;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height
}
public void draw(SpriteBatch batch) {
sp.draw(batch);
}
}
basically, the game as it is now is just a sprite moving from left to right of the screen - when you tap, it jumps, when you drag the screen it dashes forward. i want to now start adding things like power-ups and platforms so i thought it would be a good idea to create a GameObject class. but now when i run it the screen goes black and the application just crashes, i have a feeling it has something to do with the way i'm initialising my Texture? any ideas?
any help is highly appreciated, many thanks in advance :)
edit: in GameObject constructor width and height are the width and height of the screen
if you have your image in asset folder of android project, test img = new Texture (Gdx.files.internal ("dildo.png"));
example:
AdroidProyect->Asset->YourFile.png
and in player i think
1-
public Player(Texture sprite) {
xPos = 0;
yPos = (super.getHeight()/6);
xSpeed = 5.5f;
ySpeed = 0;
te = sprite;
//add and refactor your code if it works
sp = new Sprite(sprite);
sp.setX(xPos);
sp.setY(yPos);
g = 0.2f;
accelerationX=0.02f;
}
or 2-
I do not want to do, but in your MyGdxGame, your
you say,
..//
img = new Texture ("dildo.png");
         sp = new Sprite (img);
         p = new Player (img);
..//
but after you pass the player a texture, because no sprite or as I said before initializing the player, you could also change the constructor of the player.
public Player(Sprite sprite){t
..//
this.sp = sprite;
ADD NEW:
Musta Been to look at your code and no offense, you have a code a little crazy :)
visibility of variables is somewhat confusing because what you want but do not test well and looks over the access modifiers, aver if they match what you want to do.
my English is also a little crazy too.
in your class MyGdxGame;
..//
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture(Gdx.files.internal("dildo.png"));
sp = new Sprite(img);
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
p = new Player(sp);
Gdx.input.setInputProcessor(p);
}
..//
class GameObject Change;
public abstract class GameObject {
protected Sprite sp;
protected int width;
protected int height;
protected float xPos;
protected float yPos;
protected float xSpeed;
protected float ySpeed;
player class.
public class Player extends GameObject implements InputProcessor {
//private Sprite sp; remove
private Texture te;
//private float xPos; remove
//private float yPos; remove
//private float xSpeed; remove
//private float ySpeed; remove
private float accelerationX;
private float g;
Vector2 position;
private float accelerationJump = 9f;
private boolean allowJump = true;
private boolean isDragged = false;
public Player(Sprite sprite) {
..//
sp = sprite;

Categories

Resources