AS2 Basic Steam Particles; requires MC Tween


/ Published in: ActionScript
Save to your folder(s)

This is a CS3/AS2 Flash file containing a simple particle system. It requires the use of MC Tween (http://hosted.zeh.com.br/mctween/).


Copy this code and paste it in your HTML
  1. // This script requires a movieclip in the library exported for actionscript as "particle_mc"
  2.  
  3. #include "mc_tween2.as" // from http://hosted.zeh.com.br/mctween/
  4.  
  5.  
  6. var speed:Number = 1.2; // speed at which each particle rises per cycle
  7. var lifespan:Number = 4000; // in milliseconds
  8. var particleCount:Number = 30; // the system starts slowing down at/above 50. This is just a quick & easy particle system, and AS2 isn't the best for particles, either.
  9. var alphaInitial:Number = 80; // what alpha level each particle is set to upon spawning
  10. var fadeRate:Number = .2; // how much per cycle each particle's alpha drops
  11. var spawnMovieClip:MovieClip; // set this to a movieclip if you want to attach the steam to something
  12. if(spawnMovieClip != undefined) {
  13. var spawnX:Number = spawnMovieClip._x;
  14. var spawnY:Number = spawnMovieClip._y;
  15. } else {
  16. var spawnX:Number = Stage.width/2;
  17. var spawnY:Number = Stage.height/2;
  18. }
  19.  
  20. var wandering:Array = Array(particleCount); // this is a per-particle left-right direction, so that the horizontal movement of each particle is less jumpy
  21.  
  22. var merging:Number = 0; // Don't change this. An incrementing value.
  23. var mergingLoop:Number = 1000; // length of merge cycle
  24. var mergingThreshold:Number = 500; // change this to set the frequency of the tendency for particles to group together. Higher is more frequent, Lower is less.
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. this.onEnterFrame = function() {
  39. for(intI = 0; intI < particleCount; intI++) {
  40. if(this["steam" + intI + "_mc"] == undefined) { // this happens once for each particle
  41. this.attachMovie("particle_mc", "steam" + intI + "_mc", this.getNextHighestDepth(), {_x:-500, _y:-500, _alpha:0});
  42. if(spawnMovieClip != undefined) this["steam" + intI + "_mc"].swapDepths(spawnMovieClip); // slips each particle underneath the attach object, if defined.
  43. var timeout:Number = setTimeout(spawnSteam, randRange(.25*lifespan,.75*lifespan), this["steam" + intI + "_mc"]); // "spawning" each particle really only resets its default properties, so that we can just recyle the movieclips
  44. wandering[intI] = 0;
  45. }
  46.  
  47.  
  48. merging++;
  49. if (merging == mergingLoop) merging = 0;
  50.  
  51.  
  52. wandering[intI] += randRange(-100,100)/100;
  53.  
  54. if (merging < mergingThreshold) {
  55. var thisX:Number = this["steam" + intI + "_mc"]._x;
  56. var prevX:Number = this["steam" + (intI-1) + "_mc"]._x;
  57. var Dif:Number = thisX - prevX;
  58.  
  59. if (Dif > 0) { // if we're currently to the right of the previous particle
  60. if (thisX - prevX > 10) { // if the difference is more than 10 pixels
  61. wandering[intI] = -1; // set the current particle's direction to move left
  62. }
  63. } else {
  64. if (prevX - thisX > 10) {
  65. wandering[intI] = 1;
  66. }
  67. }
  68. }
  69.  
  70.  
  71. // limit each particles horizontal movement to no more than one pixel per cycle
  72. if(wandering[intI] < -1) {wandering[intI] = -1};
  73. if(wandering[intI] > 1) {wandering[intI] = 1};
  74.  
  75.  
  76. // apply the changes to each particle
  77. this["steam" + intI + "_mc"]._x += wandering[intI];//randRange(-2*speed,2*speed);
  78. this["steam" + intI + "_mc"]._y -= speed;
  79. this["steam" + intI + "_mc"]._alpha -= fadeRate + randRange(2*fadeRate,4*fadeRate);
  80. this["steam" + intI + "_mc"]._rotation += (randRange(-150,150)/300);
  81.  
  82. }
  83. }
  84.  
  85. function spawnSteam (which_mc:MovieClip) {
  86. which_mc.scaleTo(randRange(20,50));
  87. which_mc.blurTo(10);
  88. which_mc._x = spawnX;// + 80 + randRange(20,40); // remove tweaking or change it if you want to adjust where in relation to the attach object you want each particle to spawn
  89. which_mc._y = spawnY;// + 140 + randRange(-5,5); // remove tweaking or change it if you want to adjust where in relation to the attach object you want each particle to spawn
  90. which_mc.alphaTo(alphaInitial, .25);
  91. var timeout:Number = setTimeout(spawnSteam, lifespan+randRange(-.5*lifespan,.5*lifespan), which_mc);
  92. }
  93.  
  94. function randRange(min:Number, max:Number):Number {
  95. var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
  96. return randomNum;
  97. }
  98.  
  99.  
  100. stop();

URL: http://www.winkyboy.com/files/steam_template.zip

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.