Center sprites on top of each other?
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.
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;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement