-1

I want to draw an image over another image. Then I want to rotate the images separately.

  1. Is it possible?
  2. How?

Many Thanks

Sneh
  • 3,527
  • 2
  • 19
  • 37

1 Answers1

3

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 -

The tank

Let's say this is the turret (cannon) -

enter image description here

Now this is the result (ignore the background)-

enter image description here

I have rotated the turret to 90 degrees and tank is at 0 degrees. You can make changes accordingly.

Sneh
  • 3,527
  • 2
  • 19
  • 37
  • Why the down vote? Please do comment when you down vote. – Sneh Jan 18 '16 at 19:14
  • Think should be more 20 canons in deffrent angle place on the tank. And You need to move the Tank and rotate it So all the 20 cannos will move and rotate speratly, I thought there is simple way to edit each canon rotate and when the tank move all the canons with the tank are one unit like one image – jaspreetblue Jan 19 '16 at 12:45
  • There can be many different ways to do it but in the end you will have to keep track of each of your cannon. I like this approach more because you have more control over each individual cannon and you can animate or do whatever you want on each image separately. If you have one single image that will be very complex to play around with. – Sneh Jan 19 '16 at 12:48
  • @jaspreetblue I have mail but I will not be able to help give it a try and post questions about what you don't understand :) – Sneh Jan 19 '16 at 12:53