graphics.newOutline()

类型 函数
graphics.*
返回值
修订 版本 2024.3703
关键词 轮廓, 物理, 刚体, 图形
另请参阅 物理刚体 (指南)
图像表单 (指南)
physics.addBody()

概述

此函数生成从图像文件或图像表单中帧获得的形状轮廓。形状由图像的非透明alpha 值识别。通常,更简单的图像会产生更好的轮廓。

返回值是一个包含内容空间中 **x** 和 **y** 坐标的表,可用作 physics.addBody() 函数的轮廓

语法

图像文件

graphics.newOutline( coarsenessInTexels, imageFileName [, baseDir] )
coarsenessInTexels (必填)

数字. 以纹素为单位的粗糙度。值越高,轮廓分辨率越低。值越低,轮廓越大,可能会影响性能。

imageFile (必填)

字符串. 要跟踪的图像的文件名。

baseDir (可选)

常量. 加载图像的路径。默认为 system.ResourceDirectory(项目文件夹;与 main.lua 相同的位置)。有效值请参阅 system.pathForFile())。

图像表单帧

graphics.newOutline( coarsenessInTexels, imageSheet, frameIndex )
coarsenessInTexels (必填)

数字. 以纹素为单位的粗糙度。值越高,轮廓分辨率越低。值越低,轮廓越大,可能会影响性能。

imageSheet (必填)

图像表单. 使用 graphics.newImageSheet() 创建的图像表单对象的引用。

frameIndex (必填)

数字. 表示图像表单中用于创建对象的帧索引号。仅当指定了 imageSheet 时才需要。

示例

图像文件
local imageFile = "star.png"

local imageOutline = graphics.newOutline( 2, imageFile )
local image = display.newImage( imageFile )

physics.addBody( image, { outline=imageOutline, bounce=0.5, friction=0.1 } )
图像表单帧
local options =
{
    width = 64,
    height = 64,
    numFrames = 12,
    sheetContentWidth = 256,  -- width of original 1x size of entire sheet
    sheetContentHeight = 192  -- height of original 1x size of entire sheet
}
local letterSheet = graphics.newImageSheet( "first_12_letters_of_the_alphabet.png", options )

local frameIndex = 9

local letterOutline = graphics.newOutline( 2, letterSheet, frameIndex )

local letterImage = display.newImageRect( letterSheet, frameIndex, 64, 64 )

physics.addBody( letterImage, { outline=letterOutline, bounce=0.5, friction=0.1 } )