I use Sprite (generated from SpriteSet, SpriteSet is generated from SpriteSheet) and want to change its size. I tried assinging its width, height, contentWidth, contentHeight , but nothing works. Please help me. Here is my code:
For some reason, no.
So you need to be mindful of the original sprite size.
If your sprite starts at 100, and you want it to be 200, you must set a scale of
newsize/originalsize
eg 200/100 = scale of 2
if you want 46 pixels, its 46/100 = 0.46
You need to be prepared to accept that in some cases, the maths will generate ‘half a pixel’ in which case it is hard to predict the exact size you will get. This can lead to one pixel gaps between objects placed next to each other.
[import]uid: 108660 topic_id: 18744 reply_id: 86201[/import]
yeah thanks. a little bit of math changes everything
here’s a small demo I made for testing your formula.
In fact I was trying to set the original size on an scaled object.
In that case it’s originalsize / newsize of course.
local boxOrig = display.newRect( 120, 30, 15, 100 )
boxOrig:setFillColor ( 155, 155, 155 )
local box = display.newRect( 120, 50, 15, 3 )
local boxOrigWidth = box.contentWidth
local function grow ()
local randomScale = 1 + (math.random (10) / 10)
box:scale ( randomScale, 1 )
end
local function setToOriginalSize ()
local boxNewWidth = box.contentWidth
local scaleFactor = ( boxOrigWidth / boxNewWidth )
print ("boxOrigWidth: "..boxOrigWidth)
print ("boxNewWidth: "..boxNewWidth)
print ("scaleFactor: "..scaleFactor)
box:scale ( scaleFactor, 1 )
end
timer.performWithDelay ( 100, grow, 10 )
timer.performWithDelay ( 1500, setToOriginalSize, 1 )
calculating the scale factor to set an object to original size is a bit convoluted, but at least it works