Hi, I’ve been learning Flashandmath’s basic bitmap particle system and I’m trying to make particles with movement of the mouse. A problem I’m having right now is that whenever I move the mouse, the particles are immediately erased from the bitmap and they are re-created at the mouse coordinate. I’m thinking that because I run createParticles() every mouseMove, the particles are created, but isn’t that what the update() from the timer is for (see code below). Thanks. I’ve also looked at other tutorials from flashandmath that has mouse interactivity but they all use Sprite, so they’d be different from bitmap I guess.
package
{
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.utils.*;
import flash.geom.*;
import Particles2D; //from flashandmath
import BasicInfo;
public class BitmapParticlesMouse extends MovieClip
{
private var display:Sprite;
private var displayWidth:uint;
private var displayHeight:uint;
private var back:Shape;
private var timer:Timer;
private var timerStarted:Boolean;
private var bitmap:Bitmap;
private var bmpData:BitmapData;
private var darken:ColorTransform;
private var numParticles:uint;
private var firstParticle:Particles2D;
private var skipTimer:Boolean;
public function BitmapParticlesMouse()
{
skipTimer = false;
addChild(new BasicInfo());
display = new Sprite();
displayWidth = stage.stageWidth - 20;
displayHeight = stage.stageWidth - 10;
display.x = 10;
display.y = 100;
addChild(display);
numParticles = 1000;
darken = new ColorTransform(0.9,0.9,0.9,0.33,0,0,0,0);
drawBack(0x000000);
//'back' is put underneath 'bitmap'
display.addChild(back);
display.addChild(bitmap);
display.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
display.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
display.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
timer = new Timer(40);
timer.addEventListener(TimerEvent.TIMER, update);
//timer is start in the first touchBegin to prevent any undefined values
timerStarted = false;
}
private function mouseDownHandler(e:MouseEvent):void
{
if (! timerStarted)
{
timer.start();
timerStarted = true;
}
}
private function mouseUpHandler(e:MouseEvent):void
{
touching = false;
}
private function mouseMoveHandler(e:MouseEvent):void
{
createParticles(display.mouseX, display.mouseY);
}
private function update(e:TimerEvent):void
{
var p:Particles2D = firstParticle;
bmpData.lock();
bmpData.colorTransform(bmpData.rect, darken);
do
{
p.vel.x = Math.random() * 2 - 1;
p.vel.y = 1;
p.acc.x = 0;
p.acc.y = 0.98;
p.vel = p.vel.add(p.acc);
p.x += p.vel.x;
p.y += p.vel.y;
bmpData.setPixel32(p.x, p.y, p.color);
p = p.next;
} while (p != null);
bmpData.unlock();
}
private function drawBack(color:uint):void
{
back = new Shape();
back.graphics.beginFill(color, 1);
back.graphics.lineStyle(2, 0xFFFFFF, 1);
back.graphics.drawRect(0,0,displayWidth,displayHeight);
back.graphics.endFill();
back.cacheAsBitmap = true;
}
//Within 'createParticles', a list of particles: firstParticle and its 'next'
//Each particle is assigned with a 32-bit 0xAARRGGBB color with alpha 255 (0xFF)
//and a white (0xFFFFFF) RGB. This means all particles will have an opaque white color
//Particle's x, y position is passed where the mouse is.
private function createParticles(xCoor:Number, yCoor:Number):void
{
var color:uint = 0xFFFFFFFF;
var lastParticle:Particles2D;
var i:uint;
for (i = 0; i < numParticles; i++)
{
var thisP:Particles2D = new Particles2D(color);
thisP.x = xCoor;
thisP.y = yCoor;
//This is the process of creating a list of particles using the 'next' property
//in Particles2D. This thing just work.
if (i == 0)
{
firstParticle = thisP;
//trace("firstParticle:" + firstParticle);
}
else
{
lastParticle.next = thisP;
}
lastParticle = thisP;
}
}
}
}