Monday, July 2, 2012

[Action Script] How to tween an object to target without any tween libraries and without easing? June,2012

So I want to tween an object to targetX and targetY without any tween libraries and without easing.

I have always used greensock for all my tweens and for this case greensock's DynamicPropsPlugin would be perfect, but its only for club members......

This time I need to tween an object to target which will be changing dynamically.

I know how to tween objects with easing, but this time I need to tween an object without it, so after reaching its first target it would continue smoothly to next target.

This is code that works, but the problem is the easing:

ActionScript Code: package  {    import flash.display.*;    import flash.events.*;        /**     * ...     * @author fjo     */    [SWF(width="1200", height="600", frameRate="30", backgroundColor="#ffffff")]    public class Test extends Sprite     {                // first target coordinates        private var targetX:Number=600;        private var targetY:Number = 450;                // second target coordinates        private var targetX2:Number=900;        private var targetY2:Number = 300;                // check if first target is reached        private var _pointReached:Boolean                // object to tween        private var _ball:Sprite;                public function Test()         {            init()        }                private function init():void         {            // simple ball             _ball = new Sprite();            _ball.graphics.beginFill(0xff0000);            _ball.graphics.drawCircle( -50, -50, 50);            _ball.graphics.endFill();            addChild(_ball);                        addEventListener(Event.ENTER_FRAME, onLoop);        }            private function onLoop(e:Event):void         {            var vx:Number;             var vy:Number;                         if (_pointReached) { // next target                // easing                vx = (targetX2 - _ball.x) * .2;                vy = (targetY2 - _ball.y) * .2;            }else {                // easing                vx = (targetX - _ball.x) * .2;                vy = (targetY - _ball.y) * .2;            }                        // check distance            var dx:Number = targetX - _ball.x;            var dy:Number = targetY - _ball.y;            var dist:Number = Math.sqrt(dx * dx + dy * dy);                        // some lousy distance checking            if (dist <= 1) {                // first target point reached                _pointReached = true;            }                        // tween an object            _ball.x += vx;            _ball.y += vy;                    }            }}
Any thoughts?
Thanks!
How to tween an object to target without any tween libraries and without easing?

Related Post



0 comments: