display.newEmitter()

类型 函数
display.*
返回值 EmitterObject
修订 版本 2024.3703
关键词 newEmitter
另请参阅 EmitterObject:start()
EmitterObject:stop()
EmitterObject:pause()

概述

此函数创建一个 EmitterObject,用于显示粒子效果。

重要

发射器发射的粒子除非其生命周期结束,否则**不会**自动移除,即使它们移出屏幕也不会被销毁。如果您正在使用 Composer 进行场景管理,则使用 display.newEmitter() 创建的发射器应插入到 Composer 场景视图(或场景视图的子组)中,以便在场景退出时将其移除。如果您未使用 Composer,则可以使用 object:removeSelf()display.remove() 手动销毁发射器以移除其生成的所有粒子。

注意

在 macOS 上,可以使用 Particle Designer 将发射器设计并输出为JSON 编码的文件。否则,可以将发射器属性作为键值对在 Lua 表中传递给此函数。有关用法详情,请参阅下面的示例。

语法

display.newEmitter( emitterParams [, baseDir] )
emitterParams (必填)

发射器的参数表。有关有效属性列表,请参阅 EmitterObject 文档。

baseDir (可选)

常量 指定粒子发射器图像文件所在的基目录。选项包括 system.ResourceDirectorysystem.DocumentsDirectorysystem.ApplicationSupportDirectorysystem.TemporaryDirectorysystem.CachesDirectory。默认为 system.ResourceDirectory

示例

键值参数
-- Table of emitter parameters
local emitterParams = {
    startColorAlpha = 1,
    startParticleSizeVariance = 53.47,
    startColorGreen = 0.3031555,
    yCoordFlipped = -1,
    blendFuncSource = 770,
    rotatePerSecondVariance = 153.95,
    particleLifespan = 0.7237,
    tangentialAcceleration = -144.74,
    finishColorBlue = 0.3699196,
    finishColorGreen = 0.5443883,
    blendFuncDestination = 1,
    startParticleSize = 50.95,
    startColorRed = 0.8373094,
    textureFileName = "fire.png",
    startColorVarianceAlpha = 1,
    maxParticles = 256,
    finishParticleSize = 64,
    duration = -1,
    finishColorRed = 1,
    maxRadiusVariance = 72.63,
    finishParticleSizeVariance = 64,
    gravityy = -671.05,
    speedVariance = 90.79,
    tangentialAccelVariance = -92.11,
    angleVariance = -142.62,
    angle = -244.11
}

-- Create the emitter
local emitter = display.newEmitter( emitterParams )

-- Center the emitter within the content area
emitter.x = display.contentCenterX
emitter.y = display.contentCenterY
粒子设计器
-- Require the JSON library for decoding purposes
local json = require( "json" )

-- Read the exported Particle Designer file (JSON) into a string
local filePath = system.pathForFile( "fire.json" )
local f = io.open( filePath, "r" )
local emitterData = f:read( "*a" )
f:close()

-- Decode the string
local emitterParams = json.decode( emitterData )

-- Create the emitter with the decoded parameters
local emitter = display.newEmitter( emitterParams )

-- Center the emitter within the content area
emitter.x = display.contentCenterX
emitter.y = display.contentCenterY