/// <summary> /// This function generates a new array of particle sprites to attach to the emitter. /// </summary> /// <param name="graphics">Texture for the particles - can be one square or a spritesheet. Set Multiple to true if it is a spritesheet and it will automatically create the particles as long as each frame on the spritesheet is square</param> /// <param name="Multiple">Whether or not the Texture contains multiple sprites for particles</param> /// <param name="Quantity">The number of particles to generate</param> /// <param name="Rotation">The amount of rotation in degrees per frame, so keep this number low</param> /// <param name="Collide">The collidability of the particle, 1 = Full and 0 = None</param> /// <returns>This FlxEmitter instance (nice for chaining stuff together, if you're into that).</returns> //public FlxEmitter makeParticles(Texture2D graphics, bool Multiple=false, uint Quantity = 50, float Rotation = 1f, float Collide = 0.8f) public FlxEmitter makeParticles(string graphics, uint Quantity = 50, uint BakedRotations = 0, bool Multiple = false, float Collide = 0.8f) { maxSize = Quantity; uint totalFrames = 0; if (Multiple) { FlxSprite sprite = new FlxSprite(); sprite.loadGraphic(graphics,true); totalFrames = sprite.Frames; sprite.destroy(); } uint randomFrame; FlxParticle particle; int i = 0; while(i < Quantity) { particle = new FlxParticle(); /* if(particleClass == null) particle = new FlxParticle(); else particle = new particleClass(); */ if(Multiple) { randomFrame = (uint)(FlxG.random()*totalFrames); if(BakedRotations > 0) particle.loadRotatedGraphic(graphics,BakedRotations,(int)randomFrame); else { particle.loadGraphic(graphics,true); particle.Frame = (int)randomFrame; } } else { if(BakedRotations > 0) particle.loadRotatedGraphic(graphics,BakedRotations); else particle.loadGraphic(graphics); } if(Collide > 0) { particle.Width *= Collide; particle.Height *= Collide; particle.centerOffsets(); } else particle.AllowCollisions = FlxObject.None; particle.Exists = false; add(particle); i++; } return this; }
override public void create() { FlxG.Framerate = 60; //FlxG.flashFramerate = 60; //Here we actually initialize out emitter //The parameters are X Y Size (Maximum number of particles the emitter can store) theEmitter = new FlxEmitter(10, FlxG.height / 2, 200); //Now by default the emitter is going to have some properties set on it and can be used immediately //but we're going to change a few things. //First this emitter is on the side of the screen, and we want to show off the movement of the particles //so lets make them launch to the right. theEmitter.setXSpeed(100, 200); //and lets funnel it a tad theEmitter.setYSpeed( -50, 50); //Let's also make our pixels rebound off surfaces theEmitter.bounce = 0.8f; //Now let's add the emitter to the state. add(theEmitter); //Now it's almost ready to use, but first we need to give it some pixels to spit out! //Lets fill the emitter with some white pixels for (int i = 0; i < theEmitter.maxSize/2; i++) { whitePixel = new FlxParticle(); whitePixel.makeGraphic(2, 2, new Color(0xFF,0xFF,0xFF)); whitePixel.Visible = false; //Make sure the particle doesn't show up at (0, 0) theEmitter.add(whitePixel); whitePixel = new FlxParticle(); whitePixel.makeGraphic(1, 1, new Color(0xFF,0xFF,0xFF)); whitePixel.Visible = false; theEmitter.add(whitePixel); } //Now let's setup some buttons for messing with the emitter. collisionButton = new FlxButton(0, FlxG.height - 22, "Collision", onCollision); add(collisionButton); gravityButton = new FlxButton(80, FlxG.height - 22, "Gravity", onGravity); add(gravityButton); quitButton = new FlxButton(FlxG.width-80, FlxG.height - 22, "Quit", onQuit); add(quitButton); //I'll just leave this here topText = new FlxText(0, 2, FlxG.width, "Welcome"); topText.setAlignment("center"); add(topText); //Lets setup some walls for our pixels to collide against collisionGroup = new FlxGroup(); wall= new FlxSprite(100, (FlxG.height/2)-50); wall.makeGraphic(10, 100, new Color(0xFF,0xFF,0xFF,0x50));//Make it darker - easier on the eyes :) wall.Visible = wall.Solid = false;//Set both the visibility AND the solidity to false, in one go wall.Immovable = true;//Lets make sure the pixels don't push out wall away! (though it does look funny) collisionGroup.add(wall); //Duplicate our wall but this time it's a floor to catch gravity affected particles floor = new FlxSprite(10, 267); floor.makeGraphic((uint)FlxG.width - 20, 10, new Color(0xFF,0xFF,0xFF,0x50)); floor.Visible = floor.Solid = false; floor.Immovable = true; collisionGroup.add(floor); //Please note that this demo makes the walls themselves not collide, for the sake of simplicity. //Normally you would make the particles have solid = true or false to make them collide or not on creation, //because in a normal environment your particles probably aren't going to change solidity at a mouse //click. If they did, you would probably be better suited with emitter.setAll("solid", true) //I just don't feel that setAll is applicable here(Since I would still have to toggle the walls anyways) //Don't forget to add the group to the state(Like I did :P) add(collisionGroup); //Now lets set our emitter free. //Params: Explode, Particle Lifespan, Emit rate(in seconds) theEmitter.start(false, 3, .01f); //Let's re show the cursors FlxG.mouse.show(); //Mouse.hide(); }
/// <summary> /// This function generates a new array of particle sprites to attach to the emitter. /// </summary> /// <param name="graphics">Texture for the particles - can be one square or a spritesheet. Set Multiple to true if it is a spritesheet and it will automatically create the particles as long as each frame on the spritesheet is square</param> /// <param name="Multiple">Whether or not the Texture contains multiple sprites for particles</param> /// <param name="Quantity">The number of particles to generate</param> /// <param name="Rotation">The amount of rotation in degrees per frame, so keep this number low</param> /// <param name="Collide">The collidability of the particle, 1 = Full and 0 = None</param> /// <returns>This FlxEmitter instance (nice for chaining stuff together, if you're into that).</returns> //public FlxEmitter makeParticles(Texture2D graphics, bool Multiple=false, uint Quantity = 50, float Rotation = 1f, float Collide = 0.8f) public FlxEmitter makeParticles(string graphics, uint Quantity = 50, uint BakedRotations = 0, bool Multiple = false, float Collide = 0.8f) { maxSize = Quantity; uint totalFrames = 0; if (Multiple) { FlxSprite sprite = new FlxSprite(); sprite.loadGraphic(graphics, true); totalFrames = sprite.Frames; sprite.destroy(); } uint randomFrame; FlxParticle particle; int i = 0; while (i < Quantity) { particle = new FlxParticle(); /* * if(particleClass == null) * particle = new FlxParticle(); * else * particle = new particleClass(); */ if (Multiple) { randomFrame = (uint)(FlxG.random() * totalFrames); if (BakedRotations > 0) { particle.loadRotatedGraphic(graphics, BakedRotations, (int)randomFrame); } else { particle.loadGraphic(graphics, true); particle.Frame = (int)randomFrame; } } else { if (BakedRotations > 0) { particle.loadRotatedGraphic(graphics, BakedRotations); } else { particle.loadGraphic(graphics); } } if (Collide > 0) { particle.Width *= Collide; particle.Height *= Collide; particle.centerOffsets(); } else { particle.AllowCollisions = FlxObject.None; } particle.Exists = false; add(particle); i++; } return(this); }