Skip to main content
Participant
May 4, 2009
Answered

Rotating particles.

  • May 4, 2009
  • 1 reply
  • 704 views

Hi. This is the first time i have ever used Actionscript and I am having a problem which i am sure will prove trivial to you and not take up much of your time. I have created an animation which sends "particles" flying out from a central point. This all works fine. I now wan't to get them to rotate and cannot seem to get it to work. Here is my current code. Very grareful for any help:

var particleArray:Array = new Array();
var maxParticles:Number = 100;

function addparticle(e:Event)
{
   var p1:particle = new particle();
   var p2:particle2 = new particle2();
   var p3:particle3 = new particle3();
   var p4:particle4 = new particle4();
   var p5:particle5 = new particle5();
   p1.x = stage.stageWidth/2;
   p1.y = stage.stageHeight/2;
   p2.x = stage.stageWidth/2;
   p2.y = stage.stageHeight/2;
   p3.x = stage.stageWidth/2;
   p3.y = stage.stageHeight/2;
   p4.x = stage.stageWidth/2;
   p4.y = stage.stageHeight/2;
   p5.x = stage.stageWidth/2;
   p5.y = stage.stageHeight/2;
   p1.alpha = Math.random() * .8 + .2;
   p1.scaleX = p1.scaleY = Math.random() * .8 + .2;
   p2.alpha = Math.random() * .8 + .2;
   p2.scaleX = p2.scaleY = Math.random() * .8 + .2;
   p3.alpha = Math.random() * .8 + .2;
   p3.scaleX = p3.scaleY = Math.random() * .8 + .2;
   p4.alpha = Math.random() * .8 + .2;
   p4.scaleX = p4.scaleY = Math.random() * .8 + .2;
   p5.alpha = Math.random() * .8 + .2;
   p5.scaleX = p5.scaleY = Math.random() * .8 + .2;
   p1.xMovement = Math.random() * 10 - 5;
   p1.yMovement = Math.random() * 10 - 5;
   p2.xMovement = Math.random() * 10 - 5;
   p2.yMovement = Math.random() * 10 - 5;
   p3.xMovement = Math.random() * 10 - 5;
   p3.yMovement = Math.random() * 10 - 5;
   p4.xMovement = Math.random() * 10 - 5;
   p4.yMovement = Math.random() * 10 - 5;
   p5.xMovement = Math.random() * 10 - 5;
   p5.yMovement = Math.random() * 10 - 5;
   particleArray.push(p1);
   particleArray.push(p2);
   particleArray.push(p3);
   particleArray.push(p4);
   particleArray.push(p5);
   addChild(p1);
   addChild(p2);
   addChild(p3);
   addChild(p4);
   addChild(p5);
   p1.cacheAsBitmap = true ;
   p2.cacheAsBitmap = true ;
  
   if (particleArray.length>= maxParticles)
   {
       removeChild(particleArray.shift());
   }
  
   p1.addEventListener(Event.ENTER_FRAME,moveparticle);
   p2.addEventListener(Event.ENTER_FRAME,moveparticle);
   p3.addEventListener(Event.ENTER_FRAME,moveparticle);
   p4.addEventListener(Event.ENTER_FRAME,moveparticle);
   p5.addEventListener(Event.ENTER_FRAME,moveparticle);
}

function moveparticle(e:Event)
{
   e.currentTarget.x += e.currentTarget.xMovement;
   e.currentTarget.y += e.currentTarget.yMovement;
}

var myTimer:Timer = new Timer(200);
myTimer.addEventListener(TimerEvent.TIMER, addparticle);
myTimer.start();

This topic has been closed for replies.
Correct answer Ned Murphy

I liked your response, but not because you complimented me (I think)... it's refreshing to have someone who's just starting out with actionscript and is showing signs of knowing what they're doing.

Too often folks take code from somewhere that they don't understand and the response to such general direction as I offered you would be a plea to help them do it step by detailed step.

Because I liked your response, I'm gonna include a quickly revised version of your code just to serve as a reference for what I described... one you probably won't need anyways.  It could/should be refined, so consider it strictly as a demo piece, but it should plug right in as a replacement for your code.

var holderArray:Array = new Array();
var maxHolders:Number = 20;

function addparticle(e:Event)
{
var holder:MovieClip = new MovieClip();
   holder.x = stage.stageWidth/2;
   holder.y = stage.stageHeight/2;
  
   var p1:particle = new particle();
   var p2:particle2 = new particle2();
   var p3:particle3 = new particle3();
   var p4:particle4 = new particle4();
   var p5:particle5 = new particle5();

   p1.alpha = Math.random() * .8 + .2;
   p1.scaleX = p1.scaleY = Math.random() * .8 + .2;
   p2.alpha = Math.random() * .8 + .2;
   p2.scaleX = p2.scaleY = Math.random() * .8 + .2;
   p3.alpha = Math.random() * .8 + .2;
   p3.scaleX = p3.scaleY = Math.random() * .8 + .2;
   p4.alpha = Math.random() * .8 + .2;
   p4.scaleX = p4.scaleY = Math.random() * .8 + .2;
   p5.alpha = Math.random() * .8 + .2;
   p5.scaleX = p5.scaleY = Math.random() * .8 + .2;
   p1.xMovement = Math.random() * 10 - 5;
   p1.yMovement = Math.random() * 10 - 5;
   p2.xMovement = Math.random() * 10 - 5;
   p2.yMovement = Math.random() * 10 - 5;
   p3.xMovement = Math.random() * 10 - 5;
   p3.yMovement = Math.random() * 10 - 5;
   p4.xMovement = Math.random() * 10 - 5;
   p4.yMovement = Math.random() * 10 - 5;
   p5.xMovement = Math.random() * 10 - 5;
   p5.yMovement = Math.random() * 10 - 5;
   holderArray.push(holder);
   holder.addChild(p1);
   holder.addChild(p2);
   holder.addChild(p3);
   holder.addChild(p4);
   holder.addChild(p5);
   addChild(holder);
   p1.cacheAsBitmap = true ;
   p2.cacheAsBitmap = true ;
  
   if (holderArray.length>= maxHolders)
   {
       removeChild(holderArray.shift());
   }
   holder.addEventListener(Event.ENTER_FRAME,rotateHolder);
   p1.addEventListener(Event.ENTER_FRAME,moveparticle);
   p2.addEventListener(Event.ENTER_FRAME,moveparticle);
   p3.addEventListener(Event.ENTER_FRAME,moveparticle);
   p4.addEventListener(Event.ENTER_FRAME,moveparticle);
   p5.addEventListener(Event.ENTER_FRAME,moveparticle);
}


function moveparticle(e:Event)
{
   e.currentTarget.x += e.currentTarget.xMovement;
   e.currentTarget.y += e.currentTarget.yMovement;
}

function rotateHolder(e:Event)
{
   e.currentTarget.rotation += 5;
}


var myTimer:Timer = new Timer(200);
myTimer.addEventListener(TimerEvent.TIMER, addparticle);
myTimer.start();

1 reply

Ned Murphy
Legend
May 4, 2009

What you could do is modify the code so that you add the particles to an empty movieclip and then rotate the movieclip.  So you'd probably be adding the movieclip to center stage and then have the particles planted relative to the movieclip 0,0 point (effectively eliminating the need for those 10 lines of particle location code).  You could have one added dynamically for each set of particles, or have just one for all the particles.

Participant
May 4, 2009

Its logical thinking like that which only experience can bring. thanks a lot.

Ned Murphy
Ned MurphyCorrect answer
Legend
May 4, 2009

I liked your response, but not because you complimented me (I think)... it's refreshing to have someone who's just starting out with actionscript and is showing signs of knowing what they're doing.

Too often folks take code from somewhere that they don't understand and the response to such general direction as I offered you would be a plea to help them do it step by detailed step.

Because I liked your response, I'm gonna include a quickly revised version of your code just to serve as a reference for what I described... one you probably won't need anyways.  It could/should be refined, so consider it strictly as a demo piece, but it should plug right in as a replacement for your code.

var holderArray:Array = new Array();
var maxHolders:Number = 20;

function addparticle(e:Event)
{
var holder:MovieClip = new MovieClip();
   holder.x = stage.stageWidth/2;
   holder.y = stage.stageHeight/2;
  
   var p1:particle = new particle();
   var p2:particle2 = new particle2();
   var p3:particle3 = new particle3();
   var p4:particle4 = new particle4();
   var p5:particle5 = new particle5();

   p1.alpha = Math.random() * .8 + .2;
   p1.scaleX = p1.scaleY = Math.random() * .8 + .2;
   p2.alpha = Math.random() * .8 + .2;
   p2.scaleX = p2.scaleY = Math.random() * .8 + .2;
   p3.alpha = Math.random() * .8 + .2;
   p3.scaleX = p3.scaleY = Math.random() * .8 + .2;
   p4.alpha = Math.random() * .8 + .2;
   p4.scaleX = p4.scaleY = Math.random() * .8 + .2;
   p5.alpha = Math.random() * .8 + .2;
   p5.scaleX = p5.scaleY = Math.random() * .8 + .2;
   p1.xMovement = Math.random() * 10 - 5;
   p1.yMovement = Math.random() * 10 - 5;
   p2.xMovement = Math.random() * 10 - 5;
   p2.yMovement = Math.random() * 10 - 5;
   p3.xMovement = Math.random() * 10 - 5;
   p3.yMovement = Math.random() * 10 - 5;
   p4.xMovement = Math.random() * 10 - 5;
   p4.yMovement = Math.random() * 10 - 5;
   p5.xMovement = Math.random() * 10 - 5;
   p5.yMovement = Math.random() * 10 - 5;
   holderArray.push(holder);
   holder.addChild(p1);
   holder.addChild(p2);
   holder.addChild(p3);
   holder.addChild(p4);
   holder.addChild(p5);
   addChild(holder);
   p1.cacheAsBitmap = true ;
   p2.cacheAsBitmap = true ;
  
   if (holderArray.length>= maxHolders)
   {
       removeChild(holderArray.shift());
   }
   holder.addEventListener(Event.ENTER_FRAME,rotateHolder);
   p1.addEventListener(Event.ENTER_FRAME,moveparticle);
   p2.addEventListener(Event.ENTER_FRAME,moveparticle);
   p3.addEventListener(Event.ENTER_FRAME,moveparticle);
   p4.addEventListener(Event.ENTER_FRAME,moveparticle);
   p5.addEventListener(Event.ENTER_FRAME,moveparticle);
}


function moveparticle(e:Event)
{
   e.currentTarget.x += e.currentTarget.xMovement;
   e.currentTarget.y += e.currentTarget.yMovement;
}

function rotateHolder(e:Event)
{
   e.currentTarget.rotation += 5;
}


var myTimer:Timer = new Timer(200);
myTimer.addEventListener(TimerEvent.TIMER, addparticle);
myTimer.start();