1
votes

I have a Sprite object with a Width and a Height (texture size).

I want to display the sprite on the screen with the same size as the original texture size.

Because the scene size, the camera position, and texture sizes are not constant values I need some way to scale the Sprite.

Most of the time camera is Perspective but some times it can be Orthographic.

So I need 2 formulas for the scale.

I've found some answers on how to make the Sprite size constant when zooming but in this calculation the initial scale is unknown.

Thanks.

1

1 Answers

0
votes

One way that I do something similar to this is by creating an initial(optimal) size of the screen, and scaling the node based on changes to the screen.

The easiest way to do this is to have an initial size of the scene in which your sprite is the exact size you want it. You would call this before doing any zooming and you only call it ONCE:

var initialScreenWidth = self.size.width var initialScreenHeight = self.size.height

Now whenever you change the size of screen(such as zooming), you have to find the scale at which the screen has changed:

var scaleWidth = self.size.width/initialScreenWidth
var scaleHeight = self.size.height/initialScreenHeight 

Now that you have those two scales, all you have to do is:

texture.xScale = scaleWidth
texture.yScale = scaleHeight

You will have to create the scales and set the texture dimensions every single time you change the size of the screen. (Most likely in the Update function so it looks smooth).

Hope this helps!