object.alpha

Type Number
Object DisplayObject
Library display.*
Revision Release 2024.3703
Keywords opacity, transparent, opaque

Overview

This property represents the alpha value of a display object. Use it to set or retrieve the object’s opacity. A value of 0 is transparent and 1.0 is fully opaque.

This property is often used in the transition.to() function to fade in/out an object over time.

Gotchas

The alpha value set by object:setFillColor only applies to the color/tint portion of the object, while object.alpha controls the overall opacity of the object, including the fill color/image. The fill alpha is the object.alpha value multiplied by the object:setFillColor() alpha value. Consider the following examples:

Also note that an object may consist of both the fill color/tint and the stroke. In this case, object.alpha controls the opacity of both portions, while object:setFillColor only controls the fill color/tint.

Examples

local rect1 = display.newRect( 100, 100, 50, 50 )
rect1:setFillColor( 1, 0, 0 )

local rect2 = display.newRect( 125, 125, 50, 50 )
rect2:setFillColor( 1, 1, 1 )
rect2.alpha = 0.5
local object = display.newImage( "image.png" )
object.alpha = 0.75  -- set to 75% opacity