Jump to content
Search Community

TweenMax trouble

Macan_NBGD test
Moderator Tag

Recommended Posts

Can anyone pls help me realize what I'm doing wrong here..

goal is to animate particles into letters, and when finally completed to execute removeParticles function which should remove particles from the stage in a same manner they were added. everything works just fine except that last function (when function removeParticles executes flash player begins that 'not responding' behaviour)..any advice welcome..thanx. here's code:

 

import flash.display.BitmapData;
 import flash.display.Sprite;
 import flash.display.MovieClip;
 import com.greensock.TweenMax;
 import com.greensock.easing.*;
 import flash.display.Bitmap;
 import flash.events.Event;
 
[sWF(backgroundColor="0x000000", frameRate="30", width="800", height="200")]
 
     var positions:Array = [];
     var SPACING_Y:uint = 3; 
     var SPACING_X:uint = 3;
var circlesArray:Array = [];
     var circleRadius:uint = 3;
 
     var container:Sprite;
 
mapParticles(); 
 
function mapParticles():void
{
container = new Sprite();
addChild(container);
var letters:Letters= new Letters();
container.addChild(letters);
var bmd:BitmapData = new BitmapData(letters.width, letters.height, false, 0xffffff);
bmd.draw(letters);
 
   letters.visible = false;
 
var rows:int = letters.height / SPACING_Y;
var cols:int = letters.width / SPACING_X;
 
for(var i:int = 0; i < cols; i++)
 {
  for(var j:int = 0; j < rows; j++)
 {
 
  var pixelValue:uint = bmd.getPixel(i * SPACING_X, j * SPACING_Y);
 
  if(pixelValue.toString(16) != 'ffffff' && pixelValue.toString(16) != '0')
  {
positions.push({xpos:i * SPACING_X, ypos:j * SPACING_Y });
  }
}
  } 
 
 
shuffle(positions);
 
generateParticles();
}
  
function shuffle(arr:Array):void 
{
var rand:int;
for(var i:int= 0; i<arr.lenght; i++)
{
var tmp:*= arr[int(i)];
rand= int(Math.random()* arr.length);
arr[int(i)]=arr[rand];
arr[int(rand)]=tmp;
}
 
}
  
function generateParticles():void
{
for(var i:int = 0; i < positions.length; i++)
{
 var circle = new Sprite()
 circle.graphics.beginFill(Math.random()* 0xffffff);
 circle.graphics.drawCircle(0, 0, circleRadius);
 circle.graphics.endFill();
 container.addChild(circle);
 circle.x = Math.random() * stage.stageWidth;
 circle.y = Math.random() * stage.stageHeight;
 circle.alpha = 0; 
 circlesArray.push(circle);
}
animateParticles();
}
 
 
function animateParticles():void
{
for(var i:int = 0; i < circlesArray.length; i++)
{
 
 TweenMax.too(circlesArray,0.5, {
 x:positions.xpos,
 y:positions.ypos,
 alpha: 1,
 delay:i * 0.01, 
 ease:Back.easeOut,
 easeParams:[3], onComplete:removeParticles()});
  
}
 
}
 
function removeParticles():void
{
for(var i:int= 0; i<circlesArray.length; i++)
{
TweenMax.to(circlesArray, 1, {
x:Math.random() * stage.stageWidth, 
y:Math.random() * stage.stageHeight, 
alpha:0, 
delay:i* 0.01, 
ease:Back.easeIn, 
easeParams:[1]}); 
}
 
}
 

 

Link to comment
Share on other sites

Its difficult to imagine what exactly is causing the error but this block of code has 2 mistakes

 

BAD

TweenMax.too(circlesArray,0.5, {
 x:positions.xpos,
 y:positions.ypos,
 alpha: 1,
 delay:i * 0.01, 
 ease:Back.easeOut,
 easeParams:[3], onComplete:removeParticles()});
 
GOOD
TweenMax.to(circlesArray,0.5, {
 x:positions.xpos,
 y:positions.ypos,
 alpha: 1,
 delay:i * 0.01, 
 ease:Back.easeOut,
 easeParams:[3], onComplete:removeParticles});
 
Its very important that the onComplete callback contains just the name of the funciton. adding the () forces that function to execute when the tween is created which is bad.
 
----
 
Also it appears that all tweens remove all particles when they are complete, maybe removeParticles only needs to be called once, but I could be wrong. I'm pretty sure the fixes above should help remedy the error. If not, feel free to post an fla that is as simplified as possible to replicate the error.
 
 
Link to comment
Share on other sites

Thanx for trying Carl, but still not working. I've made those spelling errors just before posting code here, so that was not real problem, also those "( )" in tweenMax's onComplete didn't solve problem. here's fla file - you'll see that if you comment code in last (removeParticles) function everything will work just fine, so I'm definitelly doing something wrong in that one.. 


thanx in advance………


Texteffwct_2.swf.zip

Link to comment
Share on other sites

Thanks for providing the file. The problem most likely had to do with the fact that EVERY tween was calling removeParticle via its onComplete callback.

 

There were 1800 tweens running virtually simultaneously that were ending at virtually the same time trying to create and overwrite 1800 tweens each time each tween completed. 

 

The solution is to just make sure ONLY the last tween has onComplete:removeParticles like so

function animateParticles():void{
var lastIndex = circlesArray.length-1;
//loop through every circle EXCEPT the last one
for(var i:int = 0; i < lastIndex; i++)
{


 TweenMax.to(circlesArray[i],0.5, {
 x:positions[i].xpos,
 y:positions[i].ypos,
 alpha: 1,
 delay:i * 0.01, 
 ease:Back.easeOut,
 easeParams:[3]});
  
}
//only add onComplete callback to last tween of last particle
TweenMax.to(circlesArray[lastIndex],0.5, {
 x:positions[lastIndex].xpos,
 y:positions[lastIndex].ypos,
 alpha: 1,
 delay:lastIndex * 0.01, 
 ease:Back.easeOut,
 easeParams:[3], onComplete:removeParticles});


}

Just replace your animateParticles() function with the one above.

 

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...