Fork me on GitHub

Explode Image

Flash required: You need version 10 or later of the free Flash player from Adobe to use this content. To download and install the free player from Adobe's web site click here.

Here we use one of Flint’s utility functions to create many small particles from a single image. Then, by applying the Explode activity at the location of the mouse click, we blow the image apart.

package
{
  import org.flintparticles.common.events.EmitterEvent;
  import org.flintparticles.common.particles.Particle;
  import org.flintparticles.twoD.actions.DeathZone;
  import org.flintparticles.twoD.actions.Explosion;
  import org.flintparticles.twoD.actions.Move;
  import org.flintparticles.twoD.emitters.Emitter2D;
  import org.flintparticles.twoD.particles.Particle2DUtils;
  import org.flintparticles.twoD.renderers.DisplayObjectRenderer;
  import org.flintparticles.twoD.zones.RectangleZone;

  import flash.display.BitmapData;
  import flash.display.Sprite;
  import flash.events.MouseEvent;
  import flash.geom.Point;

  [SWF(width='500', height='350', frameRate='60', backgroundColor='#000000')]
  
  public class Main extends Sprite
  {
    private var emitter:Emitter2D;
    private var bitmap:BitmapData;
    private var renderer:DisplayObjectRenderer;
    private var explosion:Explosion;
    
    public function Main()
    {
      bitmap = new Image1( 384, 255 );
      
      emitter = new Emitter2D();
      emitter.addAction( new DeathZone( new RectangleZone( -5, -5, 505, 355 ), true ) );
      emitter.addAction( new Move() );
      prepare();
      
      renderer = new DisplayObjectRenderer();
      addChild( renderer );
      renderer.addEmitter( emitter );
      emitter.start();
      
      stage.addEventListener( MouseEvent.CLICK, explode, false, 0, true );
      emitter.addEventListener( EmitterEvent.EMITTER_EMPTY, prepare );
    }
    
    private function prepare( event:EmitterEvent = null ):void
    {
      if( explosion )
      {
        emitter.removeAction( explosion );
        explosion = null;
      }
      var particles:Vector.<Particle> = Particle2DUtils.createRectangleParticlesFromBitmapData( bitmap, 8, emitter.particleFactory, 56, 47 );
      emitter.addParticles( particles, false );
    }
    
    private function explode( ev:MouseEvent ):void
    {
      if( !explosion )
      {
        var p:Point = renderer.globalToLocal( new Point( ev.stageX, ev.stageY ) );
        explosion = new Explosion( 8, p.x, p.y, 500 );
        emitter.addAction( explosion );
      }
    }
  }
}
import org.flintparticles.common.events.EmitterEvent;
import org.flintparticles.common.particles.Particle;
import org.flintparticles.twoD.actions.DeathZone;
import org.flintparticles.twoD.actions.Explosion;
import org.flintparticles.twoD.actions.Move;
import org.flintparticles.twoD.emitters.Emitter2D;
import org.flintparticles.twoD.particles.Particle2DUtils;
import org.flintparticles.twoD.renderers.DisplayObjectRenderer;
import org.flintparticles.twoD.zones.RectangleZone;

var explosion:Explosion;
var bitmap:BitmapData = new Image1( 384, 255 );

var emitter:Emitter2D = new Emitter2D();
emitter.addAction( new Move() );
emitter.addAction( new DeathZone( new RectangleZone( -10, -10, 510, 360 ), true ) );
prepare();

var renderer:DisplayObjectRenderer = new DisplayObjectRenderer();
addChild( renderer );
renderer.addEmitter( emitter );
emitter.start();

stage.addEventListener( MouseEvent.CLICK, explode, false, 0, true );
emitter.addEventListener( EmitterEvent.EMITTER_EMPTY, prepare );

function prepare( event:EmitterEvent = null ):void
{
  if( explosion )
  {
    emitter.removeAction( explosion );
    explosion = null;
  }
  var particles:Vector.<Particle> = Particle2DUtils.createRectangleParticlesFromBitmapData( bitmap, 8, emitter.particleFactory, 56, 47 );
  emitter.addParticles( particles, false );
}
    
function explode( ev:MouseEvent ):void
{
  if( !explosion )
  {
    var p:Point = renderer.globalToLocal( new Point( ev.stageX, ev.stageY ) );
    explosion = new Explosion( 8, p.x, p.y, 500 );
    emitter.addAction( explosion );
  }
}
<?xml version="1.0" encoding="utf-8"?>
<s:Application
  xmlns:fx="http://ns.adobe.com/mxml/2009" 
  xmlns:s="library://ns.adobe.com/flex/spark" 
  xmlns:f="http://flintparticles.org/2009/flint2d"
  width="500" height="350"
  backgroundColor="#000000" creationComplete="complete()">
  
<fx:Script>
  <![CDATA[
    import org.flintparticles.common.events.EmitterEvent;
    import org.flintparticles.common.particles.Particle;
    import org.flintparticles.twoD.actions.Explosion;
    import org.flintparticles.twoD.particles.Particle2DUtils;
    
    private var explosion:Explosion;
    private var bitmap:BitmapData;

    private function complete():void
    {
      bitmap = new Image1( 384, 255 );
      prepare();
      addEventListener( MouseEvent.CLICK, explode, false, 0, true );
      emitter.addEventListener( EmitterEvent.EMITTER_EMPTY, prepare );
    }
    
    private function prepare( event:EmitterEvent = null ):void
    {
      if( explosion )
      {
        emitter.removeAction( explosion );
        explosion = null;
      }
      var particles:Vector.<Particle> = Particle2DUtils.createRectangleParticlesFromBitmapData( bitmap, 8, emitter.particleFactory, 56, 47 );
      emitter.addParticles( particles, false );
    }

    private function explode( ev:MouseEvent ):void
    {
      if( !explosion )
      {
        var p:Point = renderer.globalToLocal( new Point( ev.stageX, ev.stageY ) );
        explosion = new Explosion( 8, p.x, p.y, 500 );
        emitter.addAction( explosion );
      }
    }
  ]]>
</fx:Script>

<f:DisplayObjectRenderer id="renderer" width="500" height="350">
  <f:emitters>
    <f:Emitter id="emitter" autoStart="true">
      <f:actions>
        <f:Move/>
        <f:DeathZone zoneIsSafe="true">
          <f:RectangleZone left="-5" right="505" top="-5" bottom="355"/>
        </f:DeathZone>
      </f:actions>
    </f:Emitter>
  </f:emitters>
</f:DisplayObjectRenderer>
</s:Application>