How to change Sprite texture to Animation - java

I have a Sprite that spawns every second, what I wan't to do is change the sprite texture to animation, and the when it's touched it will be back to a normal texture.
public void draw(SpriteBatch batch){
enemyIterator=enemies.iterator(); //arraylist iterator
boolean touched=Gdx.input.justTouched();
float touchX=Gdx.input.getX();
//rendering and making the current sprite move
while(enemyIterator.hasNext()){
Sprite sprite=enemyIterator.next();
sprite.draw(batch);
sprite.translateY(deltaTime*movement);
//detecting if the screen is touched and if the inputX is inside of the sprite.
if(touched==true && touchX > sprite.getX() && touchX < sprite.getX()+sprite.getWidth()){
enemyIterator.remove(); //removing the sprite when touched.
Pools.free(sprite); //freeing the Pools
}
}

To change from a texture to an animation
Create a subclas of Sprite called MySprite or something, and override the draw(batch) method.
In the overriden draw method, if you want to draw a texture, simply call super.draw(batch), otehrwise use your animation draw code. You can get the delta time using Gdx.graphics.getDeltaTime()
Why you have to specify timePassed
Your program will run at different frame rates to the animation, so by telling the animation how much time has passed, it can work out what frame it should be on according to it's own framerate.
Note that the framerate of your app can vary from frame-to-frame depending on how much work it has to do.

Related

libgdx drawing an image at touch location

I'm just trying to get libgdx to create a picture wherever I touch the screen.
here's what i have that isn't doing anything
SpriteBatch batch;
Texture img;
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
public class MyInputProcessor implements InputProcessor {
public boolean touchDown (int x, int y, int pointer, int button) {
batch.begin();
batch.draw(img,Gdx.input.getX(),Gdx.input.getY());
batch.end();
return true;
}
... (the rest of the input methods)
if you can't tell, I don't really know what I'm doing yet, I think it has to do with the batch.draw() being in the touchDown method instead of the render area but I can't figure out from research how to do it a different way either
or maybe this is all wrong too, point is I'm doing this to learn so hopefully the correct answer will help me understand some important things about java in general
LibGDX, like basically all game engines, re-renders the entire scene every time render() is called. render() is called repeatedly at the frame rate of the game (typically 60fps if you don't have a complex and unoptimized game). The first drawing-related thing you usually do in the render() method is to clear the screen, which you have already done with Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);. Then you re-draw the whole scene with whatever changes there might be since the last frame.
You are trying to draw something with the batch outside of the render method. In this case, you are doing it when there is a touch down. But since you are doing this only when there is a touch down, the object will appear and disappear on the next call to render(), so it will only be on screen for 1/60th of a second. If you want to do this with an input processor, you need to set some variable to true to indicate the render method should draw it, and other variables to indicate where to draw it. Then in the render() method, you draw the stuff if the variable is true.
Secondly, the x and y that an input processor gets do not necessarily (and usually don't) correspond with the x and y in OpenGL. This is because OpenGL has it's own coordinate system that is not necessarily sized exactly the same as the screen's coordinate system. The screen has (0,0) in the top left with the Y axis going down, and the width and height of the screen matching the number of actual pixels on the screen. OpenGL has (0,0) in the center of the screen with the Y axis going up, and the width and height of the screen being 2 regardless of the actual screen pixels.
But the OpenGL coordinate system is modified with projection matrices. The LibGDX camera classes make this simpler. For 2D drawing, you need an OrthographicCamera. You set the width and size of the OpenGL world using the camera, and can also position the camera. Then you pass the camera's calculated matrices to the SpriteBatch for it to position the scene in OpenGL space.
So to get an input coordinate into your scene's coordinates, you need to use that camera to convert the coordinates.
Finally, LibGDX cannot magically know that it should pass input commands to any old input processor you have created. You have to tell it which InputProcessor it should use with a call to Gdx.input.setInputProcessor().
So to fix up your class:
SpriteBatch batch;
Texture img;
boolean isTouchDown;
final Vector3 touchPosition = new Vector3();
OrthographicCamera camera;
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
camera = new OrthographicCamera();
Gdx.input.setInputProcessor(new MyInputProcessor()); // Tell LibGDX what to pass input to
}
#Override
void resize (int width, int height) {
// Set the camera's size in relation to screen or window size
// In a real game you would do something more sophisticated or
// use a Viewport class to manage the camera's size to make your
// game resolution-independent.
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update(); // re-calculate the camera's matrices
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined); // pass camera's matrices to batch
batch.begin();
if (isTouchDown) { // Only draw this while the screen is touched.
batch.draw(img, touchPosition.x, touchPosition.y);
}
batch.end();
}
public class MyInputProcessor implements InputProcessor {
public boolean touchDown (int x, int y, int pointer, int button) {
isTouchDown = true;
touchPosition.set(x, y, 0); // Put screen touch coordinates into vector
camera.unproject(touchPosition); // Convert the screen coordinates to world coordinates
return true;
}
public boolean touchUp (int screenX, int screenY, int pointer, int button){
isTouchDown = false;
return true;
}
//... (the rest of the input methods)
}

How to make set sprite position based on degree?

I have a sprite arrow that rotates like a clock hand, I need my other sprite to spawn where the arrow is pointed.
//arrow rotation:
int x=Gdx.input.getX();
int y=Gdx.graphics.getHeight()-Gdx.input.getY();
double radians= Math.atan2(y - launcherSpr.getY(), x - arrowSpr.getX());
float angle=(float)radians*MathUtils.radiansToDegrees-85;
arrowSpr.setRotation(angle);
//The other sprite that is spawned whenever the method is called
public void newBullet(){
Sprite sprite=Pools.obtain(Sprite.class);
sprite.set(bulletSpr);
sprite.setPosition(300, 600);
bullets.add(sprite);
}
Right now when the method newBullet() is called, a bullet sprite will spawn and it's Y is translated by 5(going upward by 5 pixels per frame). What I want is that the bullet will spawn based on where the arrow is pointed and go upward or downward based on the situation.

libgdx How do you draw or spawn a new sprite/object every second?

I'm trying to spawn a sprite or object every second, but what happens is that the sprite is only rendered for less than a second every second
public class Player {
private Sprite playerSpr,enemy,enemy1;
public Vector2 position,size;
public Rectangle bounds;
private SpriteBatch batch;
private float deltaTime;
private float timer,elapsedTime;
private ArrayList<Sprite> enemies;
private Iterator<Sprite> enemyIterator;
public void create(){
position=new Vector2(200,910);
size=new Vector2(82,80);
batch=new SpriteBatch();
timer=0;
playerSpr=new Sprite(new Texture(Gdx.files.internal("spr_player.png")));
enemy=new Sprite(new Texture(Gdx.files.internal("spr_player.png")));
enemy1=new Sprite(new Texture(Gdx.files.internal("spr_player.png")));
bounds=new Rectangle(position.x,position.y,size.x,size.y);
enemies=new ArrayList<Sprite>();
enemies.add(playerSpr);
enemies.add(enemy);
}
public void update(){
deltaTime=Gdx.graphics.getDeltaTime();
enemyIterator=enemies.iterator();
timer+=1*deltaTime;
elapsedTime+=1*deltaTime;
playerSpr.setPosition(position.x,position.y);
playerSpr.setSize(size.x,size.y);
bounds.set(position.x,position.y,size.x,size.y);
}
public void movement(){
if(position.y<=910){
position.y-=600*deltaTime;
}
}
/* wan't to draw the next sprite every second, for now I only have to stored in the iterator */
public void draw(){
if(timer>=1){
Sprite cur=enemyIterator.next();
batch.begin();
cur.draw(batch);
batch.end();timer=0;
}
only the playerspr is being rendered for less that as second every second,I know be cause the timer is being reset every second but how do you spawn or draw the next one?
You are conflating your game logic with your drawing. All you should be doing in your draw method is drawing all your stuff, so you should just go through your list of enemies and draw each one:
public void draw(SpriteBatch batch){
for (Sprite enemy : enemies)
enemy.draw(batch);
}
Note that this class should not own or even reference a sprite batch, and should not call begin or end. The Game or Screen class that owns this class should call begin and end so they are only called once for the whole game.
And you want to spawn an enemy each second, which is something that should be done in your update method. Right after you increment your timer (the timer+=1*deltaTime; line), you can spawn an enemy by creating one and adding it to your list:
while (timer >= 1f) {
createNewEnemy();
timer -= 1f; //don't simply reset to 0, or you will be rounding off every time a new one is spawned, so the rate won't be consistent over time.
}
(The reason for using while instead of if is because theoretically, you might want to spawn enemies more often than your max frame time. This prevents your game logic from falling behind if there is a slow frame somewhere.)
The createNewEnemy method should get a sprite, set it up, and add it to the enemies list. It could simply instantiate a new sprite new Sprite(enemy);, which creates a new Sprite and seeds it with the existing enemy prototype sprite you have. But if you use pooling, you can avoid triggering the GC, which will cause stuttering in your game:
private void newEnemy(){
Sprite newEnemy = Pools.obtain(Sprite.class);
newEnemy.set(enemy); //reference same Texture from your prototype enemy sprite
//set up enemy position and other parameters here
enemies.add(newEnemy);
}
If you use pooling, then later when you want to remove an enemy, you should do it like this:
private void removeEnemy(Enemy removedEnemy){
Pools.free(removedEnemy);
enemies.remove(removedEnemy);
}
Hopefully this gets you on the right track, although your game structure seems a bit confused (like why is this class called Player if it contains both the player and all the enemies?).

Camera moving along with the background out of sync in LIBGDX

I have a sprite that I move around the world; it has its velocity increased based on a constant acceleration when the user touches the screen. I also move the camera with this sprite; but I also have a BG image that needs to stay still on the screen so the camera moves with the exact same velocity and acceleration as the background image so that it seems that the background is still. However, I see a slight out of sync between the camera and the background image; the background should stay still but it doesn't :
#Override
public void render () {
batch.setProjectionMatrix(camera.combined);
world_velocity += acceleration;
back.setPosition(back.getX(), back.getY() + world_velocity);
camera.position.y += world_velocity;
camera.update();
}
How can I sync the BG image with the camera so that the BG image stays still? NOTE : If I use constant velocity (no updates based on acceleration ) there is no issue.

LibGdx How to repeat background?

Few days ago I figured out how to do some scrolling in LibGdx. Now I'm triying to do something related. I want to repeat the background. My scrolling follows a ship (Is an s[ace ship game). In the background there is a space photo loaded as a Texture. When the ship reach the end of the backgorund, It keeps going and there's no background anymore. I have read about wrap but I don't really understand How It works. I did that:
px=new Pixmap(Gdx.files.internal("fondo.jpg"));
background=new Texture(px);
background.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
And then, in my render method
spriteBatch.begin();
spriteBatch.draw(background,0,0,500,50);
drawShip();
spriteBatch.end();
Of course It doesn't work, It only draws the background once. I don't know how make this wrap method work. Any help?
SOLUTION
I figured It out. It's not a nice code but It works.
First I declare two Textures with the same image
bck1=new Texture(Gdx.files.internal("fondo.jpg"));
bck2=new Texture(Gdx.files.internal("fondo.jpg"));
Also I declare two variables like this to specify the X value of the position of each bck
int posXBck1=0,posXBck2=0;
Then I use that in Render()
public void calculoPosicionFondos(){
posXBck2=posXBck1+ANCHODEFONDO;
if(cam.position.x>=posXBck2+cam.viewportWidth/2){
posXBck1=posXBck2;
}
}
Where:
ANCHODEFONDO is the width of my background
Cam is an OtrhoCam.
So I said that if the cam is in bck2 (wich means that you can't see bck1 anymore) It change positions, giving bck1 de position of bck2 and, in the next render loop, recalculating bck2
Then just paint both bck in your render mode.
Like Teitus said, do not load your texture multiple times, ever! Anyway, you where on the right track with the wrapper:
texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
Now you can just use the draw method with the source location. The source location is the area you choose to draw on the texture.
batch.draw(texture, x, y, srcX, srcY, srcWidth, srcHeight)
To scroll your texture from right to left all you have to do is increase srcX incrementally. So create a int that increments in the update/render method.
int sourceX = 0;
//render() method
//Increment the variable where to draw from on the image.
sourceX += 10;
//Simply draw it using that variable in the srcX.
batch.draw(YourTexture, 0, 0, sourceX, 0, screenWidth, screenHeight);
Because you are wrapping the texture it will wrap/loop and scroll indefinitely. There might be a issue with the sourceX int if the game runs for a very long time because a int can only hold 2147483647. It takes a while but you can fix it by subtracting the image width each time the number goes over the total image width.
Don't to this, please:
bck1=new Texture(Gdx.files.internal("fondo.jpg"));
bck2=new Texture(Gdx.files.internal("fondo.jpg"));
That will load your big background texture twice. That's a complete waste. If you want to keep your solution at least do:
bck1=new Texture(Gdx.files.internal("fondo.jpg"));
bck2=bkg1;
Regarding the texture Wrapping. If your texture is 500px wide, and you draw a 500px sprite, you won't see any repetition. If you want it repeated 2 times, draw it 1000px wide with 0-2 texture coordinates.
I'm not sure how spriteBatch handles the call you posted, you could try that one, or may be use the overload that uses a texture region and set your region manually.
I see this is a pretty old question, but I think there is an easier way to accomplish background scrolling. Just use the Sprite class. Here is a snippet I use for layered background images that scroll from right to left.
public class LevelLayer
{
public float speedScalar = 1;
private List<Sprite> backgroundSprites = new ArrayList<Sprite>();
public LevelLayer()
{
}
public void addSpriteLayer(Texture texture, float startingPointX, float y, int repeats)
{
for (int k = 0; k < repeats; k++)
{
Sprite s = new Sprite(texture);
s.setX(startingPointX + (k*texture.getWidth()));
s.setY(y);
backgroundSprites.add(s);
}
}
public void render(SpriteBatch spriteBatch, float speed)
{
for (Sprite s : backgroundSprites)
{
float delta = s.getX() - (speed * speedScalar);
s.setX(delta);
s.draw(spriteBatch);
}
}
}
Then you can use the same texture or series of textures like so:
someLayer.addSpriteLayer(sideWalkTexture1, 0, 0, 15);
someLayer.addSpriteLayer(sideWalkTexture2, 15 * sideWalkTexture1.getWidth(), 0, 7);
I change background repeating sections randomly in code and make new ones or reset existing sets when they go off screen. All the layers go to a pool and get pulled randomly when a new one is needed.
SOLUTION
I figured It out. It's not a nice code but It works.
First I declare two Textures with the same image
bck1=new Texture(Gdx.files.internal("fondo.jpg"));
bck2=new Texture(Gdx.files.internal("fondo.jpg"));
Also I declare two variables like this to specify the X value of the position of each bck
int posXBck1=0,posXBck2=0;
Then I use that in Render()
public void calculoPosicionFondos(){
posXBck2=posXBck1+ANCHODEFONDO;
if(cam.position.x>=posXBck2+cam.viewportWidth/2){
posXBck1=posXBck2;
}
}
Where:
ANCHODEFONDO is the width of my background
Cam is an OtrhoCam.
So I said that if the cam is in bck2 (wich means that you can't see bck1 anymore) It change positions, giving bck1 de position of bck2 and, in the next render loop, recalculating bck2
Then just draw both bck in your render()

Categories

Resources