object.width

Description:

Retrieve or change the width of a display object. It's possible to use this to set the width of objects, with the exception of text objects.

On display.newLine objects, this sets the stroke width of the line.

For images, the returned parameter is the original bit map width.

This parameter is unaffected by object:scale() or object.xScale. (See object.contentWidth.)

Syntax:

local var = object.width
object.width = value

Example:

local rect1 = display.newRect(100, 100, 50, 50)
rect1:setFillColor(170, 170, 170)
 
local rect2 = display.newRect(100, 100, 50, 50)
rect2:setFillColor(255, 255, 255)
rect2.x = rect1.x + rect1.width + 1

Returns:

number: Width value of the object

Note: Setting a new height or width of a display object will always expand or shrink the object from the center no matter what display.setReferencePoint value is set. Case #677/1222

Remarks:

The value is not affected by object scaling or rotation.

Supported on operating systems and platforms for build numbers shown:
  • Mac OS X:
    Corona SDK 1.0
  • Windows:
    Corona SDK 2.0
  • iOS:
    Corona SDK 1.0
  • Android:
    Corona SDK 2.0

Replies

open768
User offline. Last seen 27 weeks 5 days ago. Offline
Joined: 6 Jul 2011

note: setting width and height doesnt scale graphics, rather changes the size of the bounding box and effectively crops the image.

use xscale and yscale to scale the graphic rather than crop it.

RicardoGraca
User offline. Last seen 1 day 4 hours ago. Offline
Joined: 17 May 2011

@open768 It can actually be a useful feature, if it is indeed a feature. We can use it to crop images (as you mentioned) and to simulate padding, which is something that I have been missing for some time until I read your comment :)