|
|
|
Particle Explosion Effect
Added on 1/17/2001
|
Attach this behavior to a single sprite on the stage. Then open the behavior properties and specify how many copies of the sprite should be created. Click anywhere to trigger explosion
Download PC Source Download Mac Source
on getBehaviorDescription me
txt=""
txt=txt & "This is a particle effects demo. "
txt=txt & "Based on code by Andre LaMothe. "
txt=txt & "Author: adam@papakane.com "
txt=txt & "Site: http://www.papakane.com "
return txt
end
-- Author-defined properties.
property pNumParticles
property pLifeSpan
property pGravity
property pUseFade
property pSound
property pUseMouseScript
-- Other properties.
property pSpr, pMbr
property pParticles
------------------------------------------------------------
-- getPropertyDescriptionList
------------------------------------------------------------
on getPropertyDescriptionList me
--•
if not(the currentSpriteNum) then exit -->
--•
props=[:]
props[#pNumParticles]=[#comment:"Number of particles", #format:#integer, #range:[#min:1, #max:100], #default:50]
props[#pLifeSpan]=[#comment:"Lifespan (milliseconds)", #format:#integer, #range:[#min:0, #max:5000], #default:5000]
props[#pGravity]=[#comment:"Gravity vector", #format:#list, #default:[0,0.5]]
props[#pUseFade]=[#comment:"Fade out", #format:#boolean, #default:false]
props[#pSound]=[#comment:"Sound", #format:#sound, #default:0]
props[#pUseMouseScript]=[#comment:"Steal mouse down script", #format:#boolean, #default:true]
return props
end
------------------------------------------------------------
-- beginSprite
------------------------------------------------------------
on beginSprite me
--•
pSpr=sprite(me.spriteNum)
pMbr=pSpr.member
--•
if pUseMouseScript then
the mouseDownScript="sprite(" & me.spriteNum & ").mouseDown()"
end if
--•
me.mReset()
end
------------------------------------------------------------
-- mReset
------------------------------------------------------------
on mReset me
--•
pParticles=[]
repeat with i=1 to pNumParticles
particle=[:]
particle[#pState]=#dead
particle[#pSpr]=sprite(me.mCreateSprite())
particle[#pStartTime]=0
particle[#pAge]=0
particle[#pX]=0
particle[#pY]=0
particle[#pXV]=0
particle[#pYV]=0
particle[#pLifeSpan]=pLifeSpan
pParticles.add(particle)
end repeat
end
------------------------------------------------------------
-- prepareFrame
------------------------------------------------------------
on prepareFrame me
repeat with i=1 to pParticles.count
-- Bail if this particle is dead.
if pParticles[i].pState=#dead then
pParticles[i].pSpr.loc=point(-1000,-1000)
next repeat -->
end if
-- Add gravity.
pParticles[i].pXV=pParticles[i].pXV + pGravity[1]
pParticles[i].pYV=pParticles[i].pYV + pGravity[2]
-- Otherwise, move particle one step.
pParticles[i].pX=pParticles[i].pX+pParticles[i].pXV
pParticles[i].pY=pParticles[i].pY+pParticles[i].pYV
-- Test if particle is off screen.
if me.mOffstage(point(pParticles[i].pX,pParticles[i].pY)) then
pParticles[i].pState=#dead
next repeat -->
end if
-- Test if particle has died of old age
pParticles[i].pAge=the milliseconds - pParticles[i].pStartTime
if pParticles[i].pAge >= pParticles[i].pLifeSpan then
pParticles[i].pState=#dead
next repeat -->
end if
-- Set ink.
pParticles[i].pSpr.ink=33
-- Set particle fade amount.
if pUseFade then
fadeRatio=float(pParticles[i].pAge) / pParticles[i].pLifeSpan
blendRatio=1 - fadeRatio
curBlend=blendRatio * 100
pParticles[i].pSpr.blend=curBlend
end if
-- Draw particle posiiton.
pParticles[i].pSpr.loc=point(pParticles[i].pX, pParticles[i].pY)
end repeat
end
------------------------------------------------------------
-- exitFrame
------------------------------------------------------------
on exitFrame me
go to the frame
end
------------------------------------------------------------
-- mouseDown
------------------------------------------------------------
on mouseDown me
me.mStart()
end
------------------------------------------------------------
-- mStart
------------------------------------------------------------
on mStart me
-- Bail if any particles are still alive.
repeat with i=1 to pParticles.count
-- Bail if this particle is already alive.
-- if pParticles[i].pState=#alive then exit-->
end repeat
--•
sound(1).play(pSound)
--•
repeat with i=1 to pParticles.count
-- Explosion happens where user clicked.
pParticles[i].pX=the mouseH
pParticles[i].pY=the mouseV
-- Look, it's a new born particle!
pParticles[i].pAge=0
pParticles[i].pStartTime=the milliseconds
-- Compute random trajectory angle (in damn radians).
angle=random(360) * pi/180.0
-- Compute random trajectory velocity.
vel=2 + random(4)
-- Set (and show) initial particle velocity.
pParticles[i].pXV=cos(angle) * vel
pParticles[i].pYV=sin(angle) * vel
-- Start particle on its way.
pParticles[i].pState=#alive
end repeat
end
------------------------------------------------------------
-- mOffstage
-- checks to see if a specified point is offstage.
------------------------------------------------------------
on mOffstage (me, pt)
ptRect=rect(pt[1], pt[2], pt[1]+1, pt[2]+1)
stageRect=(the stage).drawRect
if intersect(ptRect, stageRect) = rect(0,0,0,0) then
return true
else
return false
end if
end
------------------------------------------------------------
-- mCreateSprite
------------------------------------------------------------
on mCreateSprite me
-- Walk through channels 'till we find an open one.
repeat with chan=1 to the lastChannel
if sprite(chan).memberNum=0 then
openChan=chan
exit repeat
end if
end repeat
-- Place member into empty score channel
puppetSprite chan, true
sprite(openChan).forecolor=255
sprite(openChan).loc=point(random(640), random(480))
sprite(openChan).member=pMbr
--•
return openChan
end
|
|