All konw。TweenMax带来了很多方便,也节省了我们很多的时间。下边给出一个效果,将一组显示对象丢出舞台或丢回舞台。

该想法来自于《完美世界射雕zero》印象站。

思路是:随机出一定范围的x坐标,y坐标,旋转角度。然后,随机这些现实对象的延长时间。这个所谓的延长时间,是TweenMax自己处理的。我们只要给出就行。再加一些透明度,缩放。

最后,用一个for循环TweenMax所有你想丢的显示对象。

先看效果,请拿起你的小小手,点击舞台吧。

这里,仅仅是给出的随机颜色的小方块。如果项目中,可以是图片也可以是其他比较好看的小对象。也不一定非要那么整齐,大小一致。位置等。自己发挥。

好啦,下边贴出完整代码。供参考。

文档类:Main.as

package com.vini123
{
    import com.greensock.TweenMax;
    import com.vini123.ui.Rect;
    import com.vini123.ui.Tishi;
    
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    
    
    [SWF(width = "680" , height="400" , frameRate="30")]
    
    public class Main extends Sprite
    {
        private var _contain:Sprite;
        private var _alowMove:Boolean = false;
        private var _willInorOut:String = "In";
        private var _step:int = 0;
        
        private var _originalPosList:Vector.<Point> = new Vector.<Point>();
        
        private var _len:int = 12 ;
        private var _gap:int = 10 ;
        private var _width:int = 100 ;
        private var _height:int = 75 ;
        
        private var _tishi:Tishi;
        
        public function Main()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE ;
            stage.align = StageAlign.TOP_LEFT ;
            
            
            addRect();
            
            _tishi = new Tishi();
            addChild(_tishi);
            _tishi.x = stage.stageWidth * 0.5 - _tishi.width * 0.5 ;
            _tishi.y = stage.stageHeight  - _tishi.height - 10 ;
            
            _alowMove = true;
            _willInorOut = "Out"
            stage.addEventListener(MouseEvent.MOUSE_DOWN , mouseDownHandler);
        }
        
        private function addRect():void
        {
            _contain = new Sprite();
            addChild(_contain);
            
            var xNum:int = Math.floor(stage.stageWidth / _width);
            
            for(var i:int = 0 ; i< _len ; i++)
            {
                var rect:Rect = new Rect(_width , _height);
                _contain.addChild(rect);
                rect.name = "rect" + i;
                rect.x = (rect.width + _gap) * (i%xNum);
                rect.y = (rect.height + _gap) * Math.floor(i/xNum);
                _originalPosList.push(new Point(rect.x , rect.y));
            }
            
            _contain.x = stage.stageWidth *0.5  - _contain.width * 0.5 ;
            _contain.y = stage.stageHeight * 0.5   - _contain.height * 0.5 ;    
        }
        
        private function mouseDownHandler(e:MouseEvent):void
        {
            if(_alowMove)
            {
                _alowMove = false ;
                _step = 0 ;
                if(_willInorOut == "Out")
                {
                    _willInorOut = "In" ;
                    moveOut() ;
                }
                else if(_willInorOut == "In")
                {
                    _willInorOut = "Out" ;
                    moveIn() ;
                }
            }
        }
        
        private function moveOut():void
        {
            var tempNumList:Vector.<int> = new Vector.<int>();
            var tempNum:int;
            var delayNum:int;
            var tempX:int;
            var tempY:int;
            var tempRotation:int;
            
            for (var i:int = 0; i< _len; i++)
            {
                tempNumList.push(i);
            }
            
            for (i = 0; i<_len; i++)
            {
                tempNum = Math.floor(Math.random() * tempNumList.length);
                delayNum = tempNumList[tempNum];
                tempNumList.splice(tempNum,1);
                tempX = 340 + Math.round(Math.random() * 680);
                tempY = -400 + Math.round(Math.random() * 200);
                tempRotation = Math.floor((Math.random() * 180));
                TweenMax.to(_contain.getChildByName("rect" + i) , 0.5 , {
                    alpha:0,
                    rotation:tempRotation,
                    x:tempX,
                    y:tempY,
                    scaleX:1.5,
                    scaleY:1.5,
                    delay:delayNum * 0.07,
                    onComplete:moveHandler
                });
            }
        }
        
        private function moveHandler():void
        {
            _step ++ ;
            
            if(_step == _len)
            {
                _alowMove = true ;
            }
        }
        
        private function moveIn():void
        {
            var tempNumList:Vector.<int> = new Vector.<int>();
            var tempNum:int;
            var delayNum:int;
            var tempX:int;
            var tempY:int;
            var tempRotation:int;
            
            for (var i:int = 0; i< _len; i++)
            {
                tempNumList.push(i);
            }
            
            for (i = 0; i<_len; i++)
            {
                tempNum = Math.floor(Math.random() * tempNumList.length);
                delayNum = tempNumList[tempNum];
                tempNumList.splice(tempNum,1);
                tempX = _originalPosList[i].x;
                tempY = _originalPosList[i].y;
                tempRotation = 0
                TweenMax.to(_contain.getChildByName("rect" + i) , 0.5 , {
                    alpha:1,
                    rotation:tempRotation,
                    x:tempX,
                    y:tempY,
                    scaleX:1,
                    scaleY:1,
                    delay:delayNum * 0.07,
                    onComplete:moveHandler
                });
            }
        }
    }
}

 

package com.vini123.ui
{
    import flash.display.Sprite;
    
    public class Rect extends Sprite
    {
        private var _w:int = 200 ;
        private var _h:int = 150 ;
        
        public function Rect(w:int = 200 , h:int = 150)
        {
            _w = w ;
            _h = h ;
            
            draw();
        }
        
        private function draw():void
        {
            this.graphics.clear();
            this.graphics.beginFill(Math.random() * 0xffffff);
            this.graphics.drawRect(0 , 0 , _w , _h);
            this.graphics.endFill();
        }
    }
}

 

package com.vini123.ui
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
    
    public class Tishi extends Sprite
    {
        private var _txt:TextField;
        private var _txtFormat:TextFormat;
        private var _size:int = 14;
        private var _font:String = "Microsoft YaHei";
        private var _color:int = 0xffffff * Math.random();
        private var _type:String = TextFieldType.DYNAMIC;
        
        public function Tishi()
        {
            _txt = new TextField();
            addChild(_txt);
            
            _txtFormat = new TextFormat();
            _txtFormat.leading = 10;
            _txtFormat.letterSpacing = 0.5;
            _txtFormat.bold = true;
            _txtFormat.color = _color;
            _txtFormat.size = _size;
            _txtFormat.font = _font;
            
            _txt.wordWrap = false;
            _txt.multiline = false;
            _txt.autoSize = TextFieldAutoSize.LEFT;
            _txt.type = _type;
            
            _txt.text = "Hi,boy or girl!请点击丢掉图片或寻回图片!"
            _txt.setTextFormat(_txtFormat);
                
            _txt.mouseEnabled = false;
            this.mouseEnabled = false;
        }
    }
}