object.rotation

Type Number
Object DisplayObject
Library display.*
Revision Release 2024.3703
Keywords rotation, object, rotate
See also object:rotate()

Overview

Sets the rotation of an object in degrees; this rotation is based in the clockwise direction where 0 is directly upward. The rotation occurs around the object's anchor point.

Rotation can also be set using the object:rotate() function.

Gotchas

This cannot be used on a physical body during a collision event. However, your collision handler may set a flag or include a time delay via timer.performWithDelay() so that the action can occur in the next application cycle or later. See the Collision Detection guide for a complete list of which APIs and methods are subject to this rule.

Examples

Explicit Rotation
local rect = display.newRect( 100, 100, 50, 50 )
rect:setFillColor( 1, 1, 1 )

rect.rotation = 45
print( rect.rotation )  -- Prints 45 in the terminal
Transitional Rotation
local rect = display.newRect( 50, 50, 100, 150 )
rect:setFillColor( 1, 0, 0 )
rect.rotation = -45
local reverse = 1

local function rockRect()
    if ( reverse == 0 ) then
        reverse = 1
        transition.to( rect, { rotation=-45, time=500, transition=easing.inOutCubic } )
    else
        reverse = 0
        transition.to( rect, { rotation=45, time=500, transition=easing.inOutCubic } )
    end
end
 
timer.performWithDelay( 600, rockRect, 0 )  -- Repeat forever