dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: AS3 Tweening with If Statement, animation problem

  1. #1
    Junior Member
    Join Date
    Jun 2015
    Posts
    1

    AS3 Tweening with If Statement, animation problem

    Hello,

    I've got this Flash AS3 code I'm playing around with below. Im trying to get the spheres created to move off the stage at frame 81 (of a 360 frame timeline). I'm using an If statement but I think its causing problems as the tween is happening every time the code runs after frame 81 causing the spheres to judder.

    If you let the animation loop the problem seems to go away?

    I'm sure this can't be too tricky, please help an absolute beginner out!

    Many thanks
    ZM


    Code:
    import fl.transitions.Tween;
        import fl.transitions.easing.*;
        import fl.transitions.TweenEvent;
        
        //number of balls
        var numBalls:uint = 100;
        var defaultBallSize:uint = 15;
        
        //init
        makeDots();
        
        function makeDots():void {
            //create desired number of balls
            for (var ballNum:uint=0; ballNum<numBalls; ballNum++){
                var c1:Number = 0xA35853;
                var c2:Number = 0xA35853;
        	
        
                //create ball
        		
        		var thisBall:MovieClip = new MovieClip();
                thisBall.graphics.beginFill(c1, .9);
                /*thisBall.graphics.lineStyle(defaultBallSize/3, c2, .9);*/
                thisBall.graphics.drawCircle(defaultBallSize, defaultBallSize, defaultBallSize);
                thisBall.graphics.endFill();
        		
                addChild(thisBall);
        		
                //coordinates
        		
        		
        	
                thisBall.x = Math.random() * stage.stageWidth;
                thisBall.y = Math.random() * stage.stageHeight;
        		
        	
        			
        		//percieved depth
                thisBall.ballNum = ballNum;
                thisBall.depth = ballNum/numBalls;
                thisBall.scaleY = thisBall.scaleX = thisBall.alpha = ballNum/numBalls;
                //velocity
                thisBall.vx = 0;
                thisBall.vy = 0;
                thisBall.vz = 0;
        
                //ball animation
                thisBall.addEventListener(Event.ENTER_FRAME, animateBall);
        		/*var myTweenOn:Tween = new Tween(thisBall, "alpha", Regular.easeIn, 0, 0.3, 6, true);*/
            }
        }
        
        
        
        
        var dampen:Number = 0.95;
        var maxScale:Number = 1.3;
        var minScale:Number = 0.1;
        var maxAlpha:Number = 0.4;
        var minAlpha:Number = 0.3;
        function animateBall(e:Event):void{
            var thisBall:Object = e.target;
        
        
        	//apply randomness to velocity
            thisBall.vx += Math.random() * 0.2 - 0.1;
            thisBall.vy += Math.random() * 0.2 - 0.1;
            thisBall.vz += Math.random() * 0.002 - 0.001;
        
            thisBall.x += thisBall.vx;
            thisBall.y += thisBall.vy;
            thisBall.scaleX = thisBall.scaleY += thisBall.vz;
            thisBall.alpha += thisBall.vz;
            thisBall.vx *= dampen;
            thisBall.vy *= dampen;
            thisBall.vz *= dampen;
        	
        	
        	
        	
        	if(currentFrame > 81) {
        		var myTween:Tween = new Tween(thisBall, "alpha", Regular.easeOut, thisBall.alpha, 0, 2, true);
        		
        	
        		/*thisBall.alpha = minAlpha;*/
        	}
        	else(currentFrame < 82);  {
        		thisBall.alpha = thisBall.alpha;
        	}
        	
        	if(currentFrame > 81 && thisBall.x < stage.stageWidth/2 ) { 
        			var myTweenXLeft:Tween = new Tween(thisBall, "x", None.easeOut, thisBall.x, thisBall.x - 140, 1, true);
        			thisBall.x = thisBall.x;
        	}
        	if(currentFrame > 81 && thisBall.x > stage.stageWidth/2 ) { 
        			var myTweenXRight:Tween = new Tween(thisBall, "x", Regular.easeOut, thisBall.x, thisBall.x + 140, 1, true);
        			thisBall.x = thisBall.x;
        	}
        	
        	
        	//else(currentFrame < 81);  {
        	//	thisBall.x = thisBall.x;
        	//	/*thisBall.y = thisBall.y;*/
        	//}
        	
        	if(currentFrame > 81 && thisBall.y < stage.stageHeight/2 ) { 
        			var myTweenYUp:Tween = new Tween(thisBall, "y", Regular.easeOut, thisBall.y, thisBall.y - 115, 1, true);
        			thisBall.y = thisBall.y;
        	}
        	if(currentFrame > 81 && thisBall.y > stage.stageHeight/2 ) { 
        			var myTweenYDown:Tween = new Tween(thisBall, "y", Regular.easeOut, thisBall.y, thisBall.y + 115, 1, true);
        			thisBall.y = thisBall.y;
        	}
        	
        	
        	//else(currentFrame < 81);  {
        	//	/*thisBall.x = thisBall.x;*/
        	//	thisBall.y = thisBall.y;
        	//}
        	
        	
        	
            if(thisBall.x > stage.stageWidth) {
                thisBall.x = 0 - thisBall.width;
            }
            else if(thisBall.x < 0 - thisBall.width) {
                thisBall.x = stage.stageWidth;
            }
            if(thisBall.y > stage.stageHeight) {
                thisBall.y = 0 - thisBall.height;
            }
            else if(thisBall.y < 0 - thisBall.height) {
                thisBall.y = stage.stageHeight;
            }
        
            if (thisBall.scaleX > maxScale){
                thisBall.scaleX = thisBall.scaleY = maxScale;
            }
            else if (thisBall.scaleX < minScale){
                thisBall.scaleX = thisBall.scaleY = minScale;
            }
            if (thisBall.alpha > maxAlpha){
                thisBall.alpha = maxAlpha;
            }
            else if (thisBall.alpha < minAlpha){
                thisBall.alpha = minAlpha;
            }
        }
        
        function randomColor():Number{
            return Math.floor(Math.random() * 16777215);
        }
        
        
        /*
            function fadeBall(e:Event):void{
        		var thisBall:Object = e.target;
        	if(currentFrame > 48) {
        		thisBall.alpha == 0;
        	}
        }*/

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    I'm not entirely sure what you are trying to do, with my mock up as you didnt include any files, I come up with this.
    PHP Code:
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import fl.transitions.TweenEvent;

    //number of balls
    var numBalls:uint 100;
    var 
    defaultBallSize:uint 15;

    //init
    makeDots();

    function 
    makeDots():void
    {
        
    //create desired number of balls
        
    for (var ballNum:uint=0ballNum<numBallsballNum++)
        {
            var 
    c1:Number 0xA35853;
            var 
    c2:Number 0xA35853;

            
    //create ball

            
    var thisBall:MovieClip = new MovieClip();
            
    thisBall.graphics.beginFill(c1.9);
            
    /*thisBall.graphics.lineStyle(defaultBallSize/3, c2, .9);*/
            
    thisBall.graphics.drawCircle(defaultBallSizedefaultBallSizedefaultBallSize);
            
    thisBall.graphics.endFill();

            
    //coordinates;

            
    thisBall.Math.random() * stage.stageWidth;
            
    thisBall.Math.random() * stage.stageHeight;

            
    //percieved depth
            
    thisBall.ballNum ballNum;
            
    thisBall.depth ballNum numBalls;
            
    thisBall.scaleY thisBall.scaleX thisBall.alpha ballNum numBalls;
            
    //velocity
            
    thisBall.vx 0;
            
    thisBall.vy 0;
            
    thisBall.vz 0;

            
    //ball animation
            
    thisBall.addEventListener(Event.ENTER_FRAMEanimateBall);
            
    /*var myTweenOn:Tween = new Tween(thisBall, "alpha", Regular.easeIn, 0, 0.3, 6, true);*/
            
    addChild(thisBall);
        }
    }

    var 
    dampen:Number 0.95;
    var 
    maxScale:Number 1.3;
    var 
    minScale:Number 0.1;
    var 
    maxAlpha:Number 0.4;
    var 
    minAlpha:Number 0.3;

    function 
    animateBall(e:Event):void
    {
        var 
    thatBall:Object e.target;
        var 
    cFrame:Number currentFrame;

        
    //apply randomness to velocity
        
    thatBall.vx +=  Math.random() * 0.2 0.1;
        
    thatBall.vy +=  Math.random() * 0.2 0.1;
        
    thatBall.vz +=  Math.random() * 0.002 0.001;

        
    thatBall.+=  thatBall.vx;
        
    thatBall.+=  thatBall.vy;
        
    thatBall.scaleX thatBall.scaleY +=  thatBall.vz;
        
    thatBall.alpha +=  thatBall.vz;
        
    thatBall.vx *=  dampen;
        
    thatBall.vy *=  dampen;
        
    thatBall.vz *=  dampen;

        if (
    cFrame 81)
        {
            var 
    myTween:Tween = new Tween(thatBall,"alpha",Regular.easeOut,thatBall.alpha,0,2,true);

            if (
    cFrame 81 && thatBall.stage.stageWidth )
            {
                var 
    myTweenXLeft:Tween = new Tween(thatBall,"x",None.easeOut,thatBall.x,thatBall.140,1,true);
            }
            else if (
    cFrame 81 && thatBall.stage.stageWidth )
            {
                var 
    myTweenXRight:Tween = new Tween(thatBall,"x",Regular.easeOut,thatBall.x,thatBall.140,1,true);
            }

            if (
    cFrame 81 && thatBall.stage.stageHeight )
            {
                var 
    myTweenYUp:Tween = new Tween(thatBall,"y",Regular.easeOut,thatBall.y,thatBall.115,1,true);
            }
            else if (
    cFrame 81 && thatBall.stage.stageHeight )
            {
                var 
    myTweenYDown:Tween = new Tween(thatBall,"y",Regular.easeOut,thatBall.y,thatBall.115,1,true);
            }

            if (
    thatBall.stage.stageWidth)
            {
                
    thatBall.thatBall.width;
            }
            else if (
    thatBall.thatBall.width)
            {
                
    thatBall.stage.stageWidth thatBall.width;
            }
            if (
    thatBall.stage.stageHeight)
            {
                
    thatBall.thatBall.height;
            }
            else if (
    thatBall.thatBall.height)
            {
                
    thatBall.stage.stageHeight thatBall.height;
            }

            if (
    thatBall.scaleX maxScale)
            {
                
    thatBall.scaleX thatBall.scaleY maxScale;
            }
            else if (
    thatBall.scaleX minScale)
            {
                
    thatBall.scaleX thatBall.scaleY minScale;
            }
            if (
    thatBall.alpha maxAlpha)
            {
                
    thatBall.alpha maxAlpha;
            }
            else if (
    thatBall.alpha minAlpha)
            {
                
    thatBall.alpha minAlpha;
            }

            
    thatBall.removeEventListener(Event.ENTER_FRAMEanimateBall);
            return;
        }
    }

    function 
    randomColor():Number
    {
        return 
    Math.floor(Math.random() * 16777215);
    }

    /*
    function fadeBall(e:Event):void
    {
    var thisBall:Object = e.target;
    if (cFrameFrame > 48)
    {
    thisBall.alpha == 0;
    }
    }*/ 

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    3
    Have you tried using event dispatcher ? The dispatcher could be easily triggered from any frame you want

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center