display.newPolygon()

Type Function
Library display.*
Return value ShapeObject
Revision Current Public Release (2014.2381)
Keywords shape, polygon, vector
See also Shapes — Paths, Fills, Strokes

Overview

Draw a polygon shape by providing the outline (contour) of the shape. This includes convex or concave shapes. Self-intersecting shapes, however, are not supported and will result in undefined behavior.

Syntax

display.newPolygon( x, y, vertices )
display.newPolygon( parent, x, y, vertices )
parent (optional)

GroupObject. An optional display group in which to insert the polygon.

x / y (required)

Numbers. The location of the object relative to its parent.

vertices (required)

Array. An array of x and y coordinates. These coordinates will automatically be re-centered about the center of the polygon.

Gotchas

If an invalid polygon is requested, for example a polygon with intersecting lines, it may not render properly. For correct results, specify valid polygon coordinates.

Example

local halfW = display.contentWidth * 0.5
local halfH = display.contentHeight * 0.5

local vertices = { 0,-110, 27,-35, 105,-35, 43,16, 65,90, 0,45, -65,90, -43,15, -105,-35, -27,-35, }

local o = display.newPolygon( halfW, halfH, vertices )
o.fill = { type="image", filename="mountains.png" }
o.strokeWidth = 10
o:setStrokeColor( 1, 0, 0 )