Making A Sprite Bounce Off The Edge - java

I'm trying to make a sprite bounce off the edge of the screen when it is less than 0. Right now it just zooms across the screen into the void. Here is my code-note that CentipedeBody is a class that extends Sprite. In the render method I call ex.update(); which is an object of the class. Then in between the batch.begin() and batch.end() I have batch.draw(ex,ex.getPosition().x,ex.getPosition().y,ex.getSize().x,ex.getSize().y);
public class CentipedeBody extends Sprite
{
public CentipedeBody(TextureRegion image,Vector2 position,Vector2 size) {
super(new TextureRegion(image));
this.position = position;
this.size=size;
bounds=new Rectangle(position.x,position.y,8,8);
left=true;
}
public void update() {
bounds.set(getPosition().x,getPosition().y,8,8);
if (left==true) {
position.x-=(.5f);
up=false;
down=false;
right=false;
left=true;
}
if (right==true) {
position.x+=.5f;
left=false;
right=true;
down=false;
up=false;
}
if (down==true) {
position.y-=(.5f);
right=false;
left=false;
down=true;
up=false;
if(position.x<0)
{
left=false;
right=true;
}
}
}

why bounds in your child class, Sprite having already bounds, use that if you're interested in collision with other objects. Same for position and for size, I don't think you need these extra data member in your Child class, use parent x,y for position and width and height for dimension.
public class CentipedeBody extends Sprite {
enum State{
LEFT,RIGHT,DOWN
}
State currentState,previousState ;
public static final float DOWN_MOVEMENT=50;
public float downMovCounter;
public float speed;
public CentipedeBody(TextureRegion image, Vector2 position, Vector2 size) {
super(new TextureRegion(image));
setPosition(position.x,position.y);
setSize(size.x,size.y);
currentState=State.LEFT;
previousState=State.LEFT;
speed=50;
}
public void update() {
float delta=Gdx.graphics.getDeltaTime();
if(currentState ==State.LEFT){
setPosition(getX()-speed*delta,getY());
if(getX()<0) {
previousState=currentState;
currentState = State.DOWN;
}
}
if(currentState ==State.RIGHT){
setPosition(getX()+speed*delta,getY());
if(getX()> Gdx.graphics.getWidth()-getWidth()) {
previousState=currentState;
currentState = State.DOWN;
}
}
if(currentState ==State.DOWN){
setPosition(getX(),getY()+speed*delta);
downMovCounter++;
if(downMovCounter>DOWN_MOVEMENT){
downMovCounter=0;
currentState =previousState==State.LEFT?State.RIGHT:State.LEFT;
}
}
}
}
In render method
batch.begin();
batch.draw(centipedeBody,centipedeBody.getX(),centipedeBody.getY(),centipedeBody.getWidth(),centipedeBody.getHeight());
batch.end();
centipedeBody.update();
May be you need bounds, size and position in CentipedeBody, I can't judge your game requirement by one class code so you can easily integrate your variable in my code.

Related

Java LibGDX sprite moving twice as fast as camera

So I've got a class which extends ApplicationAdapter implements InputProcessor and does the following when you drag around on the screen.
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
float x = Gdx.input.getDeltaX();
float y = Gdx.input.getDeltaY();
if (Store.isGameState) {
Store.Entity.player.setxMove(x);
Store.Entity.player.setyMove(-y);
}
return true;
}
In my player class I have a update method which does the following:
#Override
public void update() {
x += xMove;
y += yMove;
Store.Camera.Camera.position.set(Store.Entity.player.getXPos() + Store.Entity.player.getWidth() / 2, Store.Entity.player.getYPos() + Store.Entity.player.getHeight() / 2, 0);
Store.Camera.Camera.update();
}
and a render method which is:
public void render(SpriteBatch SB) {
SB.begin();
Store.Entity.sprite.setPosition(Store.Entity.player.getXPos(), Store.Entity.player.getYPos());
Store.Entity.sprite.draw(SB);
SB.end();
}
Which all-in-all works, the camera will move around as will my sprite. However my sprite does not move at the same speed as my camera and I can't for the life of me figure out why this is the case. The sprite moves roughly twice as fast as the camera does, and isn't centered on the player which I'd like ideally.
EDIT:
So in my GameState I have the following:
package com.imjoshscott.dataria.states;
import com.imjoshscott.dataria.Game;
import com.imjoshscott.dataria.Store;
public class GameState extends State {
public GameState(Game game) {
super(game);
Store.isGameState = true;
Store.Entity.getPlayer(game, Store.Entity.getSprite());
Store.Graphics.getSpriteBatch();
Store.Graphics.getTiledMap();
Store.Graphics.getTiledMapRenderer();
Store.Camera.getCamera();
Store.Camera.getHudCamera();
Store.Camera.Camera.setToOrtho(false, Game.GAME_WIDTH, Game.GAME_HEIGHT);
Store.Camera.HudCamera.setToOrtho(false, Game.GAME_WIDTH, Game.GAME_HEIGHT);
Store.Camera.Camera.viewportHeight = Game.GAME_HEIGHT / 2.5f;
Store.Camera.Camera.viewportWidth = Game.GAME_WIDTH / 2.5f;
}
#Override
public void update() {
Store.Graphics.tiledMapRenderer.setView(Store.Camera.Camera);
Store.Entity.player.update();
}
#Override
public void render() {
Store.Graphics.tiledMapRenderer.render();
Store.Entity.player.render(Store.Graphics.SB);
}
}
The Camera stuff in the Store class:
public static class Camera {
public static OrthographicCamera Camera;
public static OrthographicCamera HudCamera;
public static OrthographicCamera getCamera() {
if(Camera == null)
Camera = new OrthographicCamera();
return Camera;
}
public static OrthographicCamera getHudCamera() {
if(HudCamera == null)
HudCamera = new OrthographicCamera();
return HudCamera;
}
}
EDIT: Showing update & render methods
public void update() {
moveCreature();
Store.Entity.sprite.setPosition(Store.Entity.player.getXPos(), Store.Entity.player.getYPos());
Store.Camera.Camera.position.set(Store.Entity.player.getXPos(), Store.Entity.player.getYPos(), 0);
Store.Camera.Camera.update();
xMove = 0f;
yMove = 0f;
}
public void render(SpriteBatch SB) {
SB.begin();
Store.Entity.sprite.draw(SB);
SB.end();
}
Updated My previous answer is not correct, upon seeing and checking the project I saw this render() method for GameState class (after looked around for several places but no dice, if ever found this very weird problem again I would direct to this).
public void render() {
Store.Graphics.tiledMapRenderer.render();
Store.Entity.player.render(Store.Graphics.SB);
}
Just one thing we need to add to fix very weird problem is to add the following code
Store.Graphics.SB.setProjectionMatrix(Store.Camera.Camera.combined);
so we would have
public void render() {
Store.Graphics.SB.setProjectionMatrix(Store.Camera.Camera.combined);
Store.Graphics.tiledMapRenderer.render();
Store.Entity.player.render(Store.Graphics.SB);
}
That means we need to set projection matrix to current active SpriteBatch to properly render stuff. It's also safe to set it before even render the tile as it uses the same camera as of player.

Removing sprite when Touched

The sprite is spawned every second and when it's touched, it should be removed.
This is what I did:
//Render the sprites and making them move:
public void draw(SpriteBatch batch) {
for(Sprite drawEnemy:enemies) {
drawEnemy.draw(batch);
drawEnemy.translateY(deltaTime * movement);
touchInput(drawEnemy.getX(),drawEnemy.getY(),
drawEnemy.getWidth(),drawEnemy.getHeight(),drawEnemy);
}
}
//Detecting the touch input:
public void touchInput(float x,float y,float w,float h,Sprite sprite){
float touchX=Gdx.input.getX();
float touchY=Gdx.input.getY();
if(Gdx.input.justTouched()){
if(touchX > x && touchX < x+w ){
enemyIterator.remove();
Pools.free(sprite);
}
}
}
The touch input is detected, but I'm not sure how to remove them.
The result is an error when I touch them.
You cannot reuse an iterator, so your enemyIterator is invalid and will cause exceptions.
To avoid needing to do this, change your touchInput method to simply test whether the object should be removed, not to remove it. Also note that you need to convert screen coordinates to world coordinates, so you must use the camera as well.
private Vector3 TMPVEC = new Vector3();
public boolean touched(float x,float y,float w,float h) {
TMPVEC.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(TMPVEC);
return Gdx.input.justTouched() && TMPVEC.x > x && TMPVEC.x < x+w;
}
You can only use the iterator at the place where you're iterating. So you must acquire a local reference at the loop like this:
public void draw(SpriteBatch batch) {
for (Iterator<Sprite> iterator = enemies.iterator(); iterator.hasNext();) {
Sprite drawEnemy = iterator.next();
drawEnemy.draw(batch);
drawEnemy.translateY(deltaTime * movement);
if (touched((drawEnemy.getX(),drawEnemy.getY(), drawEnemy.getWidth(),drawEnemy.getHeight())){
iterator.remove();
Pools.free(sprite);
}
}
}
However, the above is kind of muddy because you're mixing update and drawing code up together, and drawing before updating, and checking for touches over and over without need. I would redo it all like this:
private Vector3 TMPVEC = new Vector3();
public void update (Camera camera, float deltaTime) {
boolean checkTouch = Gdx.input.justTouched();
if (checkTouch) {
TMPVEC.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(TMPVEC);
} //This way we only unproject the point once for all sprites
for (Iterator<Sprite> iterator = enemies.iterator(); iterator.hasNext();) {
Sprite enemy = iterator.next();
enemy.translateY(deltaTime * movement);
if (checkTouch && touched(enemy, TMPVEC)){
iterator.remove();
Pools.free(sprite);
}
}
}
private void touched (Sprite sprite, Vector3 touchPoint){
return sprite.getX() <= touchPoint.x &&
sprite.getX() + sprite.getWidth() <= touchPoint.x;
}
public void draw (SpriteBatch batch){
for (Sprite sprite : enemies) sprite.draw(batch);
}
Here you would call update before draw from the owning class.

LibGDX Bouncing ball

I want to create an infinite bouncing ball. For now I'm just trying to make the bouncing on Y (up & down).
This is my GameWorld class, you can see there is a method collides to detect the collision but How to make that "circle" go up?
public class GameWorld {
private Circle rond;
private Rectangle rect;
public GameWorld(int midPointY) {
rond = new Circle(100, midPointY - 5, 5);
rect = new Rectangle(0, 200, Gdx.graphics.getWidth(), 5);
}
public void update(float delta) {
if(collides(rond)){
}else
rond.y++;
}
public boolean collides(Circle rond) {
if (rect.y < rond.y + (rond.radius)*2) {
return (Intersector.overlaps(rond, rect));
}
return false;
}
public Circle getRond() {
return rond;
}
public Rectangle getRect() {
return rect;
}
}
Of course, I have another class GameRenderer that rendere these two objects
Typically with any kind of physics you want to store the speed of an object as well as its position. That way, each time though your update loop you just have to change the y position by the y speed. When the collision occurs, all you need to do is calculate the new yspeed and update rond.
public void update(float delta) {
if(collides(rond)){
rond.yspeed = -rond.yspeed;
}else{
rond.y+=rond.yspeed*delta;
}
}

In my 2d game, the camera is moving 1 extra pixel each time the player is at the left or the right limit of the screen

Please excuse my bad english. I am having a problem while implementing a 2d camera. I made the camera to follow the player until it reaches near the edge of the game-level, where only the player moves and the camera stops. I made this easily but my problem is that the camera is not proper. The camera continues to move 1 extra pixel every time you reach at the edge of the game-level limit in all four directions (The code I posted only shows horizontal movement. This I did for simplicity). This means if you move 40 times to and fro near the left edge of the game, camera will move 40 extra pixels right! I have no idea how to solve this.
I posted a very simplified version from my original code below and made it as small as i could to show how the program works. Following just moves the player and camera horizontally across the screen.
This is 'theGamePanel' class (it is the main class) :-
public class TheGamePanel extends JPanel implements Runnable, KeyListener
{
private boolean left, right;
private float cameraX, cameraY;
private World world = new World();
private Player player = new Player();
public TheGamePanel()
{
//setting the size of panel
}
public static void main(String[] args)
{
//setting the window
}
public void paint(Graphics g)
{
super.paint(g);
// drawing the game-level and player
g.translate((int)cameraX, (int)cameraY);
world.paint(g);
player.paint(g);
g.translate(-(int)cameraX, -(int)cameraY);
}
public void upd()
{
player.update(left, right, this);
}
#Override
public void run()
{
// game-loop
}
#Override
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if(code == KeyEvent.VK_LEFT)
{
left = true;
}
if(code == KeyEvent.VK_RIGHT)
{
right = true;
}
}
#Override
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
left = false;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
right = false;
}
}
#Override
public void keyTyped(KeyEvent e)
{
}
//to set camera position
public void setCameraX(float cameraDx)
{
cameraX += cameraDx;
}
}
This is the 'Player' class. This class is where the player and camera movement takes place. The camera's x and values are then returned to 'TheGamePanel' :-
public class Player
{
private float x, y;
private float dx = 1;
private int width = 32, height = 32;
private float leftLimit, rightLimit;
public Player()
{
//player's initial x and y coordinates
x = 320;
y = 240;
//camera's limit (where the camera needs to stop following player)
leftLimit = x;
rightLimit = 960;
}
public void paint(Graphics g)
{
//for painting the player
g.setColor(Color.GREEN);
g.fillRect((int)x, (int)y, width, height);
}
//to move player and camera
public void update(boolean left, boolean right, TheGamePanel panel)
{
if(left == true)
{
x -= dx;
if(x > leftLimit && x < rightLimit)
{
panel.setCameraX(dx);
}
}
if(right == true)
{
x += dx;
if(x > leftLimit && x < rightLimit)
{
panel.setCameraX(-dx);
}
}
}
}
And lastly, the 'World' class. This class is used to simply paint a big map(level) in the background :-
public class World
{
private BufferedImage map;
private int tileWd = 32, tileHi = 32;
public World()
{
try
{
map = ImageIO.read(new File("map1.png"));
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void paint(Graphics g)
{
//simply paints a game-level in the background
}
}
If anything not understandable please tell. I will add more details.
Consider the case where the player is standing near the right edge of the map. One quick tap on the "right" key moves him out of the camera-follow area so the camera doesn't move. One quick tap on the "left" key moves him back into the camera-follow area. The camera will be moved "dx" units to the left. This will have the effect of the player slowly creeping closer to the right edge of the playing field, if I'm correct.
(To find bugs like this, I generally litter the code with System.out.println messages. If there's a lot of messages, I write them to a file and do text searches on keywords)
you have a location for your camera (cameraX,cameraY) but nothing for the player?
just create a location for player (playerX,playerY)
change the paint like:
public void paint(Graphics g)
{
super.paint(g);
// drawing the game-level and player
g.translate((int)cameraX, (int)cameraY);
world.paint(g);
g.translate((int)playerX, (int)playerY);
player.paint(g);
g.translate(-(int)(playerX+cameraX), -(int)(playerY+cameraY));
}
and when player come near borders don't move camera and only player.
I believe the following change fixes the problem:
Player class:
//to move player and camera
public void update(boolean left, boolean right, TheGamePanel panel)
{
if(left == true)
{
x -= dx;
if(x > leftLimit && x < rightLimit)
{
panel.setCameraX(dx);
}
}
if(right == true)
{
if(x > leftLimit && x < rightLimit)
{
panel.setCameraX(-dx);
}
x += dx; // Only change: moved this line AFTER the if block
}
}
By testing x after changing it when moving left and before changing it when moving right you compensate for the error that was otherwise accumulating every time you reached one of the boundaries.
Anyway I suggest you to change your approach to the problem in order to make your code easier to maintain and more flexible. One way to do that is to calculate the camera position based on current player's position on every frame.
Note: The line g.translate(-(int)cameraX, -(int)cameraY); in the paint() method of TheGamePanel class is unnecessary once the method translate(int, int) is not incremental.
Regards

LibGDX - IllegalArgumentException causes my game to crash whenever my main character touches a Coin

Trying to figure out why my Android game crashes whenever the player touches an animated coin. I have attached an image of the LogCat and my code is below (NOTE: all game objects in ![Renderer] are in an arraylist called toRender. the 2 coins in the game are currently held in the 3rd and 4th position in the list). Renderer and Coin classes respectively:
public class Renderer extends ApplicationAdapter {
private SpriteBatch batch;
private Texture background;
private ArrayList<GameObject> toRender;
private Timer timer;
private float delta;
private Game game;
public Renderer(ArrayList<GameObject> toRender) {
batch = new SpriteBatch();
background = new Texture(Gdx.files.internal("background2.png"));
this.toRender = toRender;
timer = Timer.getInstance();
}
public void collect() {
// for every object in toRender (an arraylist of objects)
for (GameObject o : toRender) {
// if player collides with/collects an object
if (Player.getInstance(null).hasCollected(o)) {
// if its the first coin that he collides with, dispose it
if (o.equals((Coin) toRender.get(3))) {
((Coin) toRender.get(3)).dispose();
}
// if its the second coin that he collides with, dispose it
if (o.equals((Coin) toRender.get(4))) {
((Coin) toRender.get(4)).dispose();
}
}
}
}
public void beginRendering() {
delta = Timer.getInstance().getTime();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(background, 0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
timer.drawTime(batch);
for (GameObject object : toRender) {
object.update();
boolean objectIsntCoin = !(object.equals(toRender.get(3)) ||
object.equals(toRender.get(4))); //the 2 coins are in the 3rd and 4th position in the array list
// draw every object's sprite apart from coin, since coin should render the animation rather than the sprite
if (objectIsntCoin) {
object.draw(batch);
}
}
collect();
((Flag) toRender.get(toRender.size() - 1)).drawLevelComplete(batch);
// if the coin exists (i.e. hasn't been disposed), render the animation
if (((Coin) toRender.get(3)).checkExists()) {
((Coin) toRender.get(3)).render(delta);
}
// if the coin exists (i.e. hasn't been disposed), render the animation
if (((Coin) toRender.get(4)).checkExists()) {
((Coin) toRender.get(4)).render(delta);
}
batch.end();
}
}
public class Coin extends GameObject implements Screen {
private SpriteBatch batch;
private Animation animation;
private float time;
private float xPos;
private float yPos;
private Rectangle objectRect;
private boolean exists;
public Coin(Sprite spr, float xPos, float yPos, float radius) {
super(spr, xPos, yPos, radius);
this.xPos = xPos;
this.yPos = yPos;
batch = new SpriteBatch();
objectRect = new Rectangle(getxPos(), getyPos(), getSprite().getWidth(), getSprite().getHeight());
exists = true;
time = 0;
show();
}
public Rectangle getRect() {
return objectRect;
}
public void render(float delta) {
// TODO Auto-generated method stub
batch.begin();
batch.draw(animation.getKeyFrame(time += delta), xPos, yPos);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
animation = new Animation(1 / 8f,
new TextureRegion(new Texture(Gdx.files.internal("coin1.png"))),
new TextureRegion(new Texture(Gdx.files.internal("coin2.png"))),
new TextureRegion(new Texture(Gdx.files.internal("coin3.png"))),
new TextureRegion(new Texture(Gdx.files.internal("coin4.png"))),
new TextureRegion(new Texture(Gdx.files.internal("coin5.png"))),
new TextureRegion(new Texture(Gdx.files.internal("coin6.png"))),
new TextureRegion(new Texture(Gdx.files.internal("coin7.png"))),
new TextureRegion(new Texture(Gdx.files.internal("coin8.png"))));
animation.setPlayMode(Animation.PlayMode.LOOP);
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
batch.dispose();
exists = false;
}
public boolean checkExists() {
return exists;
}
}
LogCat:1
So the errors that the LogCat point to:
1)dispose() method in Coin:
batch.dispose();
2)collect() method in Renderer:
if(o.equals((Coin) toRender.get(3))) {
3)beginRendering() method in Renderer:
for (GameObject object : toRender) {
Does anyone know why my program is crashing? I just want the animated coin to disappear when the Player touches it. Currently the coin DOES disappear, the application just shuts down immediately after though. Have been stuck on this for a while so any insight is highly appreciated.
Thank you in advance.
First, I want to mention that downcasting the objects in toRender like you are is both dangerous and indicates there is a flaw in your design. Additionally, you'll notice that equals(Object) accepts an Object as an argument; you don't need to cast it to Coin.
Anyway, the reason your program is crashing is explained in the IllegalArgumentException message,
buffer not allocated with newUnsafeByteBuffer or already disposed.
You're trying to dispose your Coin's batch when it has already been disposed.
In your collect() method, you loop through the objects, and you dispose of their batches, but the Coin objects themselves are never removed from your toRender list. So, the next time collect() is called, it will loop through those same Coin objects and try to dispose of them again, and an exception is thrown.
The solution is to remove the Coin objects from your toRender list when they no longer belong in your game's scene. However, you can't remove an element from a list while you're iterating over it, as this would disrupt the loop. Instead, remove them like so:
public void collect() {
// Holds the Coins we want to remove from toRender
final Collection<GameObject> toRemove = new LinkedList<>();
for (GameObject o : toRender) {
if (Player.getInstance(null).hasCollected(o)) {
if (o.equals(toRender.get(3))) {
final Coin coin = (Coin) toRender.get(3);
coin.dispose();
toRemove.add(coin);
}
if (o.equals(toRender.get(4))) {
final Coin coin = (Coin) toRender.get(4);
coin.dispose();
toRemove.add(coin);
}
}
}
// Remove the collected Coins
toRender.removeAll(toRemove);
}

Categories