Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

And again about destroying particle emitters
Started by zombiexp2010 Mar 24 2020 11:09 AM

2 replies to this topic

Best Answer XeduR @Spyric , 24 March 2020 - 11:37 AM

The particles emitted will be destroyed once their lifetime ends or once you remove the emitter, whichever comes first.

 

I'm guessing that your problem has to do with two things. First, you have a transition with a time of 250ms after which the emitter will be removed, i.e. pretty instant. Secondly, you are using a tap listener, which means that if you accidentally move your finger/cursor on the screen even by a pixel, then it becomes a touch event, i.e. a tap event won't fire.

 

You can make these adjustments and see that everything should be working as expected.

local function tapEvent( event )
    if event.phase == "began" then
        local emitter = display.newEmitter( emitterParams )
        emitter.x, emitter.y = event.x, event.y
        -- Using time of 5000 to make this extra clear.
        transition.moveTo( emitter, { x=event.x, y=event.y - 200, time=5000, onComplete=function() emitter:removeSelf() emitter = nil end} )
    end
end

-- Replace tap with touch.
Runtime:addEventListener("touch", tapEvent)

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

zombiexp2010

[GLOBAL: userInfoPane.html]
zombiexp2010
  • Enthusiast

  • 63 posts
  • Corona SDK

Hi guys! 

 

Here we have a piece of code, where tap on the screen creates new warping up particle emitter under your finger:

 

 

 

-- Table of emitter parameters
local emitterParams = {
    startColorAlpha = 1,
    startParticleSizeVariance = 0,
    startColorGreen = 1,
    yCoordFlipped = -1,
    blendFuncSource = 768,
    rotatePerSecondVariance = 0,
    particleLifespan = 0.1,
    tangentialAcceleration = 0,
    finishColorBlue = 1,
    finishColorGreen = 1,
    blendFuncDestination = 769,
    startParticleSize = 512,
    startColorRed = 1,
    textureFileName = "SomeImage.png",
    startColorVarianceAlpha = 1,
    maxParticles = 10,
    finishParticleSize = 512,
    duration = 0.25,
    finishColorRed = 1,
    maxRadiusVariance = 0,
    finishParticleSizeVariance = 0,
    gravityy = 200,
    speedVariance = 0,
    tangentialAccelVariance = 0,
    angleVariance = 0,
    angle = 0
}
 
local function tapEvent( event )
    local emitter = display.newEmitter( emitterParams )
    emitter.x, emitter.y = event.x, event.y
    transition.moveTo( emitter, { x=event.x, y=event.y - 200, time=250, onComplete=function() emitter:removeSelf() emitter = nil end} )
end

Runtime:addEventListener("tap", tapEvent)

 

I'm not clearly understand: is the entire emitter destroyed after it's life duration or only generated particles are destroyed? 

I've tested this by printing emitter variable and the result was address of emitter table. This means, emitter still alive after it's «duration». Just invisible, but alive.

 

As already was answered in https://forums.coronalabs.com/topic/58503-best-practice-for-removing-particle-objects/ post:

However, that will NOT destroy the emitter object... if you don't need it anymore (no need for new particles) then you SHOULD remove it outright in your code.
For this, I would suggest that you run a timer simultaneously, and when the timer is completed, you ...(remove)... the emitter object.

 

You can test the code above and see, that if you are tapping fast enough (~2-3 taps per second) only first emitter works well while other new emitters will be generating only few particles from many or generating nothing at all. If you will wait a second after last emitter ended the new one will be working fine once again.

But all works fine and all « fast-tap-created» emitters will work fine without emitter-removing code. 

 

E-e-em-m... What need to be changed in code to make this simple work? Any help, please?



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,246 posts
  • Corona SDK

  Best Answer

The particles emitted will be destroyed once their lifetime ends or once you remove the emitter, whichever comes first.

 

I'm guessing that your problem has to do with two things. First, you have a transition with a time of 250ms after which the emitter will be removed, i.e. pretty instant. Secondly, you are using a tap listener, which means that if you accidentally move your finger/cursor on the screen even by a pixel, then it becomes a touch event, i.e. a tap event won't fire.

 

You can make these adjustments and see that everything should be working as expected.

local function tapEvent( event )
    if event.phase == "began" then
        local emitter = display.newEmitter( emitterParams )
        emitter.x, emitter.y = event.x, event.y
        -- Using time of 5000 to make this extra clear.
        transition.moveTo( emitter, { x=event.x, y=event.y - 200, time=5000, onComplete=function() emitter:removeSelf() emitter = nil end} )
    end
end

-- Replace tap with touch.
Runtime:addEventListener("touch", tapEvent)


  • zombiexp2010 likes this

[TOPIC: post.html]
#3

zombiexp2010

[GLOBAL: userInfoPane.html]
zombiexp2010
  • Enthusiast

  • 63 posts
  • Corona SDK

Tap event still buggy even placing cursor in simulator automatically with use of WinAPI mouse_event two times per second in random point without changing it's position while tap goes.

But works well with touch event!

 

Thank you XeduR @Spyric!




[topic_controls]
[/topic_controls]