I want to draw an image over another image. Then I want to rotate the images separately.
- Is it possible?
- How?
Many Thanks
I want to draw an image over another image. Then I want to rotate the images separately.
Many Thanks
It is possible to draw image one over the other. You can do draw as many image as you want one over the other.
Basically when you draw the image you need to provide positions. If you provide the same positions for multiple images, depending on the order of the images being drawn, they will be drawn one over the other.
So let's say you have a Tank and its's cannon as separate images. What you will do is draw the tank on the screen. Now you need the cannon image with transparent background and same dimensions as the tank(can be different dimensions) and draw on the same position. That will give an illusion that the cannon is attached to the Tank. Now u can rotate these 2 images separately.
Below is an example :
Sprite tankSprite = new Sprite(new Texture("tank.png"));
Sprite turretSprite = new Sprite(new Texture("turret.png"));
//Set the rotations
tankSprite.setRotation(angle);
turretSprite.setRotation(angle);
//Set the positions
tankSprite.setPosition(x, y);
turretSprite.setPosition(x, y);
//Draw the sprites, using spritebatch
tankSprite.draw(batch); // Drawn first
turretSprite.draw(batch); //Drawn over tank
Let's say this is tank body -
Let's say this is the turret (cannon) -
Now this is the result (ignore the background)-
I have rotated the turret to 90 degrees and tank is at 0 degrees. You can make changes accordingly.