object.anchorX

Type Number
Object DisplayObject
Library display.*
Revision Release 2024.3703
Keywords x, anchor
See also Transforms and Anchors (guide)
Group Programming (guide)
object.anchorY
display.setDefault()

Overview

This property allows you to control the alignment of the object along the x direction.

In general, the anchors of an object control how geometry is positioned relative to the object’s origin. A value of 0 corresponds to left alignment, meaning the left edge of the object is aligned with the origin. A value of 0.5 corresponds to center alignment. A value of 1 corresponds to right alignment.

By default, anchor values must range from 0.0 to 1.0 and the anchor of new objects is set to 0.5, however these defaults can be modified (see below).

Anchor Defaults

Gotchas

When you change an anchor value, the object’s origin does not change. Instead, its geometry moves relative to its origin. See the Transforms and Anchors guide for more information.

Example

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

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

rect2.anchorX = 0
rect2.x = 100
rect2.y = 100