Cannot set size (width and height) for Sprites

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:

[lua]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;[/lua]

The Sprite only take the original size from the image. [import]uid: 103204 topic_id: 18744 reply_id: 318744[/import]

try
[lua]EggSpriteSheet:scale(xScale,yScale)[/lua] [import]uid: 64174 topic_id: 18744 reply_id: 72129[/import]

Thank you very much! It works. [import]uid: 103204 topic_id: 18744 reply_id: 72165[/import]

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 [import]uid: 70635 topic_id: 18744 reply_id: 86188[/import]

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 :wink:

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 :wink:

  • finefin [import]uid: 70635 topic_id: 18744 reply_id: 86285[/import]