Center sprites on top of each other?

Started by
1 comment, last by TheDodo 12 years, 9 months ago
Hello, I am fairly new to programming. I am trying to build an object with multiple sprites on a sprite sheet, with the top layer of sprite being centered on the bottom sprite. I have been able to build an object with multiple sprites, but haven't been able to get it to be centered, and can't seem to find any tutorials on it. I am curious if anyone might post a quick example of how this would be done, it would be highly appreciated.
Advertisement

Hello, I am fairly new to programming. I am trying to build an object with multiple sprites on a sprite sheet, with the top layer of sprite being centered on the bottom sprite. I have been able to build an object with multiple sprites, but haven't been able to get it to be centered, and can't seem to find any tutorials on it. I am curious if anyone might post a quick example of how this would be done, it would be highly appreciated.


if the sprites have the same size or if your sprites origin is at the center its easy, then you just give them the same position.

If on the other hand the sizes are different and the origin is in a corner then you need to adjust the position based on their sizes.

assuming 2 sprite objects sp1 and sp2 with the attributes x,y,width and height and the origin of the sprite and your viewport is in the same corner you do


//place sp2 on top of sp1
sp2.x = sp1.x + (sp1.width-sp2.width)*0.5;
sp2.y = sp1.y + (sp1.height-sp2.height)*0.5;


if the origin is at the bottom left on the sprite and top left on the viewport you change the second line to sp2.y = sp1.y - (sp1.height-sp2.height)*0.5;
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Thank you for your help.

This topic is closed to new replies.

Advertisement