Chinaunix首页 | 论坛 | 博客
  • 博客访问: 474937
  • 博文数量: 280
  • 博客积分: 337
  • 博客等级: 二等列兵
  • 技术积分: 1957
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-17 21:36
文章分类

全部博文(280)

文章存档

2017年(13)

2016年(38)

2015年(78)

2014年(67)

2013年(70)

2012年(14)

分类: C/C++

2013-07-02 11:14:37

大元件上面的小元件也可以飞

点击(此处)折叠或打开

  1. /**
  2.  * ...
  3.  * @author ...
  4.  */

  5. class CFSFlyWin extends MovieClip
  6. {
  7.     var mChildArray:Array;
  8.     var mChildNameArray:Array;
  9.     var mChildTextArray:Array;
  10.     var mChildPosXArray:Array;
  11.     var mChildPosYArray:Array;
  12.     var mChildInterval:Number = 0;
  13.     var mTargetX:Number = 0;
  14.     var mTargetY:Number = 480;
  15.     var mSpeedX:Number = 0;
  16.     var mSpeedY:Number = 0;
  17.     var mPublicChildsX:Number = 0;
  18.     var mNewChildAddedNum:Number = 0;
  19.     var mChildsMaxLen:Number = 0;
  20.     var mFlyUnitNum:Number = 1; //parent本身就是1
  21.     var mFlyingUnitNum:Number = 0;
  22.     
  23.     public function CFSFlyWin()
  24.     {
  25.         trace("CFSFlyWin()");
  26.         this._x = 0;
  27.         this._y = 960;
  28.         mChildArray = new Array();
  29.         mChildNameArray = new Array();
  30.         mChildTextArray = new Array();
  31.         mChildPosXArray = new Array();
  32.         mChildPosYArray = new Array();
  33.     }
  34.     
  35.     public function setStartPos(x, y)
  36.     {
  37.         trace("CFSFlyWin setPos()");
  38.         this._x = x;
  39.         this._y = y;
  40.     }
  41.     
  42.     public function setTargetPos(x, y)
  43.     {
  44.         trace("CFSFlyWin setTargetPos()");
  45.         this.mTargetX = x;
  46.         this.mTargetY = y;
  47.     }
  48.     
  49.     public function addOneChild(elemName)//, textName, x, y)
  50.     {
  51.         if (mFlyingUnitNum > 0)
  52.         {
  53.             return;
  54.         }
  55.         mNewChildAddedNum = mNewChildAddedNum + 1;
  56.         mChildNameArray.push(elemName);
  57.         mFlyUnitNum = mFlyUnitNum + 1;
  58.     }
  59.     
  60.     //mc是要执行fly动作的obj,x、y是起始地址,tX、tY是目标地址,
  61.     //times是帧数, 帧数=帧频*时间,比如flash 30帧/s,想用0.3秒执行fly动作,帧数=30*0.3=9
  62.     public function _mcFly(mc, x, y, tX, tY, times)
  63.     {
  64.         trace("CFSFlyWin _mcFly() " + mc);

  65.         if ((x == tX) && (y == tY))
  66.         {
  67.             return;
  68.         }
  69.     
  70.         mc._x = x;
  71.         mc._y = y;
  72.         var speedX:Number = (tX - x) / times;
  73.         var speedY:Number = (tY - y) / times;

  74.         mc.onEnterFrame = function()
  75.         {
  76.             times = times - 1;
  77.             this._x = this._x + speedX;
  78.             this._y = this._y + speedY;
  79.             if (times <= 0)
  80.             {
  81.                 this._x = tX;
  82.                 this._y = tY;
  83.                 
  84.                 if (mFlyingUnitNum == undefined)
  85.                 {
  86.                     _parent.mFlyingUnitNum = _parent.mFlyingUnitNum - 1;
  87.                 }
  88.                 else
  89.                 {
  90.                     mFlyingUnitNum = mFlyingUnitNum - 1;
  91.                 }
  92.                 
  93.                 mc.onEnterFrame = null;
  94.             }            
  95.         }
  96.     }
  97.     
  98.     //mc是要执行fly动作的obj,x、y是起始地址,tX、tY是目标地址,
  99.     //parent_frames是parent飞行帧数,child_interval是child飞行帧数差
  100.     public function mcFly(mc, x, y, tX, tY, parent_frames, child_interval)
  101.     {
  102.         trace("CFSFlyWin mcFly()");
  103.         if ((x == tX) && (y == tY) || (mFlyingUnitNum > 0))
  104.         {
  105.             return;
  106.         }
  107.         mFlyingUnitNum = mFlyUnitNum;
  108.         
  109.         var child;
  110.         var child_x:Number = 0;
  111.         var child_y:Number = 0;
  112.         
  113.         this._mcFly(mc, x, y, tX, tY, parent_frames);
  114.         
  115.         for (var i = 0; i < mChildArray.length; i++)
  116.         {
  117.             child = this.mChildArray[i];
  118.             child_x = mChildPosXArray[i];
  119.             child_y = mChildPosYArray[i];
  120.             this._mcFly(child, child_x + x - tX, child_y + y - tY, child_x, child_y, parent_frames + child_interval*(i+1));
  121.         }
  122.     }
  123.     
  124.     public function caculateInterval()
  125.     {
  126.         mChildInterval = this._height;
  127.         
  128.         for (var i = 0; i < this.mChildArray.length; i++)
  129.         {
  130.             mChildInterval = mChildInterval - mChildArray[i]._height;
  131.         }
  132.         mChildInterval = mChildInterval / (mChildArray.length + 1);
  133.     }
  134.     
  135.     public function loadChilds()
  136.     {
  137.         trace("CFSFlyWin loadChilds()");
  138.         if (mNewChildAddedNum <= 0)
  139.         {
  140.             trace("..........");
  141.             return;
  142.         }
  143.         
  144.         for (var i = mChildArray.length; i < this.mChildNameArray.length; i++)
  145.         {
  146.             var child = attachMovie(this.mChildNameArray[i], "" + this + this.mChildNameArray[i], this.getNextHighestDepth());
  147.             mChildArray.push(child);
  148.             mChildPosXArray.push(0);
  149.             mChildPosYArray.push(0);

  150.             if (mChildsMaxLen < child._width)
  151.             {
  152.                 mChildsMaxLen = child._width;
  153.             }
  154.         }
  155.         
  156.         //计算各child间隔高度
  157.         caculateInterval();
  158.         
  159.         //所有child以最大长度居中左对齐
  160.         mPublicChildsX = (this._width - mChildsMaxLen) / 2;
  161.         
  162.         var yChild:Number = this.mChildInterval;
  163.         for (var i = 0; i < mChildArray.length; i++)
  164.         {
  165.             mChildArray[i]._x = mPublicChildsX;
  166.             mChildArray[i]._y = yChild;
  167.             mChildPosXArray[i] = mPublicChildsX;
  168.             mChildPosYArray[i] = yChild;
  169.             yChild = yChild + this.mChildInterval + this.mChildArray[i]._height;
  170.         }
  171.         
  172.         //load完毕,新child数量重置为0
  173.         mNewChildAddedNum = 0;
  174.     }
  175. }

改进版本,gameswf解析到c++可用,上面的flash里用as2.0可以跑,但是gameswf解析到c++里跑步起

点击(此处)折叠或打开

  1. /**
  2.  * ...
  3.  * @author ...
  4.  */

  5. class CFSFlyWin extends MovieClip
  6. {
  7.     var mChildArray:Array;
  8.     var mChildNameArray:Array;
  9.     var mChildTextArray:Array;
  10.     var mChildPosXArray:Array;
  11.     var mChildPosYArray:Array;
  12.     var mChildInterval:Number = 0;
  13.     var mTargetX:Number = 0;
  14.     var mTargetY:Number = 480;
  15.     var mSpeedX:Number = 0;
  16.     var mSpeedY:Number = 0;
  17.     var mPublicChildsX:Number = 0;
  18.     var mNewChildAddedNum:Number = 0;
  19.     var mChildsMaxLen:Number = 0;
  20.     var misFlying:Boolean = false; //parent本身就是1
  21.     var mFlyFrames:Number = 0;
  22.     
  23.     public function CFSFlyWin()
  24.     {
  25.         trace("CFSFlyWin()");
  26.         this._x = 0;
  27.         this._y = 960;
  28.         mChildArray = new Array();
  29.         mChildNameArray = new Array();
  30.         mChildTextArray = new Array();
  31.         mChildPosXArray = new Array();
  32.         mChildPosYArray = new Array();
  33.     }
  34.     
  35.     public function setStartPos(x, y)
  36.     {
  37.         trace("CFSFlyWin setPos()");
  38.         this._x = x;
  39.         this._y = y;
  40.     }
  41.     
  42.     public function setTargetPos(x, y)
  43.     {
  44.         trace("CFSFlyWin setTargetPos()");
  45.         this.mTargetX = x;
  46.         this.mTargetY = y;
  47.     }
  48.     
  49.     public function addOneChild(elemName)//, textName, x, y)
  50.     {
  51.         if (this.misFlying == true)
  52.         {
  53.             return;
  54.         }
  55.         for (var i = 0; i < this.mChildArray.length; i++)
  56.         {
  57.             if (this.mChildArray[i].isFlying == true)
  58.             {
  59.                 return;
  60.             }
  61.         }

  62.         mNewChildAddedNum = mNewChildAddedNum + 1;
  63.         mChildNameArray.push(elemName);
  64.     }
  65.     
  66.     public function caculateInterval()
  67.     {
  68.         this.mChildInterval = this._height;
  69.         for (var i = 0; i < this.mChildArray.length; i++)
  70.         {
  71.             this.mChildInterval = this.mChildInterval - this.mChildArray[i]._height;
  72.         }
  73.         mChildInterval = mChildInterval / (mChildArray.length + 1);
  74.     }
  75.     
  76.     public function loadChilds()
  77.     {
  78.         
  79.         if (mNewChildAddedNum <= 0)
  80.         {
  81.             trace("..........");
  82.             return;
  83.         }

  84.         for (var i = mChildArray.length; i < this.mChildNameArray.length; i++)
  85.         {
  86.             var child = attachMovie(this.mChildNameArray[i], "" + this + this.mChildNameArray[i], this.getNextHighestDepth());
  87.             child._x = 0;
  88.             child._y = 0;
  89.             mChildArray.push(child);
  90.             mChildPosXArray.push(0);
  91.             mChildPosYArray.push(0);

  92.             if (mChildsMaxLen < child._width)
  93.             {
  94.                 mChildsMaxLen = child._width;
  95.             }
  96.         }
  97.         
  98.         //计算各child间隔高度
  99.         caculateInterval();
  100.         
  101.         //所有child以最大长度居中左对齐
  102.         mPublicChildsX = (this._width - mChildsMaxLen) / 2;

  103.         var yChild:Number = this.mChildInterval;
  104.         for (var i = 0; i < mChildArray.length; i++)
  105.         {
  106.             mChildArray[i]._x = mPublicChildsX;
  107.             mChildArray[i]._y = yChild;
  108.             mChildPosXArray[i] = mPublicChildsX;
  109.             mChildPosYArray[i] = yChild;
  110.             yChild = yChild + this.mChildInterval + this.mChildArray[i]._height;
  111.         }
  112.         
  113.         //load完毕,新child数量重置为0
  114.         mNewChildAddedNum = 0;
  115.     }
  116.         
  117.     //mc是要执行fly动作的obj,x、y是起始地址,tX、tY是目标地址,
  118.     //times是帧数, 帧数=帧频*时间,比如flash 30帧/s,想用0.3秒执行fly动作,帧数=30*0.3=9
  119.     public function mcFly(x, y, tX, tY, frames)
  120.     {        
  121.         if ((x == tX) && (y == tY) || (this.misFlying == true))
  122.         {
  123.             return;
  124.         }
  125.         
  126.         this.misFlying = true;
  127.         
  128.         this._x = x;
  129.         this._y = y;
  130.         this.mTargetX = tX;
  131.         this.mTargetY = tY;
  132.         this.mFlyFrames = frames;
  133.         this.mSpeedX = (this.mTargetX - this._x) / this.mFlyFrames;
  134.         this.mSpeedY = (this.mTargetY - this._y) / this.mFlyFrames;

  135.         this.onEnterFrame = function()
  136.         {
  137.             this.mFlyFrames = this.mFlyFrames - 1;
  138.             this._x = this._x + this.mSpeedX;
  139.             this._y = this._y + this.mSpeedY;
  140.             
  141.             if (mFlyFrames <= 0)
  142.             {
  143.                 this._x = this.mTargetX;
  144.                 this._y = this.mTargetY;
  145.                 this.misFlying = false;
  146.                 this.onEnterFrame = null;
  147.             }            
  148.         }
  149.     }
  150.     
  151.     //mc是要执行fly动作的obj,x、y是起始地址,tX、tY是目标地址,
  152.     //parent_frames是parent飞行帧数,child_interval是child飞行帧数差
  153.     public function mcFlyAll(x, y, tX, tY, parent_frames, child_interval)
  154.     {
  155.         trace("CFSFlyWin mcFlyAll()");
  156.         if ((x == tX) && (y == tY) || (this.misFlying == true))
  157.         {
  158.             return;
  159.         }
  160.         
  161.         for (var i = 0; i < this.mChildArray.length; i++)
  162.         {
  163.             if (this.mChildArray[i].isFlying == true)
  164.             {
  165.                 return;
  166.             }
  167.         }
  168.         
  169.         this.mcFly(x, y, tX, tY, parent_frames);
  170.         
  171.         var child;
  172.         var child_x:Number = 0;
  173.         var child_y:Number = 0;
  174.         
  175.         for (var i = 0; i < mChildArray.length; i++)
  176.         {
  177.             child = this.mChildArray[i];
  178.             child_x = mChildPosXArray[i];
  179.             child_y = mChildPosYArray[i];
  180.             child.mcFly(child_x + x - tX, child_y + y - tY, child_x, child_y, parent_frames + child_interval*(i+1));
  181.         }
  182.     }
  183. }



阅读(1712) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~