前一篇已大致說明 flint 粒子系統原理,
emitter -> particle -> renderer
接下來實作雪花效果,如圖
程式碼說明寫得很清楚,就不多寫了,
如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.geom.Point; | |
import flash.geom.Rectangle; | |
import net.hires.debug.Stats; | |
import org.flintparticles.common.counters.Steady; | |
import org.flintparticles.common.displayObjects.RadialDot; | |
import org.flintparticles.common.initializers.ImageClass; | |
import org.flintparticles.common.initializers.ScaleImageInit; | |
import org.flintparticles.twoD.actions.RandomDrift; | |
import org.flintparticles.twoD.actions.DeathZone; | |
import org.flintparticles.twoD.actions.Move; | |
import org.flintparticles.twoD.emitters.Emitter2D; | |
import org.flintparticles.twoD.initializers.Position; | |
import org.flintparticles.twoD.initializers.Velocity; | |
import org.flintparticles.twoD.renderers.BitmapRenderer; | |
import org.flintparticles.twoD.renderers.DisplayObjectRenderer; | |
import org.flintparticles.twoD.renderers.PixelRenderer; | |
import org.flintparticles.twoD.zones.LineZone; | |
import org.flintparticles.twoD.zones.PointZone; | |
import org.flintparticles.twoD.zones.RectangleZone; | |
public class Main extends Sprite | |
{ | |
private var emitter:Emitter2D; | |
public function Main():void | |
{ | |
if (stage) | |
init(); | |
else | |
addEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
private function init(e:Event = null):void | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, init); | |
// entry point | |
// stats | |
addChild(new Stats); | |
// emiiter | |
emitter = new Emitter2D(); | |
// counter,每秒產生 50 個粒子 | |
emitter.counter = new Steady(50); | |
// particle init, set up the particle’s position, velocity, image, color, lifetime. | |
// image | |
var imageClass:ImageClass = new ImageClass(RadialDot, [2]); | |
emitter.addInitializer(imageClass); | |
// position | |
var position:Position = new Position(new LineZone(new Point(-5, -5), new Point(1005, -5))); | |
emitter.addInitializer(position); | |
// velocity,向下 | |
var velocity:Velocity = new Velocity(new PointZone(new Point(0, 60))); | |
emitter.addInitializer(velocity); | |
// scale | |
var scaleImage:ScaleImageInit = new ScaleImageInit(0.75, 2); | |
emitter.addInitializer(scaleImage); | |
// action | |
emitter.addAction(new Move); | |
// random drift action | |
var rd:RandomDrift = new RandomDrift( 20, 20 ) | |
emitter.addAction(rd); | |
// remove particle,進入 deadthZone 區域就移除粒子 | |
var dzone:RectangleZone = new RectangleZone( -10, -10, 1020, 605 ); | |
var deathZone:DeathZone = new DeathZone( dzone, true ); | |
emitter.addAction( deathZone ); | |
// render | |
var renderer:DisplayObjectRenderer = new DisplayObjectRenderer(); | |
addChild(renderer); | |
renderer.addEmitter(emitter); | |
emitter.start(); | |
// 快轉十秒,讓雪花先填滿螢幕 | |
emitter.runAhead(10); | |
} | |
} | |
} |
沒有留言:
張貼留言