Fork me on GitHub
Not signed in (Sign In)

Welcome, Guest

Want to take part in these discussions? Sign in if you have an account, or apply for one below

    • CommentAuthorven
    • CommentTimeJan 4th 2011 edited
     
    I'm working on a puzzle game that has an 8x8 grid of MovieClip objects. When the game is over I wanted to explode the objects from the center of the board. Here is the working code; in your FLA file you'll need to create a MovieClip and export it as Jewel or modify the code to accept an array of MovieClip objects.

    -Ven


    package
    {
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.geom.Point;
    import flash.events.*;

    import org.flintparticles.twoD.actions.*;
    import org.flintparticles.twoD.emitters.Emitter2D;
    import org.flintparticles.twoD.initializers.*;
    import org.flintparticles.twoD.renderers.*;

    import org.flintparticles.twoD.particles.Particle2DUtils;

    public class FlintExpl extends MovieClip
    {
    var emitter:Emitter2D = new Emitter2D();

    function FlintExpl()
    {
    // setup display
    init();

    // wait for click
    stage.addEventListener( MouseEvent.CLICK, mouseClick );
    }

    // handle mouse click, start emitter when mouse is clicked
    function mouseClick( e:MouseEvent )
    {
    emitter.start();
    }

    // create a grid of blocks (jewels) 8x8 and then setup for explosion in the middle of grid
    function init()
    {
    // here we create an array of our movieclip objects to be use as particles, ideally this would
    // already be filled before passing to the explosion handler.
    var jewels:Array = new Array();

    // setup for grid
    const INIT_X = 100; // grid x,y corner point
    const INIT_Y = 100;

    var ix:int = INIT_X; // used in setting starting points for grid rows, cols
    var iy:int = INIT_Y;

    var height:int = 20; // dimensions of object
    var width:int = 20;
    const SPC = 3; // spacing between objects

    for ( var i:int = 0; i < 8; i++ )
    {
    for ( var j:int = 0; j < 8; j++ )
    {
    var obj:Jewel = new Jewel;

    obj.x = ix + ((SPC + width) * j );
    obj.y = iy;
    jewels.push( obj );
    }
    iy += SPC + height;
    }

    // create particles from our existing objects
    var particles:Array = Particle2DUtils.createParticles2DFromDisplayObjects( jewels );

    // add particles
    emitter.addExistingParticles( particles, false );

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

    // set up the explosion - good to play with explosion strenght, depth and epsilon
    // the object's origin is in the center, so need to offsets in x/y to get to center of grid
    // check Explosion constructor documentation
    emitter.addAction(new Explosion(1,(INIT_X + (width + SPC) * 4) - ((width+SPC)/2), (INIT_Y + (height + SPC) * 4 ) - ((height+SPC)/2), 300, 10)); // set up explosion
    emitter.addAction( new Move() ); // set to objects to move

    // TODO: here we can limit how far out the explosions will go


    // start emitter when mouse is clicked
    // emitter.start();
    }
    }
    }