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:
1 2 3 | local EggSpriteSheet = Sprite.newSpriteSheet("gfx/level.png", 75, 85); local sprEgg = Sprite.newSprite(Sprite.newSpriteSet(EggSpriteSheet, 1, 2)); sprEgg.width = EGG_SIZE; sprEgg.height = EGG_SIZE; |
try
EggSpriteSheet:scale(xScale,yScale)
Thank you very much! It works.
scale works, yes.
but the parameters are in fact percentages.
EggSpriteSheet:scale(0.5,0.5) -- half...
EggSpriteSheet:scale(2,2) -- double size
but I want to set the width and height directly to let's say 200 pixels
sprite.width = 200 -- not working.
is this possible in some other way?
-finefin
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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 ) |